Changed where submission views live.

This commit is contained in:
alwayscompiling
2022-07-06 21:05:04 -04:00
parent d0428afb3f
commit c4261fc7f4
6 changed files with 113 additions and 29 deletions
+11 -2
View File
@@ -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)
+26
View File
@@ -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))
+70
View File
@@ -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
-3
View File
@@ -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)
+6 -4
View File
@@ -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
-20
View File
@@ -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}")