Create buttons and enum for submission state navigation.

This commit is contained in:
alwayscompiling
2022-07-19 21:05:37 -04:00
parent 5155744ec5
commit 63db9fb7d4
3 changed files with 126 additions and 46 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ Bot command to post the highscores
from nextcord.ext import commands from nextcord.ext import commands
import nextcord import nextcord
from cogs.submission.submission_views import SubmissionButton # pylint: disable=import-error from cogs.submission.submission_views import SubmissionCreateButton # pylint: disable=import-error
from highscores import highscores_config # pylint: disable=import-error from highscores import highscores_config # pylint: disable=import-error
from highscores import highscores_data # 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 from utilities.data_storage import save_highscores_data # pylint: disable=import-error
@@ -41,7 +41,7 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
"highscore_table" "highscore_table"
].items(): ].items():
message: nextcord.Message = await send_highscore_message(channel, boss) message: nextcord.Message = await send_highscore_message(channel, boss)
await message.edit(view=SubmissionButton(self.bot)) await message.edit(view=SubmissionCreateButton(self.bot))
save_highscores_data(highscores_data) save_highscores_data(highscores_data)
+2 -2
View File
@@ -4,7 +4,7 @@ Cog for creating submission process.
from nextcord.ext import commands from nextcord.ext import commands
from .submission_views import SubmissionButton from .submission_views import SubmissionCreateButton
from .verification_view import VerificationView from .verification_view import VerificationView
@@ -17,7 +17,7 @@ class SubmissionCog(commands.Cog, name="Submission Cog"):
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
"""Called when cog is loaded""" """Called when cog is loaded"""
self.bot.add_view(SubmissionButton(self.bot)) self.bot.add_view(SubmissionCreateButton(self.bot))
self.bot.add_view(VerificationView()) self.bot.add_view(VerificationView())
+122 -42
View File
@@ -1,8 +1,10 @@
"""Views used for submission process.""" """Views used for submission process."""
from typing import Optional from typing import Optional
from enum import Enum
import nextcord import nextcord
from nextcord import Embed
from nextcord.ext import commands from nextcord.ext import commands
from utilities.data_storage import open_message_map # pylint: disable=import-error from utilities.data_storage import open_message_map # pylint: disable=import-error
@@ -11,37 +13,59 @@ from highscores import submission_messages # pylint: disable=import-error
from .verification_view import VerificationView from .verification_view import VerificationView
class SubmissionButton(nextcord.ui.View): class SubmissionState(Enum):
"""Enum for which state a submission is in"""
CATEGORY = 1
SCORE = 2
PROOF = 3
NAME = 4
SUBMIT = 5
def embedFromState(state: SubmissionState) -> nextcord.Embed:
embed = Embed(title="submission for thingy", description="a description i guess")
embed.add_field(name="State", value=state)
return embed
class SubmissionCreateButton(nextcord.ui.View):
"""Defines view for score submission button.""" """Defines view for score submission button."""
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
super().__init__(timeout=None) super().__init__(timeout=None)
self._bot = bot self._bot = bot
@nextcord.ui.button(label="Submit", style=nextcord.ButtonStyle.primary, custom_id="submit") @nextcord.ui.button(
async def submit_button( label="Submit", style=nextcord.ButtonStyle.primary, custom_id="submitcreate"
)
async def submitcreate_button(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction self, button: nextcord.ui.Button, interaction: nextcord.Interaction
): # pylint: disable=unused-argument ): # pylint: disable=unused-argument
"""Button for score submission.""" """Button for score submission."""
highscores_message_map = open_message_map() highscores_message_map = open_message_map()
boss_name = highscores_message_map[str(interaction.message.id)] boss_name = highscores_message_map[str(interaction.message.id)]
embed = nextcord.Embed(title="Highscores Submission") # embed = nextcord.Embed(title="Highscores Submission")
embed.add_field(name="Boss", value=boss_name, inline=False) # embed.add_field(name="Boss", value=boss_name, inline=False)
embed.add_field(name="User", value=interaction.user.display_name, inline=False) # embed.add_field(name="User", value=interaction.user.display_name, inline=False)
channel = self._bot.get_channel(highscores_data["submission_channel_id"]) channel = self._bot.get_channel(highscores_data["submission_channel_id"])
embed = embedFromState(SubmissionState.CATEGORY)
submission_message = await channel.send( submission_message = await channel.send(
f"<@{interaction.user.id}>", f"<@{interaction.user.id}>",
view=SubmissionDropdownView( view=SubmissionView(
self._bot, interaction, await self._category_select_options(boss_name) self._bot, interaction, await self._category_select_options(boss_name)
), ),
embed=embed, embed=embed,
) )
# a user should only have 1 submission active at once. # a user should only have 1 submission active at once.
submission_messages[interaction.user.id] = submission_message.id submission_messages[interaction.user.id] = {
"message_id": submission_message.id,
"submission_state": SubmissionState.CATEGORY,
}
async def _category_select_options(self, boss_name: str) -> "list[nextcord.SelectOption]": async def _category_select_options(self, boss_name: str) -> "list[nextcord.SelectOption]":
options: list[nextcord.SelectOption] = [] # pylint: disable=unsubscriptable-object options: "list[nextcord.SelectOption]" = []
boss_dict = highscores_data[boss_name] boss_dict = highscores_data[boss_name]
for category in boss_dict["categories"].items(): for category in boss_dict["categories"].items():
options.append(nextcord.SelectOption(label=category[0])) options.append(nextcord.SelectOption(label=category[0]))
@@ -70,53 +94,109 @@ class SubmissionDropdown(nextcord.ui.Select):
) )
class SubmissionDropdownView(nextcord.ui.View): class SubmissionView(nextcord.ui.View):
"""Defines submission dropdown process view""" """Defines submission dropdown process view"""
def __init__( def __init__(
self, self,
bot: commands.Bot, bot: commands.Bot,
prev_interaction: nextcord.Interaction, creation_interaction: nextcord.Interaction,
options: "list[nextcord.SelectOption]", options: "list[nextcord.SelectOption]",
*, *,
timeout: Optional[float] = 180, timeout: Optional[float] = 180,
): ):
super().__init__(timeout=timeout) super().__init__(timeout=timeout)
self.add_item(SubmissionDropdown(bot, options)) # self.add_item(SubmissionDropdown(bot, options))
self._prev_interaction = prev_interaction self.add_item(CancelButton(bot))
self.add_item(NextButton(bot))
self._creation_interaction = creation_interaction
self.bot = bot self.bot = bot
async def interaction_check(self, interaction: nextcord.Interaction) -> bool: async def interaction_check(self, interaction: nextcord.Interaction) -> bool:
return self._prev_interaction.user == interaction.user return self._creation_interaction.user == interaction.user
@nextcord.ui.button( # @nextcord.ui.button(
label="Submit Score", style=nextcord.ButtonStyle.green, custom_id="submit-score" # label="Submit Score", style=nextcord.ButtonStyle.green, custom_id="submit-score"
) # )
async def submit_button( # async def submit_button(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction # self, button: nextcord.ui.Button, interaction: nextcord.Interaction
): # pylint: disable=unused-argument # ): # pylint: disable=unused-argument
"""Defines button to submit a user's score.""" # """Defines button to submit a user's score."""
verification_channel_id = highscores_data["verification_channel_id"] # verification_channel_id = highscores_data["verification_channel_id"]
channel = self.bot.get_channel(verification_channel_id) # channel = self.bot.get_channel(verification_channel_id)
error_response = "Registered Verification channel does not exist or was never registered. \ # error_response = "Registered Verification channel does not exist or was never registered. \
Contact server admins." # Contact server admins."
assert channel is not None, await channel.send(error_response) # assert channel is not None, await channel.send(error_response)
await channel.send( # await channel.send(
files=[await attch.to_file() for attch in interaction.message.attachments], # files=[await attch.to_file() for attch in interaction.message.attachments],
view=VerificationView(), # view=VerificationView(),
embed=interaction.message.embeds[0], # embed=interaction.message.embeds[0],
) # )
await interaction.message.edit(content=f"<@{interaction.user.id}> Submitted!", view=None) # await interaction.message.edit(content=f"<@{interaction.user.id}> Submitted!", view=None)
submission_messages.pop(interaction.user.id) # submission_messages.pop(interaction.user.id)
@nextcord.ui.button( # @nextcord.ui.button(
label="Cancel", style=nextcord.ButtonStyle.red, custom_id="cancel-submission" # label="Cancel", style=nextcord.ButtonStyle.red, custom_id="cancel-submission"
) # )
async def cancel_button( # async def cancel_button(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction # self, button: nextcord.ui.Button, interaction: nextcord.Interaction
): # pylint: disable=unused-argument # ): # pylint: disable=unused-argument
"""Deletes message to cancel interaction.""" # """Deletes message to cancel interaction."""
submission_messages.pop(interaction.user.id) # submission_messages.pop(interaction.user.id)
await interaction.message.delete() # await interaction.message.delete()
# class BackButton(nextcord.ui.View):
# """Defines view for back button."""
# def __init__(self, bot: commands.Bot):
# super().__init__(timeout=None)
# self._bot = bot
# @nextcord.ui.button(label="Back", style=nextcord.ButtonStyle.primary, custom_id="back")
# async def back_button(
# self, button: nextcord.ui.Button, interaction: nextcord.Interaction
# ): # pylint: disable=unused-argument
# pass
class CancelButton(nextcord.ui.Button):
"""Defines view for cancel button."""
def __init__(self, bot: commands.Bot):
super().__init__(label="Cancel", style=nextcord.ButtonStyle.red, custom_id="cancel")
self._bot = bot
async def callback(self, interaction: nextcord.Interaction): # pylint: disable=unused-argument
pass
class NextButton(nextcord.ui.Button):
"""Defines view for next button."""
def __init__(self, bot: commands.Bot):
super().__init__(label="Next", style=nextcord.ButtonStyle.primary, custom_id="next")
self._bot = bot
async def callback(self, interaction: nextcord.Interaction): # pylint: disable=unused-argument
state: SubmissionState = submission_messages[interaction.user.id]["submission_state"]
state = SubmissionState(state.value + 1)
submission_messages[interaction.user.id]["submission_state"] = state
embed = embedFromState(state)
await interaction.edit(embed=embed)
# class SubmitButton(nextcord.ui.View):
# """Defines view for submit button."""
# def __init__(self, bot: commands.Bot):
# super().__init__(timeout=None)
# self._bot = bot
# @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
# pass