Implement button navigation for language selection in Translator Cog. Introduce ButtonMenu class for managing embed pagination and update message sending logic to utilize embeds with navigation buttons. Remove outdated requirements file.
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run

This commit is contained in:
Valerie 2025-05-24 03:21:26 -04:00
parent e549698124
commit 3fcd50cbf7
3 changed files with 43 additions and 17 deletions

View file

@ -1,3 +1,29 @@
from .translator import setup
from redbot.core import commands
from redbot.core.bot import Red
import discord
from googletrans import Translator
from datetime import datetime
from typing import Optional, Union
import asyncio
class ButtonMenu(discord.ui.View):
def __init__(self, embeds: list, timeout: int = 180):
super().__init__(timeout=timeout)
self.embeds = embeds
self.current_page = 0
@discord.ui.button(label="◀️", style=discord.ButtonStyle.gray)
async def previous(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.current_page > 0:
self.current_page -= 1
await interaction.response.edit_message(embed=self.embeds[self.current_page])
@discord.ui.button(label="▶️", style=discord.ButtonStyle.gray)
async def next(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.current_page < len(self.embeds) - 1:
self.current_page += 1
await interaction.response.edit_message(embed=self.embeds[self.current_page])
__red_end_user_data_statement__ = "This cog does not persistently store data about users."

View file

@ -1,3 +0,0 @@
Red-DiscordBot>=3.5.0
discord.py>=2.0.0
googletrans==3.1.0a0 # Using this specific version as it's more stable with Google Translate

View file

@ -4,19 +4,18 @@ import discord
from discord import app_commands
import asyncio
from datetime import datetime
from redbot.core.bot import Red
from . import ButtonMenu
class TranslatorCog(commands.Cog):
"""Translate messages using Google Translate"""
def __init__(self, bot):
def __init__(self, bot: Red):
self.bot = bot
self.translator = Translator()
self.languages = LANGUAGES
self.config = Config.get_conf(self, identifier=95932766180)
default_guild = {
"use_embeds": True
}
self.config.register_guild(**default_guild)
self.config = Config.get_conf(self, identifier=95932766180, force_registration=True)
self.config.register_guild(use_embeds=True)
async def cog_unload(self):
"""Cleanup when cog is unloaded."""
@ -232,7 +231,7 @@ class TranslatorCog(commands.Cog):
common_lang_list = []
for code in common_languages:
if code in self.languages:
common_lang_list.append(f"`{code}`: {self.languages[code].title()}")
common_lang_list.append(f"`{code}`: {self.languages[code]}")
embed.add_field(
name="📌 Common Languages",
value="\n".join(common_lang_list),
@ -244,20 +243,24 @@ class TranslatorCog(commands.Cog):
chunks = [remaining_languages[i:i + 15] for i in range(0, len(remaining_languages), 15)]
# Add footer with requester info
embed.set_footer(text=f"Requested by {ctx.author}ID: {ctx.author.id}")
embed.set_footer(text=f"Requested by {ctx.author}Page 1/{len(chunks) + 1}")
# Send the main embed
await ctx.send(embed=embed, reference=ctx.message)
# Create list of embeds
embeds = [embed]
# Send additional embeds for remaining languages
# Create additional embeds for remaining languages
for i, chunk in enumerate(chunks):
chunk_embed = discord.Embed(
color=discord.Color.from_rgb(25, 118, 210),
timestamp=datetime.utcnow()
)
chunk_embed.description = "\n".join(f"`{code}`: {lang.title()}" for code, lang in chunk)
chunk_embed.set_footer(text=f"Page {i+2} • Requested by {ctx.author.id}")
await ctx.send(embed=chunk_embed)
chunk_embed.description = "\n".join(f"`{code}`: {lang}" for code, lang in chunk)
chunk_embed.set_footer(text=f"Requested by {ctx.author} • Page {i+2}/{len(chunks) + 1}")
embeds.append(chunk_embed)
# Send the embeds with button navigation
view = ButtonMenu(embeds)
await ctx.send(embed=embeds[0], view=view)
@app_commands.command(name="translate")
@app_commands.describe(