From c4261fc7f469112ddd3e11acf9f767b3e53d85b6 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Wed, 6 Jul 2022 21:05:04 -0400 Subject: [PATCH] Changed where submission views live. --- cogs/post_highscores/__init__.py | 13 +++++- cogs/submission/__init__.py | 26 +++++++++++ cogs/submission/submission_views.py | 70 +++++++++++++++++++++++++++++ highscores.py | 3 -- utilities/utils.py | 10 +++-- utilities/views.py | 20 --------- 6 files changed, 113 insertions(+), 29 deletions(-) create mode 100644 cogs/submission/__init__.py create mode 100644 cogs/submission/submission_views.py delete mode 100644 utilities/views.py diff --git a/cogs/post_highscores/__init__.py b/cogs/post_highscores/__init__.py index 8821f1a..91a87b5 100644 --- a/cogs/post_highscores/__init__.py +++ b/cogs/post_highscores/__init__.py @@ -3,6 +3,9 @@ Bot command to post the highscores """ from nextcord.ext import commands +import nextcord + +from cogs.submission.submission_views import SubmissionButton # pylint: disable=import-error from highscores import highscores_config # pylint: disable=import-error from highscores import highscores_data # pylint: disable=import-error from utilities.data_storage import save_highscores_data # pylint: disable=import-error @@ -15,6 +18,11 @@ class PostHighscores(commands.Cog, name="Post Highscores"): def __init__(self, bot: commands.Bot): self.bot = bot + @commands.Cog.listener() + async def on_ready(self): + """Called when cog is loaded""" + self.bot.add_view(SubmissionButton()) + @commands.command(name="post-scores") async def post_highscores(self, ctx: commands.Context): """ @@ -29,12 +37,13 @@ class PostHighscores(commands.Cog, name="Post Highscores"): print(f"Printing out highscores information in channel {highscore_channel_id}") channel = self.bot.get_channel(highscore_channel_id) - error_response = 'Registered Highscores channel does not exist or was never registered. \ + error_response = 'Registered Highscores channel does not exist or was never registered. \n\ Register with "?register" command.' assert channel is not None, await ctx.send(error_response) for boss in highscores_config["bosses"]: - await send_highscore_message(channel, boss["boss"]) + message: nextcord.Message = await send_highscore_message(channel, boss["boss"]) + await message.edit(view=SubmissionButton()) save_highscores_data(highscores_data) diff --git a/cogs/submission/__init__.py b/cogs/submission/__init__.py new file mode 100644 index 0000000..65adb93 --- /dev/null +++ b/cogs/submission/__init__.py @@ -0,0 +1,26 @@ +""" +Bot command to post the highscores +""" + +from nextcord.ext import commands + +from .submission_views import SubmissionButton + + +class SubmissionCog(commands.Cog, name="Submission Cog"): + """Posts Highscores.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.Cog.listener() + async def on_ready(self): + """Called when cog is loaded""" + self.bot.add_view(SubmissionButton()) + + +# This function will be called when this extension is loaded. +# It is necessary to add these functions to the bot. +def setup(bot: commands.Bot): + """Adds functon to bot""" + bot.add_cog(SubmissionCog(bot)) diff --git a/cogs/submission/submission_views.py b/cogs/submission/submission_views.py new file mode 100644 index 0000000..6a1714a --- /dev/null +++ b/cogs/submission/submission_views.py @@ -0,0 +1,70 @@ +"""Views used in highscores bot.""" + +from typing import Optional + +import nextcord + +from utilities.data_storage import open_message_map # pylint: disable=import-error +from highscores import highscores_data # pylint: disable=import-error + + +class SubmissionButton(nextcord.ui.View): + """Defines view for score submission button.""" + + def __init__(self): + super().__init__(timeout=None) + + @nextcord.ui.button(label="Submit", style=nextcord.ButtonStyle.green, custom_id="submit") + async def submit_button( + self, button: nextcord.ui.Button, interaction: nextcord.Interaction + ): # pylint: disable=unused-argument + """Button for score submission.""" + highscores_message_map = open_message_map() + boss_name = highscores_message_map[str(interaction.message.id)] + await interaction.channel.send(f"submission for {boss_name}") + # await interaction.channel.send( + # f"Submitting score for {boss_name}", + # view=SubmissionView(self._category_select_options(interaction)), + # ) + + async def _category_select_options( + self, interaction: nextcord.Interaction + ) -> "list[nextcord.SelectOption]": + highscores_message_map = open_message_map() + boss_name = highscores_message_map[str(interaction.message.id)] + options: list[nextcord.SelectOption] = [] # pylint: disable=unsubscriptable-object + boss_dict = highscores_data[boss_name] + for category in boss_dict["categories"].items(): + options.append(nextcord.SelectOption(label=category)) + + return options + + +class SubmissionDropdown(nextcord.ui.Select): + """Defines dropdown menu for submission""" + + def __init__(self, options: "list[nextcord.SelectOption]"): + super().__init__( + placeholder="Choose submission category.", min_values=1, max_values=1, options=options + ) + + def callback(self, interaction: nextcord.Interaction): + pass + + +class SubmissionView(nextcord.ui.View): + """Defines entire submission process view""" + + def __init__( + self, + # button_view: SubmissionButtonView.submit_button, + options: "list[nextcord.SelectOption]", + *, + timeout: Optional[float] = 180, + ): + super().__init__(timeout=timeout) + self.add_item(SubmissionDropdown(options)) + # self._button_view = button_view + + # async def interaction_check(self, interaction: nextcord.Interaction) -> bool: + # return self._button_view.interaction.user == interaction.user # unsure if works diff --git a/highscores.py b/highscores.py index 8c58e99..b424c36 100644 --- a/highscores.py +++ b/highscores.py @@ -12,7 +12,6 @@ import config from utilities.data_storage import open_highscores_config from utilities.data_storage import open_highscores_data from utilities.data_storage import open_message_map -from utilities.views import SubmissionButtonView logger = logging.getLogger("nextcord") logger.setLevel(logging.DEBUG) @@ -61,8 +60,6 @@ def main(): """When Discord is connected""" assert bot.user is not None print(f"{bot.user.name} has connected to Discord!") - # add submit button view - bot.add_view(SubmissionButtonView()) # Run Discord bot bot.run(config.DISCORD_TOKEN) diff --git a/utilities/utils.py b/utilities/utils.py index 931e086..b7999b2 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -39,7 +39,7 @@ def format_highscore_message(boss_name: str): return ret_string + "```" -async def send_highscore_message(channel, boss_name: str): +async def send_highscore_message(channel, boss_name: str) -> nextcord.Message: """ Function takes care of checking if a message exists for a boss, then creating and sending/editing the message @@ -50,17 +50,19 @@ async def send_highscore_message(channel, boss_name: str): message_id = highscores_data[boss_name]["message_id"] try: - message = await channel.fetch_message(message_id) - await message.edit(content=highscore_string, view=SubmissionView()) + message: nextcord.Message = await channel.fetch_message(message_id) + await message.edit(content=highscore_string) except nextcord.NotFound: # message doesn't exist. - message = await channel.send(highscore_string, view=SubmissionView()) + message: nextcord.Message = await channel.send(highscore_string) message_id = message.id highscores_data[boss_name]["message_id"] = message_id # save message -> boss_name in message_map highscores_message_map[str(message_id)] = boss_name save_message_map(highscores_message_map) + return message + async def submit_score( interaction: nextcord.Interaction, user: str, boss_name: str, category: str, score: str diff --git a/utilities/views.py b/utilities/views.py deleted file mode 100644 index 2db01d3..0000000 --- a/utilities/views.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Views used in highscores bot.""" - -import nextcord -from utilities.data_storage import open_message_map # pylint: disable=import-error - - -class SubmissionButtonView(nextcord.ui.View): - """Defines view for score submission.""" - - def __init__(self): - super().__init__(timeout=None) - - @nextcord.ui.button(label="Submit", style=nextcord.ButtonStyle.green, custom_id="submit") - async def submit_button( - self, button: nextcord.ui.Button, interaction: nextcord.Interaction - ): # pylint: disable=unused-argument - """Button for score submission.""" - highscores_message_map = open_message_map() - boss_name = highscores_message_map[str(interaction.message.id)] - await interaction.channel.send(f"Submitting score for {boss_name}")