From 0834634cde21354c0d5f3e8aad8791493f2e6bb6 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Sun, 3 Jul 2022 17:11:06 -0400 Subject: [PATCH] Verification button working. --- cogs/score/__init__.py | 12 ++++++++---- cogs/score/verification_view.py | 28 ++++++++++++++++++++++++++++ utilities/utils.py | 18 ++++++++++-------- 3 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 cogs/score/verification_view.py diff --git a/cogs/score/__init__.py b/cogs/score/__init__.py index 7b12180..48750e2 100644 --- a/cogs/score/__init__.py +++ b/cogs/score/__init__.py @@ -2,10 +2,10 @@ Bot command to submit a score """ -from ast import AsyncFunctionDef from nextcord.ext import commands -from utils import highscore_manage # pylint: disable=import-error from highscores import highscores_data # pylint: disable=import-error +from utilities.utils import submit_score # pylint: disable=import-error +from .verification_view import VerificationView class Score(commands.Cog, name="Score"): @@ -14,6 +14,11 @@ class Score(commands.Cog, name="Score"): def __init__(self, bot: commands.Bot): self.bot = bot + @commands.Cog.listener() + async def on_ready(self): + """Called when the cog is loaded""" + self.bot.add_view(VerificationView()) + @commands.command(name="score") async def score(self, ctx: commands.Context, boss_name: str, category: str, score: str): """A command which calls a function to submit the score to highscores. @@ -35,10 +40,9 @@ class Score(commands.Cog, name="Score"): await channel.send( content=verification_string, files=[await attch.to_file() for attch in ctx.message.attachments], + view=VerificationView(), ) - # await highscore_manage.submit_score(self, ctx, boss_name, category, score) - # This function will be called when this extension is loaded. # It is necessary to add these functions to the bot. diff --git a/cogs/score/verification_view.py b/cogs/score/verification_view.py new file mode 100644 index 0000000..dc0a010 --- /dev/null +++ b/cogs/score/verification_view.py @@ -0,0 +1,28 @@ +"""View for verification buttons""" + +import nextcord +from utilities.utils import submit_score # pylint: disable=import-error + + +class VerificationView(nextcord.ui.View): + """Defines views for verifying score submissions""" + + def __init__(self): + super().__init__(timeout=None) + + @nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green, custom_id="approve") + async def approve_button(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): + """Button for approving score submission.""" + await interaction.response.send_message("Approved") + content = interaction.message.content.split(":") + user = content[0] + boss_name = content[1] + category = content[2] + score = content[3] + + await submit_score(interaction, user, boss_name, category, score) + + @nextcord.ui.button(label="Deny", style=nextcord.ButtonStyle.red, custom_id="deny") + async def deny_button(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): + """Button for denying score submission.""" + await interaction.response.send_message("Denied") diff --git a/utilities/utils.py b/utilities/utils.py index 2d59d32..1475a94 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -58,7 +58,9 @@ async def send_highscore_message(channel, boss_name: str): highscores_data[boss_name]["message_id"] = message_id -async def submit_score(self, ctx: commands.Context, boss_name: str, category: str, score: str): +async def submit_score( + interaction: nextcord.Interaction, user: str, boss_name: str, category: str, score: str +): """ Adds a score to the high scores and sorts. Removes a score if larger than size limit. @param boss: the boss to add score to @@ -75,11 +77,11 @@ async def submit_score(self, ctx: commands.Context, boss_name: str, category: st minutes = score_list[0] seconds = score_list[1] score_seconds = int(minutes) * 60 + int(seconds) - score_tuple = (ctx.author.display_name, score, score_seconds) + score_tuple = (user, score, score_seconds) sort_index = 2 else: - score_tuple = (ctx.author.display_name, int(score)) + score_tuple = (user, int(score)) sort_index = 1 scores = highscores_data[boss_name]["categories"][category] @@ -97,12 +99,12 @@ async def submit_score(self, ctx: commands.Context, boss_name: str, category: st # edit message highscore_channel_id = highscores_data["highscore_channel_id"] print(f"Editting message {highscore_channel_id}") - channel = self.bot.get_channel(highscore_channel_id) - if channel is None: - response = 'Registered Highscores channel does not exist or was never registered. \ + channel = interaction.guild.get_channel(highscore_channel_id) + + error_response = 'Registered Highscores channel does not exist or was never registered. \ Register with "?register" command.' - await ctx.send(response) - return + assert channel is not None, await interaction.send(error_response) + await send_highscore_message(channel, boss_name) # save data