Adding submission button for all highscore messages.

This commit is contained in:
alwayscompiling
2022-07-04 10:13:16 -04:00
parent 84cf3781ee
commit 22d368f0a8
3 changed files with 28 additions and 6 deletions
+3
View File
@@ -12,6 +12,7 @@ import config
from utilities.data_storage import open_highscores_config from utilities.data_storage import open_highscores_config
from utilities.data_storage import open_highscores_data from utilities.data_storage import open_highscores_data
from utilities.data_storage import open_message_map from utilities.data_storage import open_message_map
from utilities.views import SubmissionButtonView
logger = logging.getLogger("nextcord") logger = logging.getLogger("nextcord")
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
@@ -60,6 +61,8 @@ def main():
"""When Discord is connected""" """When Discord is connected"""
assert bot.user is not None assert bot.user is not None
print(f"{bot.user.name} has connected to Discord!") print(f"{bot.user.name} has connected to Discord!")
# add submit button view
bot.add_view(SubmissionButtonView())
# Run Discord bot # Run Discord bot
bot.run(config.DISCORD_TOKEN) bot.run(config.DISCORD_TOKEN)
+5 -6
View File
@@ -51,16 +51,15 @@ async def send_highscore_message(channel, boss_name: str):
message_id = highscores_data[boss_name]["message_id"] message_id = highscores_data[boss_name]["message_id"]
try: try:
message = await channel.fetch_message(message_id) message = await channel.fetch_message(message_id)
await message.edit(content=highscore_string) await message.edit(content=highscore_string, view=SubmissionView())
except nextcord.NotFound: except nextcord.NotFound:
# message doesn't exist. # message doesn't exist.
message = await channel.send(highscore_string) message = await channel.send(highscore_string, view=SubmissionView())
message_id = message.id message_id = message.id
highscores_data[boss_name]["message_id"] = message_id highscores_data[boss_name]["message_id"] = message_id
# save message -> boss_name in message_map
# save message -> boss_name in message_map highscores_message_map[str(message_id)] = boss_name
highscores_message_map[message_id] = boss_name save_message_map(highscores_message_map)
save_message_map(highscores_message_map)
async def submit_score( async def submit_score(
+20
View File
@@ -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}")