Add quote tell slash

This commit is contained in:
2021-10-07 14:01:00 +02:00
parent 3335b30879
commit d89b4fd596

96
bot.py
View File

@@ -749,6 +749,41 @@ def get_quotes(guild_id: int) -> Dict[str, List[str]]:
return quotes
def get_quotes_rolling_index(guild_id: int, author: str) -> Optional[int]:
quotes = get_quotes(guild_id)
if author not in quotes:
return None
quotes = quotes[author]
rolling_indeces = config_get("guild_quotes_rolling_indeces", guild=guild_id)
if rolling_indeces is None:
rolling_indeces = {}
if author not in rolling_indeces:
rolling_indeces[author] = 0
index = rolling_indeces[author]
rolling_indeces[author] = index + 1
config_set_raw("guild_quotes_rolling_indeces", guild=guild_id)
return index
async def _do_quote(ctx: SlashContext, author: str, quote: str, tts: bool):
text = '\n'.join(map(lambda line: '> ' + line, quote.splitlines()))
await ctx.send(text + '\n *~' + author + '*')
if tts:
voice: discord.VoiceState = ctx.author.voice
if voice.channel is not None:
source, tts_destroyer = text_to_speech(author + " sagte: " + quote)
vp: discord.VoiceClient = await connect_and_play(voice.channel, source)
while vp.is_playing() and vp.channel == voice.channel:
await asyncio.sleep(0.5)
await vp.disconnect()
tts_destroyer()
@slash.subcommand(
base="quotes",
name="random",
@@ -789,20 +824,59 @@ async def quote_random_slash(ctx: SlashContext, author: Optional[str] = None, tt
author_quotes = quotes[author]
quote = random.choice(author_quotes)
text = '\n'.join(map(lambda line: '> ' + line, quote.splitlines()))
await ctx.send(text + '\n *~' + author + '*')
await _do_quote(ctx, author, quote, tts)
if tts:
voice: discord.VoiceState = ctx.author.voice
if voice.channel is not None:
source, tts_destroyer = text_to_speech(author + " sagte: " + quote)
vp: discord.VoiceClient = await connect_and_play(voice.channel, source)
while vp.is_playing() and vp.channel == voice.channel:
await asyncio.sleep(0.5)
await vp.disconnect()
tts_destroyer()
@slash.subcommand(
base="quotes",
name="tell",
description="Recite a quote",
options=[
create_option(
name="author",
description="The author to recite from",
option_type=str,
required=True
),
create_option(
name="prefix",
description="Get a quote by prefix",
option_type=str,
required=False
),
create_option(
name="index",
description="Get a quote with a certain index",
option_type=SlashCommandOptionType.INTEGER,
required=False
),
create_option(
name="tts",
description="Text to speech to voice chat",
option_type=SlashCommandOptionType.BOOLEAN,
required=False
)
]
)
async def quote_tell_slash(ctx: SlashContext, author: str, prefix: Optional[str] = None, index: Optional[int] = None,
tts: Optional[bool] = None):
if not await check_slash_context(ctx, False):
return
quotes = get_quotes(ctx.guild_id)[author]
if index is not None:
await _do_quote(ctx, author, quotes[index], tts)
return
if prefix is not None:
for quote in quotes:
if quote.startswith(prefix):
await _do_quote(ctx, author, quote, tts)
return
index = get_quotes_rolling_index(ctx.guild_id, author)
if index is not None:
await _do_quote(ctx, author, quotes[index], tts)
@slash.subcommand(