mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 11:40:12 +01:00
refactor(tts): now works in dms and priv channels
This commit is contained in:
@@ -11,6 +11,23 @@ from gtts import gTTS
|
|||||||
|
|
||||||
DEFAULT_LANG = "en"
|
DEFAULT_LANG = "en"
|
||||||
|
|
||||||
|
async def send_error_message(context, description: str):
|
||||||
|
embed = discord.Embed(
|
||||||
|
title="Error",
|
||||||
|
description=description,
|
||||||
|
color=0xE02B2B,
|
||||||
|
)
|
||||||
|
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
|
||||||
|
|
||||||
|
interaction = getattr(context, "interaction", None)
|
||||||
|
if interaction is not None:
|
||||||
|
if not interaction.response.is_done():
|
||||||
|
await interaction.response.send_message(embed=embed, ephemeral=True)
|
||||||
|
else:
|
||||||
|
await interaction.followup.send(embed=embed, ephemeral=True)
|
||||||
|
else:
|
||||||
|
await context.send(embed=embed, ephemeral=True)
|
||||||
|
|
||||||
def tts_command():
|
def tts_command():
|
||||||
|
|
||||||
async def send_embed(
|
async def send_embed(
|
||||||
@@ -70,28 +87,19 @@ def tts_command():
|
|||||||
if isinstance(referenced, discord.Message) and referenced.content:
|
if isinstance(referenced, discord.Message) and referenced.content:
|
||||||
text = referenced.content
|
text = referenced.content
|
||||||
if not text or not text.strip():
|
if not text or not text.strip():
|
||||||
embed = (
|
await send_error_message(context, "Please provide text to convert or reply to a message containing text.")
|
||||||
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
|
return
|
||||||
|
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
if len(text) > 500:
|
if len(text) > 500:
|
||||||
embed = (
|
await send_error_message(context, "Text is too long. Please limit to 500 characters.")
|
||||||
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
|
return
|
||||||
|
|
||||||
|
interaction = getattr(context, "interaction", None)
|
||||||
|
if interaction is not None:
|
||||||
|
if not interaction.response.is_done():
|
||||||
|
await interaction.response.defer(ephemeral=False)
|
||||||
|
else:
|
||||||
processing_embed = (
|
processing_embed = (
|
||||||
discord.Embed(
|
discord.Embed(
|
||||||
title="TTS (Processing)",
|
title="TTS (Processing)",
|
||||||
@@ -99,41 +107,24 @@ def tts_command():
|
|||||||
color=0x7289DA,
|
color=0x7289DA,
|
||||||
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
|
).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)
|
processing_message = await context.send(embed=processing_embed)
|
||||||
|
|
||||||
audio_bytes, error = await generate_tts_audio(text)
|
audio_bytes, error = await generate_tts_audio(text)
|
||||||
|
|
||||||
if error or not audio_bytes:
|
if error or not audio_bytes:
|
||||||
embed = (
|
if interaction is not None:
|
||||||
discord.Embed(
|
await interaction.followup.send(embed=discord.Embed(
|
||||||
title="Error",
|
title="Error",
|
||||||
description=f"Failed to generate speech. {error or 'Unknown error.'}",
|
description=f"Failed to generate speech. {error or 'Unknown error.'}",
|
||||||
color=0xE02B2B,
|
color=0xE02B2B,
|
||||||
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
|
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"), ephemeral=True)
|
||||||
)
|
else:
|
||||||
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()
|
await processing_message.delete()
|
||||||
except Exception:
|
await context.send(embed=discord.Embed(
|
||||||
pass
|
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"), ephemeral=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
audio_file = discord.File(
|
audio_file = discord.File(
|
||||||
@@ -155,15 +146,9 @@ def tts_command():
|
|||||||
)
|
)
|
||||||
|
|
||||||
if interaction is not None:
|
if interaction is not None:
|
||||||
await context.channel.send(embed=embed)
|
await interaction.followup.send(embeds=[embed], files=[audio_file])
|
||||||
await context.channel.send(file=audio_file)
|
|
||||||
try:
|
|
||||||
await interaction.delete_original_response()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
await processing_message.delete()
|
await processing_message.delete()
|
||||||
await context.channel.send(embed=embed)
|
await context.send(embeds=[embed], files=[audio_file])
|
||||||
await context.channel.send(file=audio_file)
|
|
||||||
|
|
||||||
return tts
|
return tts
|
||||||
|
|||||||
Reference in New Issue
Block a user