diff --git a/bot.py b/bot.py index 544d13c..bcbe943 100644 --- a/bot.py +++ b/bot.py @@ -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( @@ -976,9 +1050,9 @@ async def quote_remove_slash(ctx: SlashContext, author: str, quote: Optional[str await ctx.send("Either quote or quote_position must be used!", hidden=True) -# ____ ___ _ _ _____ ___ ____ +# ____ ___ _ _ _____ ___ ____ # / ___/ _ \| \ | | ___|_ _/ ___| -# | | | | | | \| | |_ | | | _ +# | | | | | | \| | |_ | | | _ # | |__| |_| | |\ | _| | | |_| | # \____\___/|_| \_|_| |___\____|