feat(tts): new command

Co-authored-by: neoarz <tyrantneo740@gmail.com>
This commit is contained in:
se2crid
2025-10-15 06:35:19 +02:00
committed by GitHub
parent 14cb324e0e
commit 6a43c93637
5 changed files with 193 additions and 6 deletions

View File

@@ -18,7 +18,7 @@
## Commands
![Total Commands](https://img.shields.io/badge/Total%20Commands-64-5865F2)
![Total Commands](https://img.shields.io/badge/Total%20Commands-65-5865F2)
| Command&nbsp;group | Subcommands |
| ------------ | --- |
@@ -32,7 +32,7 @@
| melonx | `help`, `transfer`, `mods`, `gamecrash`, `requirements`, `error`, `26` |
| miscellaneous | `keanu`, `labubu`, `piracy`, `tryitandsee`, `rickroll`, `dontasktoask`, `support`, `depart`, `docs` `sigma` |
| utilities | `translate`, `codepreview`, `dictionary` |
| media | `download`, `mcquote`, `img2gif`, `tweety` |
| media | `download`, `mcquote`, `img2gif`, `tweety`, `tts` |
## Download

View File

@@ -51,7 +51,7 @@
- [ ] add ~~admin abuse~~ command (for games)
- [ ] add [tts](https://developer.puter.com/tutorials/free-unlimited-text-to-speech-api/) command
- [x] add [tts](https://developer.puter.com/tutorials/free-unlimited-text-to-speech-api/) command
- [x] update botinfo command

View File

@@ -7,6 +7,7 @@ from .download import download_command
from .mcquote import mcquote_command
from .img2gif import img2gif_command
from .tweety import tweety_command
from .tts import tts_command
def _require_group_prefix(context: Context) -> bool:
@@ -46,7 +47,7 @@ class Media(commands.GroupCog, name="media"):
color=0x7289DA
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.add_field(name="Available", value="download, mcquote, img2gif, tweety", inline=False)
embed.add_field(name="Available", value="download, mcquote, img2gif, tweety, tts", inline=False)
await context.send(embed=embed)
async def _invoke_hybrid(self, context: Context, name: str, *args, **kwargs):
@@ -62,6 +63,9 @@ class Media(commands.GroupCog, name="media"):
if name == "tweety":
await self.tweety(context)
return
if name == "tts":
await self.tts(context, text=kwargs.get('text'))
return
await context.send(f"Unknown media command: {name}")
@media_group.command(name="download")
@@ -80,6 +84,10 @@ class Media(commands.GroupCog, name="media"):
async def media_group_tweety(self, context: Context):
await self._invoke_hybrid(context, "tweety")
@media_group.command(name="tts")
async def media_group_tts(self, context: Context, *, text: str = None):
await self._invoke_hybrid(context, "tts", text=text)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="download",
@@ -112,11 +120,20 @@ class Media(commands.GroupCog, name="media"):
async def tweety(self, context):
return await tweety_command()(self, context)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="tts",
description="Convert text to speech using Google Text-to-Speech.",
)
async def tts(self, context, text: str = None):
return await tts_command()(context, text=text)
async def setup(bot) -> None:
cog = Media(bot)
await bot.add_cog(cog)
bot.logger.info("Loaded extension 'media.download'")
bot.logger.info("Loaded extension 'media.mcquote'")
bot.logger.info("Loaded extension 'media.img2gif'")
bot.logger.info("Loaded extension 'media.tweety'")
bot.logger.info("Loaded extension 'media.tts'")

169
cogs/media/tts.py Normal file
View File

@@ -0,0 +1,169 @@
import asyncio
import io
import tempfile
from typing import Optional
import discord
from discord import app_commands
from discord.ext import commands
from gtts import gTTS
DEFAULT_LANG = "en"
def tts_command():
async def send_embed(
context: commands.Context,
embed: discord.Embed,
*,
ephemeral: bool = False,
file: Optional[discord.File] = None,
) -> None:
interaction = getattr(context, "interaction", None)
if interaction is not None:
if interaction.response.is_done():
if file:
await interaction.followup.send(embed=embed, file=file, ephemeral=ephemeral)
else:
await interaction.followup.send(embed=embed, ephemeral=ephemeral)
else:
if file:
await interaction.response.send_message(embed=embed, file=file, ephemeral=ephemeral)
else:
await interaction.response.send_message(embed=embed, ephemeral=ephemeral)
else:
if file:
await context.send(embed=embed, file=file)
else:
await context.send(embed=embed)
async def generate_tts_audio(text: str) -> tuple[Optional[bytes], Optional[str]]:
try:
loop = asyncio.get_event_loop()
audio_bytes = await loop.run_in_executor(
None,
lambda: _generate_tts_sync(text)
)
return audio_bytes, None
except Exception as e:
return None, str(e)
def _generate_tts_sync(text: str) -> bytes:
tts = gTTS(text=text, lang=DEFAULT_LANG, slow=False)
fp = io.BytesIO()
tts.write_to_fp(fp)
fp.seek(0)
return fp.read()
@commands.hybrid_command(
name="tts",
description="Convert text to speech using Google Text-to-Speech.",
)
@app_commands.describe(
text="The text to convert to speech",
)
async def tts(context: commands.Context, text: Optional[str] = None):
if not text or not text.strip():
if context.message and context.message.reference and context.message.reference.resolved:
referenced = context.message.reference.resolved
if isinstance(referenced, discord.Message) and referenced.content:
text = referenced.content
if not text or not text.strip():
embed = (
discord.Embed(
title="Error",
description="Please provide text to convert or reply to a message containing text.",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
)
await send_embed(context, embed, ephemeral=True)
return
text = text.strip()
if len(text) > 500:
embed = (
discord.Embed(
title="Error",
description="Text is too long. Please limit to 500 characters.",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
)
await send_embed(context, embed, ephemeral=True)
return
processing_embed = (
discord.Embed(
title="TTS (Processing)",
description="<a:mariospin:1423677027013103709> Generating speech...",
color=0x7289DA,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
)
interaction = getattr(context, "interaction", None)
processing_message = None
sent_initial_interaction_response = False
if interaction is not None:
if interaction.response.is_done():
processing_message = await interaction.followup.send(embed=processing_embed, ephemeral=True)
else:
await interaction.response.send_message(embed=processing_embed, ephemeral=True)
sent_initial_interaction_response = True
else:
processing_message = await context.send(embed=processing_embed)
audio_bytes, error = await generate_tts_audio(text)
if error or not audio_bytes:
embed = (
discord.Embed(
title="Error",
description=f"Failed to generate speech. {error or 'Unknown error.'}",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
)
await send_embed(context, embed, ephemeral=True)
if interaction is not None and sent_initial_interaction_response:
try:
await interaction.delete_original_response()
except Exception:
pass
if processing_message:
try:
await processing_message.delete()
except Exception:
pass
return
audio_file = discord.File(
io.BytesIO(audio_bytes),
filename="audio.mp3",
)
embed = (
discord.Embed(
title="Text-to-Speech",
description=f"**Input:** {text}",
color=0x7289DA,
)
.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
.set_footer(
text=f"Requested by {context.author.display_name}",
icon_url=getattr(context.author.display_avatar, "url", None),
)
)
if interaction is not None:
await context.channel.send(embed=embed)
await context.channel.send(file=audio_file)
try:
await interaction.delete_original_response()
except:
pass
else:
await processing_message.delete()
await context.channel.send(embed=embed)
await context.channel.send(file=audio_file)
return tts

View File

@@ -5,4 +5,5 @@ python-dotenv
yt-dlp
Pillow
pillow-heif
pytz
pytz
gTTS