WIP: Add TTS cog #18

Draft
cswimr wants to merge 14 commits from tts into main
7 changed files with 1275 additions and 51 deletions
Showing only changes of commit 3c4fba46b6 - Show all commits

1196
poetry.lock generated

File diff suppressed because it is too large Load diff

View file

@ -7,10 +7,11 @@ license = "MPL 2"
readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.9,<3.12"
python = ">=3.11,<3.12"
Red-DiscordBot = "^3.5.5"
pytimeparse2 = "^1.7.1"
humanize = "^4.8.0"
py-lav = {extras = ["all"], version = ">=1.14.3,<1.15"}
[tool.poetry.group.dev]
optional = true

View file

@ -1,5 +1,12 @@
from __future__ import annotations
from pylav.extension.red.utils.required_methods import pylav_auto_setup
from pylav.type_hints.bot import DISCORD_BOT_TYPE
from redbot.core.utils import get_end_user_data_statement
from .tts import TTS
__red_end_user_data_statement__ = get_end_user_data_statement(__file__)
async def setup(bot):
await bot.add_cog(TTS(bot))
async def setup(bot: DISCORD_BOT_TYPE):
await pylav_auto_setup(bot, TTS)

14
tts/config.py Normal file
View file

@ -0,0 +1,14 @@
from redbot.core import Config
config: Config = Config.get_conf(None, identifier=69737245070283, cog_name="TTS")
def register_config(config_obj: Config):
config_obj.register_global(
use_google_tts = False,
)
config_obj.register_guild(
enabled_channels = [],
announce = False,
voice_channels = True
)

View file

@ -8,7 +8,10 @@
"hidden": false,
"disabled": false,
"min_bot_version": "3.5.0",
"min_python_version": [3, 10, 0],
"min_python_version": [3, 11, 0],
"requirements": [
"Py-Lav[all]"
],
"tags": [
"pylav",
"audio",

68
tts/menu.py Normal file
View file

@ -0,0 +1,68 @@
from discord import ButtonStyle, Embed, Interaction, ui
from redbot.core import commands
from redbot.core.utils.chat_formatting import bold
from tts.config import config
class Menu(ui.View):
def __init__(self, ctx: commands.Context):
super().__init__()
self.ctx = ctx
@ui.button(label="Announce", style=ButtonStyle.green, row=0)
async def announce(self, interaction: Interaction, button: ui.Button):
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
return
await interaction.response.defer()
current_setting = await config.guild(interaction.guild).announce
await config.guild(interaction.guild).announce.set(not current_setting)
await interaction.message.edit(embed=await embed(self.ctx))
@ui.button(label="Use Voice Channels", style=ButtonStyle.green, row=0)
async def voice_channels(self, interaction: Interaction, button: ui.Button):
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
return
await interaction.response.defer()
current_setting = await config.guild(interaction.guild).voice_channels()
await config.guild(interaction.guild).voice_channels.set(not current_setting)
await interaction.message.edit(embed=await embed(self.ctx))
@ui.select(placeholder="Enabled Channels", cls=ui.ChannelSelect, row=1)
async def log_channel(self, interaction: Interaction, select: ui.ChannelSelect):
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
return
await interaction.response.defer()
channels: list = await config.guild(interaction.guild).enabled_channels()
if select.values[0] in channels:
channels.remove(select.values[0])
else:
channels.append(select.values[0])
await config.guild(interaction.guild).enabled_channels.set(channels)
await interaction.message.edit(embed=await embed(self.ctx))
async def embed(ctx: commands.Context):
embed = Embed(title="TTS Settings", color=ctx.embed_color)
override_settings = {
"announce": await config.guild(ctx.guild).announce(),
"voice_channels": await config.guild(ctx.guild).voice_channels(),
"enabled_channels": await config.guild(ctx.guild).enabled_channels(),
}
override_str = [
"- " + bold("Announce: ") + get_bool_emoji(override_settings["announce"]),
"- " + bold("Voice Channels: ") + get_bool_emoji(override_settings["voice_channels"]),
"- " + ", ".join([f"<#{channel}>" for channel in override_settings["voice_channels"]])
]
embed.description = "\n".join(override_str)
def get_bool_emoji(value: bool) -> str:
"""Returns a unicode emoji based on a boolean value."""
if value is True:
return "\N{WHITE HEAVY CHECK MARK}"
if value is False:
return "\N{NO ENTRY SIGN}"
return "\N{BLACK QUESTION MARK ORNAMENT}\N{VARIATION SELECTOR-16}"

View file

@ -8,9 +8,12 @@
import logging
from discord import Message
from redbot.core import Config, commands
from redbot.core import commands
from redbot.core.bot import Red
from tts.config import config
from tts.menu import Menu
class TTS(commands.Cog):
"""Text to Speech through Pylav"""
@ -18,15 +21,6 @@ class TTS(commands.Cog):
def __init__(self, bot: Red):
self.bot = bot
self.logger = logging.getLogger("red.sea.tts")
self.config = Config.get_conf(self, 69737245070283, force_registration=True)
self.config.register_global(
use_google_tts = False,
)
self.config.register_guild(
enabled_channels = [],
announce = False,
voice_channels = True
)
async def on_message(self, message: Message):
await self.bot.wait_until_red_ready()
@ -47,4 +41,17 @@ class TTS(commands.Cog):
#TODO - add PyLav integration
return
#TODO - add commands for enabling/disabling channels, setting voice channel settings, and setting global settings
@commands.group(name="tts", autohelp=True)
@commands.admin_or_permissions(manage_guild=True)
async def tts(self, ctx: commands.Context):
"""Text to Speech settings"""
await ctx.send(view=Menu(ctx))
@tts.command(name="google")
@commands.is_owner()
async def tts_google(self, ctx: commands.Context, enable: bool = None):
"""Enable or disable Google Cloud TTS"""
if enable is None:
enable = not await config.use_google_tts()
await config.use_google_tts.set(enable)
await ctx.send(f"Google TTS has been {'enabled' if enable else 'disabled'}.")