From f5319913acc3e1b65522bd505d778e9a9c830428 Mon Sep 17 00:00:00 2001 From: qwewqa <198e559dbd446d973355f415bdfa34@gmail.com> Date: Sat, 16 Jan 2021 16:55:13 -0500 Subject: [PATCH] add pages to event command --- miyu_bot/commands/cogs/event.py | 19 ++++++++++-- .../commands/common/master_asset_manager.py | 8 +++-- miyu_bot/commands/common/reaction_message.py | 30 ++++++++++++++++--- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/miyu_bot/commands/cogs/event.py b/miyu_bot/commands/cogs/event.py index 894b4a5..edde5cb 100644 --- a/miyu_bot/commands/cogs/event.py +++ b/miyu_bot/commands/cogs/event.py @@ -17,7 +17,7 @@ from miyu_bot.commands.common.emoji import attribute_emoji_ids_by_attribute_id, from miyu_bot.commands.common.formatting import format_info from miyu_bot.commands.common.fuzzy_matching import romanize from miyu_bot.commands.common.master_asset_manager import MasterFilter, hash_master -from miyu_bot.commands.common.reaction_message import run_paged_message +from miyu_bot.commands.common.reaction_message import run_paged_message, run_dynamically_paged_message class Event(commands.Cog): @@ -53,11 +53,24 @@ class Event(commands.Cog): return self.logger.info(f'Found event "{event}" ({romanize(event.name)}).') + current_id = event.id + + def generator(n): + nonlocal current_id + new_event = masters.events.get(current_id + n, ctx) + if new_event: + current_id = new_event.id + return self.get_event_embed(new_event) + + asyncio.ensure_future(run_dynamically_paged_message(ctx, generator)) + + def get_event_embed(self, event): embed = discord.Embed(title=event.name) event_hash = hash_master(event) event_logo_path = event.logo_path - embed.set_thumbnail(url=f'https://qwewqa.github.io/d4dj-dumps/events/logos/{event_logo_path.stem}_{event_hash}{event_logo_path.suffix}') + embed.set_thumbnail( + url=f'https://qwewqa.github.io/d4dj-dumps/events/logos/{event_logo_path.stem}_{event_hash}{event_logo_path.suffix}') duration_hour_part = round((event.duration.seconds / 3600), 2) duration_hour_part = duration_hour_part if not duration_hour_part.is_integer() else int(duration_hour_part) @@ -106,7 +119,7 @@ class Event(commands.Cog): inline=True) embed.set_footer(text=f'Event Id: {event.id}') - await ctx.send(embed=embed) + return embed @commands.command(name='time', aliases=[], diff --git a/miyu_bot/commands/common/master_asset_manager.py b/miyu_bot/commands/common/master_asset_manager.py index 427076b..ca0c396 100644 --- a/miyu_bot/commands/common/master_asset_manager.py +++ b/miyu_bot/commands/common/master_asset_manager.py @@ -1,7 +1,7 @@ import hashlib from functools import lru_cache from timeit import default_timer -from typing import Callable, Any, Optional +from typing import Callable, Any, Optional, Union from d4dj_utils.manager.asset_manager import AssetManager from d4dj_utils.master.master_asset import MasterDict, MasterAsset @@ -61,11 +61,13 @@ class MasterFilter: self.default_filter.set_unprocessed(alias, master) self.unrestricted_filter.set_unprocessed(alias, master) - def get(self, name_or_id: str, ctx: Optional[commands.Context]): + def get(self, name_or_id: Union[str, int], ctx: Optional[commands.Context]): if ctx and ctx.channel.id in no_filter_channels: try: return self.masters[int(name_or_id)] except (KeyError, ValueError): + if isinstance(name_or_id, int): + return None return self.unrestricted_filter[name_or_id] else: try: @@ -74,6 +76,8 @@ class MasterFilter: master = self.default_filter[name_or_id] return master except (KeyError, ValueError): + if isinstance(name_or_id, int): + return None return self.default_filter[name_or_id] def get_sorted(self, name: str, ctx: commands.Context): diff --git a/miyu_bot/commands/common/reaction_message.py b/miyu_bot/commands/common/reaction_message.py index 37cb4d1..f6ee498 100644 --- a/miyu_bot/commands/common/reaction_message.py +++ b/miyu_bot/commands/common/reaction_message.py @@ -8,7 +8,8 @@ from discord.ext.commands import Context AnyEmoji = Union[str, Emoji] -async def run_tabbed_message(ctx: Context, emojis: List[AnyEmoji], embeds: List[Embed], files=None, starting_index=0, timeout=300): +async def run_tabbed_message(ctx: Context, emojis: List[AnyEmoji], embeds: List[Embed], files=None, starting_index=0, + timeout=300): if not files: files = [] if len(emojis) != len(embeds): @@ -22,6 +23,27 @@ async def run_tabbed_message(ctx: Context, emojis: List[AnyEmoji], embeds: List[ await run_reaction_message(ctx, message, emojis, callback, timeout) +async def run_dynamically_paged_message(ctx: Context, embed_generator: Callable[[int], discord.Embed], timeout=300): + left_arrow = '◀' + right_arrow = '▶' + arrows = [left_arrow, right_arrow] + + message = await ctx.send(embed=embed_generator(0)) + + async def callback(emoji, _ctx, _message): + if emoji == left_arrow: + new_embed = embed_generator(-1) + elif emoji == right_arrow: + new_embed = embed_generator(1) + else: + return + + if new_embed: + await message.edit(embed=new_embed) + + await run_reaction_message(ctx, message, arrows, callback, timeout) + + async def run_paged_message(ctx: Context, base_embed: discord.Embed, content: List[str], page_size: int = 15, header='', numbered: bool = True, timeout=300, max_tabbed_pages=4, files=None): if header: @@ -55,15 +77,15 @@ async def run_paged_message(ctx: Context, base_embed: discord.Embed, content: Li }).set_footer(text=f'Page {i + 1}/{len(page_contents)}') for i, page in enumerate(page_contents)] - message = await ctx.send(embed=embeds[0], files=files or []) - if len(embeds) == 1: return if len(embeds) <= max_tabbed_pages: reaction_emoji = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣'] - await run_tabbed_message(ctx, message, reaction_emoji[:len(embeds)], embeds, timeout=timeout) + await run_tabbed_message(ctx, reaction_emoji[:len(embeds)], embeds, timeout=timeout) else: + message = await ctx.send(embed=embeds[0], files=files or []) + double_left_arrow = '⏪' double_right_arrow = '⏩' left_arrow = '◀'