Get guild data everywhere highscores_data was used.

This commit is contained in:
alwayscompiling
2022-07-22 23:33:11 -04:00
parent d067da2300
commit be29194a57
6 changed files with 49 additions and 32 deletions
+9 -5
View File
@@ -23,14 +23,18 @@ class SubmissionListener(commands.Cog, name="Message Listener"):
@commands.Cog.listener()
async def on_message(self, message: nextcord.Message):
"""Listener for message creation. Adds score and attachments to submission."""
guild_id = message.guild.id
guild_data = highscores_data[str(guild_id)]
if message.author.id in submission_objects:
message_id = submission_objects[message.author.id]["message_id"]
submission_message: nextcord.Message = await self.bot.get_channel(
highscores_data["submission_channel_id"]
).fetch_message(message_id)
submission_message: nextcord.Message = (
self.bot.get_guild(guild_id)
.get_channel(guild_data["submission_channel_id"])
.fetch_message(message_id)
)
# need check if correct user and correct channel
if message.channel.id == highscores_data["submission_channel_id"]:
if message.channel.id == guild_data["submission_channel_id"]:
state = submission_objects[message.author.id]["submission_state"]
if state == SubmissionState.SCORE and message.content:
@@ -75,7 +79,7 @@ class SubmissionListener(commands.Cog, name="Message Listener"):
)
elif state == SubmissionState.NAME and message.content:
submission_objects[message.author.id]["username"] = message.content[
: highscores_data["username_length"]
: highscores_data[guild_id_str]["username_length"]
]
embed = get_submission_embed(message.author.id)
await submission_message.edit(
+2 -1
View File
@@ -29,7 +29,8 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
?post-scores
```
"""
highscore_channel_id = highscores_data["highscore_channel_id"]
guild_data = highscores_data[ctx.guild.id]
highscore_channel_id = guild_data["highscore_channel_id"]
print(f"Printing out highscores information in channel {highscore_channel_id}")
channel = ctx.guild.get_channel(highscore_channel_id)
+4 -3
View File
@@ -29,16 +29,17 @@ class RegisterVerificationChannel(commands.Cog, name="Register"):
"""
# respond to the message
channel_id = ctx.channel.id
guild_data = highscores_data[ctx.guild.id]
if registration_type == "verify":
await ctx.send(f"Registered <#{channel_id}> as channel to post verifications in.")
highscores_data["verification_channel_id"] = channel_id
guild_data["verification_channel_id"] = channel_id
elif registration_type == "highscore":
await ctx.send(f"Registered <#{channel_id}> as channel to post highscores in.")
highscores_data["highscore_channel_id"] = channel_id
guild_data["highscore_channel_id"] = channel_id
elif registration_type == "submission":
await ctx.send(f"Registered <#{channel_id}> as channel to post submissions in.")
highscores_data["submission_channel_id"] = channel_id
guild_data["submission_channel_id"] = channel_id
else:
embed = Embed(
title="Register command help", description="How to use the register command."
+5 -4
View File
@@ -29,11 +29,12 @@ class Score(commands.Cog, name="Score"):
This command still works if you prefer to use it over the ui.
```
"""
verification_channel_id = highscores_data["verification_channel_id"]
channel = self.bot.get_channel(verification_channel_id)
guild_data = highscores_data[ctx.guild.id]
verification_channel_id = guild_data["verification_channel_id"]
channel = ctx.guild.get_channel(verification_channel_id)
error_response = "Registered Verification channel does not exist or was never registered. \
Contact server admins."
error_response = "Registered Verification channel does not exist or was never registered."
error_response += "Contact server admins."
assert channel is not None, await ctx.send(error_response)
verification_string = f"{ctx.author.display_name}:{boss_name}:{category}:{score}"
+11 -8
View File
@@ -7,9 +7,10 @@ import nextcord
from nextcord import Embed
from nextcord.ext import commands
from highscores import highscores_config # pylint: disable=import-error
from highscores import highscores_data # pylint: disable=import-error
from highscores import submission_objects # pylint: disable=import-error
from highscores import highscores_message_map # pylint: disable=import-error
from highscores import submission_objects # pylint: disable=import-error
from .verification_view import VerificationView
@@ -55,9 +56,9 @@ def get_submission_embed(userid: int) -> nextcord.Embed:
def category_select_options(boss_name: str) -> "list[nextcord.SelectOption]":
"""Returns list of categories for a given boss."""
options: "list[nextcord.SelectOption]" = []
boss_dict = highscores_data["tables"][boss_name]
for category in boss_dict["categories"].items():
options.append(nextcord.SelectOption(label=category[0]))
category_list = highscores_config["tables"][boss_name]
for category in category_list:
options.append(nextcord.SelectOption(label=category))
return options
@@ -148,8 +149,8 @@ class SubmitButton(nextcord.ui.Button):
self._bot = bot
async def callback(self, interaction: nextcord.Interaction):
verification_channel_id = highscores_data["verification_channel_id"]
channel = self._bot.get_channel(verification_channel_id)
verification_channel_id = highscores_data[interaction.guild.id]["verification_channel_id"]
channel = interaction.guild.get_channel(verification_channel_id)
error_response = "Registered Verification channel does not exist or was never registered. \
Contact server admins."
@@ -186,8 +187,10 @@ class SubmissionCreateButton(nextcord.ui.View):
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
): # pylint: disable=unused-argument
"""Button for score submission."""
guild_data = highscores_data[interaction.guild.id]
boss_name = highscores_message_map[interaction.guild.id][interaction.message.id]
channel = self._bot.get_channel(highscores_data["submission_channel_id"])
channel = interaction.guild.get_channel(guild_data["submission_channel_id"])
# a user should only have 1 submission active at once.
# submitting with dummy message_id until message created
@@ -199,7 +202,7 @@ class SubmissionCreateButton(nextcord.ui.View):
"score": "",
"category": "",
"username": interaction.user.display_name.split("|")[0][
: highscores_data["username_length"]
: guild_data["username_length"]
],
}
+18 -11
View File
@@ -9,14 +9,15 @@ from highscores import highscores_message_map # pylint: disable=import-error
from utilities.data_storage import save_highscores_data # pylint: disable=import-error
def format_highscore_message(boss_name: str):
def format_highscore_message(boss_name: str, guild_data: dict):
"""
returns a formatted string for a boss's highscore
@param boss: the dictionary object for the boss
@param guild_data: the guild's highscore data
@return the formatted string
"""
ret_string = f"**{boss_name}**\n```"
boss_data = highscores_data["tables"][boss_name]
boss_data = guild_data["tables"][boss_name]
# Gather all categories and add to string
for key, value in boss_data["categories"].items():
@@ -32,7 +33,7 @@ def format_highscore_message(boss_name: str):
score_pair = value[i]
# pad username to max length.
username: str = score_pair[0].ljust(highscores_data["username_length"], " ")
username: str = score_pair[0].ljust(guild_data["username_length"], " ")
score: str = str(score_pair[1])
if highscores_config["categories"][key]["is_time_record"]:
@@ -49,16 +50,20 @@ def format_highscore_message(boss_name: str):
return ret_string + "```"
async def send_highscore_message(channel: nextcord.TextChannel, boss_name: str) -> nextcord.Message:
async def send_highscore_message(
channel: nextcord.TextChannel, boss_name: str, guild_data: dict
) -> nextcord.Message:
"""
Function takes care of checking if a message exists for a boss, then
creating and sending/editing the message
@param channel: the channel to send the message in/search for message id
@param boss: the boss to send the message about
@param guild_data: the guild's highscore data
@return: The nextcord message
"""
highscore_string = format_highscore_message(boss_name)
highscore_string = format_highscore_message(boss_name, guild_data)
message_id = highscores_data["tables"][boss_name]["message_id"]
message_id = guild_data["tables"][boss_name]["message_id"]
try:
message: nextcord.Message = await channel.fetch_message(message_id)
await message.edit(content=highscore_string)
@@ -66,7 +71,7 @@ async def send_highscore_message(channel: nextcord.TextChannel, boss_name: str)
# message doesn't exist.
message: nextcord.Message = await channel.send(highscore_string)
message_id = message.id
highscores_data["tables"][boss_name]["message_id"] = message_id
guild_data["tables"][boss_name]["message_id"] = message_id
# save message -> boss_name in message_map
# get guild id from channel
# channel should come from the guild that is requesting this highscores message anyways.
@@ -87,6 +92,7 @@ async def submit_score(
"""
boss_category_config = highscores_config["categories"][category]
guild_data = highscores_data[interaction.guild.id]
# create score tuple for either time or int
# define sort index
@@ -104,7 +110,7 @@ async def submit_score(
score_tuple = (user, int(score))
sort_index = 1
scores: "list[tuple]" = highscores_data["tables"][boss_name]["categories"][category]
scores: "list[tuple]" = guild_data["tables"][boss_name]["categories"][category]
# if user is in an existing tuple, delete it.
users_scores: "list[tuple]" = [score_tuple]
@@ -138,10 +144,10 @@ async def submit_score(
# enforce highscore size
while len(scores) > highscores_config["highscore_size"]:
scores.pop()
highscores_data["tables"][boss_name]["categories"][category] = scores
guild_data["tables"][boss_name]["categories"][category] = scores
# edit message
highscore_channel_id = highscores_data["highscore_channel_id"]
highscore_channel_id = guild_data["highscore_channel_id"]
print(f"Editting message {highscore_channel_id}")
channel = interaction.guild.get_channel(highscore_channel_id)
@@ -149,7 +155,8 @@ async def submit_score(
error_response += 'Register with "?register" command.'
assert channel is not None, await interaction.send(error_response, ephemeral=True)
await send_highscore_message(channel, boss_name)
await send_highscore_message(channel, boss_name, guild_data)
# save data
highscores_data[interaction.guild.id] = guild_data
save_highscores_data(highscores_data)