diff --git a/highscores.py b/highscores.py index b424c36..8c58e99 100644 --- a/highscores.py +++ b/highscores.py @@ -12,6 +12,7 @@ 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) @@ -60,6 +61,8 @@ 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 10e05c6..931e086 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -51,16 +51,15 @@ 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) + await message.edit(content=highscore_string, view=SubmissionView()) except nextcord.NotFound: # message doesn't exist. - message = await channel.send(highscore_string) + message = await channel.send(highscore_string, view=SubmissionView()) message_id = message.id highscores_data[boss_name]["message_id"] = message_id - - # save message -> boss_name in message_map - highscores_message_map[message_id] = boss_name - save_message_map(highscores_message_map) + # save message -> boss_name in message_map + highscores_message_map[str(message_id)] = boss_name + save_message_map(highscores_message_map) async def submit_score( diff --git a/utilities/views.py b/utilities/views.py new file mode 100644 index 0000000..2db01d3 --- /dev/null +++ b/utilities/views.py @@ -0,0 +1,20 @@ +"""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}")