Turned on all ruff rules. Formatted all files.

This commit is contained in:
2024-05-22 15:06:39 -04:00
parent 3ac164ab65
commit 796b052bfc
18 changed files with 161 additions and 469 deletions
+12 -37
View File
@@ -32,9 +32,7 @@ class RecordCog(commands.Cog, name="Record Commands"):
async def event(
self,
interaction: nextcord.Interaction,
name: str = nextcord.SlashOption(
required=True, description="Name of the event that was hosted."
),
name: str = nextcord.SlashOption(required=True, description="Name of the event that was hosted."),
host: nextcord.Member = nextcord.SlashOption(
required=False,
description="An alternative host if you are recording event for someone else.",
@@ -51,10 +49,7 @@ class RecordCog(commands.Cog, name="Record Commands"):
"""
# check role
if (
interaction.guild.get_role(self.bot.config.params.event_host_role_id)
not in interaction.user.roles
):
if interaction.guild.get_role(self.bot.config.params.event_host_role_id) not in interaction.user.roles:
await interaction.response.send_message(
f"You must have the <@&{self.bot.config.params.event_host_role_id}> role to record an event.",
ephemeral=True,
@@ -95,15 +90,11 @@ class EventView(nextcord.ui.View):
self.add_item(self.event_participants)
@nextcord.ui.button(label="Cancel", style=nextcord.ButtonStyle.red, row=1)
async def cancel(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
):
async def cancel(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
"""
Cancels event submission. Removes embed and view.
"""
await interaction.response.edit_message(
"Canceled", ephemeral=True, embed=None, view=None
)
await interaction.response.edit_message("Canceled", ephemeral=True, embed=None, view=None)
self.stop()
@nextcord.ui.button(label="Edit", style=nextcord.ButtonStyle.blurple, row=1)
@@ -111,14 +102,10 @@ class EventView(nextcord.ui.View):
"""
Calls modal to update the text input information of the event.
"""
await interaction.response.send_modal(
ParticipantModal(interaction.message.embeds[0])
)
await interaction.response.send_modal(ParticipantModal(interaction.message.embeds[0]))
@nextcord.ui.button(label="Confirm", style=nextcord.ButtonStyle.green, row=1)
async def confirm(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
):
async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
"""
Submits the event embed to the event channel. Removes view and embed from recording message.
Sends point information to database.
@@ -126,9 +113,7 @@ class EventView(nextcord.ui.View):
discord_users = self.event_participants.values.members
if discord_users:
discord_users_list: list[str] = [
str(user.id) for user in discord_users if user.id is not self.host_id
]
discord_users_list: list[str] = [str(user.id) for user in discord_users if user.id is not self.host_id]
record_event_points(
self.config,
discord_users_list,
@@ -146,17 +131,11 @@ class EventView(nextcord.ui.View):
self.config.params.event_participate_points,
)
record_event_points(
self.config, [self.host_id], True, self.config.params.event_host_points
)
record_event_points(self.config, [self.host_id], True, self.config.params.event_host_points)
event_channel: nextcord.TextChannel = interaction.guild.get_channel(
self.config.params.event_post_channel_id
)
event_channel: nextcord.TextChannel = interaction.guild.get_channel(self.config.params.event_post_channel_id)
await event_channel.send(embed=interaction.message.embeds[0])
await interaction.response.edit_message(
content="Event has been submitted.", view=None, embed=None
)
await interaction.response.edit_message(content="Event has been submitted.", view=None, embed=None)
self.stop()
@@ -201,9 +180,7 @@ class ParticipantModal(nextcord.ui.Modal):
async def callback(self, interaction: nextcord.Interaction):
self.embed.title = self.name.value
self.embed.description = self.description.value
self.embed.set_field_at(
index=1, name="Non-Discord Participants", value=self.participants.value
)
self.embed.set_field_at(index=1, name="Non-Discord Participants", value=self.participants.value)
await interaction.response.edit_message(embed=self.embed)
@@ -223,8 +200,6 @@ class ParticipantSelect(nextcord.ui.UserSelect):
async def callback(self, interaction: nextcord.Interaction):
embed: nextcord.Embed = interaction.message.embeds[0]
participants: str = "\n".join(
[f"<@{participant.id}>" for participant in self.values.members]
)
participants: str = "\n".join([f"<@{participant.id}>" for participant in self.values.members])
embed.set_field_at(0, name="Participants", value=participants)
await interaction.response.edit_message(embed=embed)
+3 -9
View File
@@ -22,9 +22,7 @@ class RefreshCog(commands.Cog, name="Refresh Commands"):
super().__init__()
self.bot: commands.Bot = bot
@nextcord.slash_command(
default_member_permissions=nextcord.Permissions(administrator=True)
)
@nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True))
async def refresh(self, interaction: nextcord.Interaction):
"""
Parent slash command for refresh. Never called.
@@ -50,9 +48,7 @@ class RefreshCog(commands.Cog, name="Refresh Commands"):
if channel is None:
channel_updates = [
dir_name
for dir_name in os.listdir(MESSAGES_DIR)
if os.path.isdir(f"{MESSAGES_DIR}/{dir_name}")
dir_name for dir_name in os.listdir(MESSAGES_DIR) if os.path.isdir(f"{MESSAGES_DIR}/{dir_name}")
]
else:
channel_updates = [channel.name]
@@ -69,6 +65,4 @@ class RefreshCog(commands.Cog, name="Refresh Commands"):
interaction (nextcord.Interaction): Interaction with the command.
"""
await edit_clan_council_info(interaction.guild, self.bot.config)
await interaction.response.send_message(
"Clan Council Info message updated.", ephemeral=True
)
await interaction.response.send_message("Clan Council Info message updated.", ephemeral=True)
+5 -15
View File
@@ -32,9 +32,7 @@ class RemoveCog(commands.Cog, name="Remove Commands"):
async def presence(
self,
interaction: nextcord.Interaction,
presence_str: str = nextcord.SlashOption(
description="The presence to remove", required=True
),
presence_str: str = nextcord.SlashOption(description="The presence to remove", required=True),
):
"""
Deletes the given presence id from the database.
@@ -53,9 +51,7 @@ class RemoveCog(commands.Cog, name="Remove Commands"):
await interaction.response.send_message("Presence deleted.", ephemeral=True)
@presence.on_autocomplete("presence_str")
async def presence_autocomplete(
self, interaction: nextcord.Interaction, presence_str: str
):
async def presence_autocomplete(self, interaction: nextcord.Interaction, presence_str: str):
"""
Collects all presences that start with the presence string.
Sends autocomplete dict {Presence Name, Presence Id}
@@ -67,20 +63,14 @@ class RemoveCog(commands.Cog, name="Remove Commands"):
with Session(engine) as session:
stmt = select(Presences)
presences_dict: "dict[str,str]" = {
pres.Presence: str(pres.Id) for pres in session.scalars(stmt)
}
presences_dict: "dict[str,str]" = {pres.Presence: str(pres.Id) for pres in session.scalars(stmt)}
# if empty compare string
if not presence_str:
await interaction.response.send_autocomplete(
dict(list(presences_dict.items())[:25])
)
await interaction.response.send_autocomplete(dict(list(presences_dict.items())[:25]))
return
autofilled_presences: "dict[str,str]" = {
pres: pres_id
for pres, pres_id in presences_dict.items()
if presence_str.lower() in pres.lower()
pres: pres_id for pres, pres_id in presences_dict.items() if presence_str.lower() in pres.lower()
}
await interaction.response.send_autocomplete(autofilled_presences[:25])
+10 -31
View File
@@ -27,9 +27,7 @@ class RequestCog(commands.Cog, name="Request Commands"):
Parent slash command for request. Never called.
"""
@request.subcommand(
description="Request to add a presence to the bot's presence list."
)
@request.subcommand(description="Request to add a presence to the bot's presence list.")
async def add_presence(
self,
interaction: nextcord.Interaction,
@@ -51,12 +49,8 @@ class RequestCog(commands.Cog, name="Request Commands"):
config: DakinyConfig = self.bot.config
content = f'<@{interaction.user.id}> would like to add presence "{presence}."'
view = PresenceChange(presence)
await self.bot.get_channel(config.params.admin_channel_id).send(
content=content, view=view
)
await interaction.response.send_message(
"Request sent to admins.", ephemeral=True
)
await self.bot.get_channel(config.params.admin_channel_id).send(content=content, view=view)
await interaction.response.send_message("Request sent to admins.", ephemeral=True)
@request.subcommand(description="Request to change your Dakiny nickname")
async def change_nickname(
@@ -80,12 +74,8 @@ class RequestCog(commands.Cog, name="Request Commands"):
config: DakinyConfig = self.bot.config
content = f'<@{interaction.user.id}> would like to change their username to "{new_nickname}."'
view = UsernameRequest(interaction.user.id, new_nickname)
await self.bot.get_channel(config.params.admin_channel_id).send(
content=content, view=view
)
await interaction.response.send_message(
"Request sent to admins.", ephemeral=True
)
await self.bot.get_channel(config.params.admin_channel_id).send(content=content, view=view)
await interaction.response.send_message("Request sent to admins.", ephemeral=True)
class UsernameRequest(nextcord.ui.View):
@@ -103,9 +93,7 @@ class UsernameRequest(nextcord.ui.View):
self.new_nickname = new_nickname
@nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green)
async def approve(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
):
async def approve(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
"""
Approve button for a nickname change request. Changes the target's nickname to self.new_nickname.
Catches an error if the target as a higher role than the bot.
@@ -119,10 +107,7 @@ class UsernameRequest(nextcord.ui.View):
if user is not None:
try:
await user.edit(nick=self.new_nickname)
content = (
interaction.message.content
+ f"\nApproved by <@{interaction.user.id}>."
)
content = interaction.message.content + f"\nApproved by <@{interaction.user.id}>."
await interaction.response.edit_message(content=content, view=None)
except nextcord.errors.Forbidden:
await interaction.response.send_message(
@@ -133,9 +118,7 @@ class UsernameRequest(nextcord.ui.View):
interaction.message.content
+ "\n\nFailed. User has too high permissions for bot to change nickname."
)
await interaction.followup.edit_message(
interaction.message.id, content=content, view=None
)
await interaction.followup.edit_message(interaction.message.id, content=content, view=None)
else:
await interaction.response.edit_message(
content="An error occured while changing the user's nickname.",
@@ -168,9 +151,7 @@ class PresenceChange(nextcord.ui.View):
self.presence = presence
@nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green)
async def approve(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
):
async def approve(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
"""
Approve button for a presence add request. Adds the presence to the database.
Removes the view from the interaction message.
@@ -184,9 +165,7 @@ class PresenceChange(nextcord.ui.View):
session.add(Presences(Presence=self.presence))
session.commit()
content = (
interaction.message.content + f"\nApproved by <@{interaction.user.id}>."
)
content = interaction.message.content + f"\nApproved by <@{interaction.user.id}>."
await interaction.response.edit_message(content=content, view=None)
@nextcord.ui.button(label="Deny", style=nextcord.ButtonStyle.red)
+8 -24
View File
@@ -30,18 +30,12 @@ class setCog(commands.Cog, name="Set Commands"):
Parent slash command for set. Never called.
"""
@set.subcommand(
description="Sets your birth month and day for the Birthday Fun. Use 0/0 to remove yourself."
)
@set.subcommand(description="Sets your birth month and day for the Birthday Fun. Use 0/0 to remove yourself.")
async def birthday(
self,
interaction: nextcord.Interaction,
month: int = nextcord.SlashOption(
required=True, min_value=0, max_value=12, description="Your birth month."
),
day: int = nextcord.SlashOption(
required=True, min_value=0, max_value=31, description="Your birth day."
),
month: int = nextcord.SlashOption(required=True, min_value=0, max_value=12, description="Your birth month."),
day: int = nextcord.SlashOption(required=True, min_value=0, max_value=31, description="Your birth day."),
):
"""
Sets a user's birthday in the database. Removes on input 0/0. If birthday is today, adds to the birthday_list.
@@ -56,9 +50,7 @@ class setCog(commands.Cog, name="Set Commands"):
await interaction.response.defer(ephemeral=True)
if month == 0 and day == 0:
stmt = delete(UserBirthday).where(
UserBirthday.UserId == interaction.user.id
)
stmt = delete(UserBirthday).where(UserBirthday.UserId == interaction.user.id)
with Session(engine) as session:
session.execute(stmt)
session.commit()
@@ -70,18 +62,14 @@ class setCog(commands.Cog, name="Set Commands"):
try:
birthday = datetime(today.year, month, day)
except ValueError:
await interaction.followup.send(
f"Invalid date: {month}/{day}.", ephemeral=True
)
await interaction.followup.send(f"Invalid date: {month}/{day}.", ephemeral=True)
return
# If the birthday is today, add to birthday_list
if today.date() == birthday.date():
birthday_list.append(interaction.user.id)
new_birthday = UserBirthday(
UserId=interaction.user.id, Birthday=f"{month}/{day}"
)
new_birthday = UserBirthday(UserId=interaction.user.id, Birthday=f"{month}/{day}")
with Session(engine) as session:
try:
session.add(new_birthday)
@@ -89,13 +77,9 @@ class setCog(commands.Cog, name="Set Commands"):
except IntegrityError:
# User already has birthday, change it instead.
session.rollback()
stmt = select(UserBirthday).where(
UserBirthday.UserId == interaction.user.id
)
stmt = select(UserBirthday).where(UserBirthday.UserId == interaction.user.id)
new_birthday = session.scalar(stmt)
new_birthday.Birthday = f"{month}/{day}"
session.commit()
await interaction.followup.send(
f"Birthday set to {month}/{day}.", ephemeral=True
)
await interaction.followup.send(f"Birthday set to {month}/{day}.", ephemeral=True)
+13 -50
View File
@@ -60,9 +60,7 @@ class ViewCog(commands.Cog, name="View Commands"):
)
if df.empty:
await interaction.followup.send(
f'Quarter "{quarter_name}" is invalid.', ephemeral=True
)
await interaction.followup.send(f'Quarter "{quarter_name}" is invalid.', ephemeral=True)
return
cycles = df.groupby("Cycle")
@@ -74,32 +72,19 @@ class ViewCog(commands.Cog, name="View Commands"):
non_members = unsorted_all.get_group(False)
sorted_members = members.sort_values(
by="UserId",
key=lambda user_ids: (
[
interaction.guild.get_member(int(id)).display_name
for id in user_ids
]
),
key=lambda user_ids: ([interaction.guild.get_member(int(id)).display_name for id in user_ids]),
)
sorted_non_members = non_members.sort_values(by="UserId")
sorted_all = pd.concat(
[sorted_members, sorted_non_members], ignore_index=True, sort=False
)
sorted_all = pd.concat([sorted_members, sorted_non_members], ignore_index=True, sort=False)
for row in sorted_all.itertuples():
userid = row.UserId
points = row.Points
discord_member = row.DiscordMember
content += (
f"<@{userid}>: {points}\n"
if discord_member
else f"{userid}: {points}\n"
)
content += f"<@{userid}>: {points}\n" if discord_member else f"{userid}: {points}\n"
content += "\n"
if len(content) > 2000:
await interaction.followup.send(
"Message length is too long, please select search to limit results."
)
await interaction.followup.send("Message length is too long, please select search to limit results.")
return
await interaction.followup.send(content=content, ephemeral=True)
@@ -128,21 +113,13 @@ class ViewCog(commands.Cog, name="View Commands"):
presences = session.scalars(stmt)
if search:
message = "\n".join(
[
presence
for presence in presences
if search.lower() in presence.lower()
]
)
message = "\n".join([presence for presence in presences if search.lower() in presence.lower()])
else:
message = "\n".join(list(presences))
await interaction.followup.send(content=message, ephemeral=True)
@view.subcommand(
description="Shows all voters and who they voted for in a given election."
)
@view.subcommand(description="Shows all voters and who they voted for in a given election.")
async def votes(
self,
interaction: nextcord.Interaction,
@@ -165,18 +142,12 @@ class ViewCog(commands.Cog, name="View Commands"):
election_name (str): Is SlashOption. The name of the election to view votes for.
"""
stmt = (
select(Votes)
.where(Votes.ElectionCycle == election_name)
.where(Votes.Type == election_type)
)
stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == election_type)
with Session(engine) as session:
# Check if election exists.
if session.scalar(stmt) is None:
await interaction.response.send_message(
f"Invalid election: {election_name}.", ephemeral=True
)
await interaction.response.send_message(f"Invalid election: {election_name}.", ephemeral=True)
return
votes = session.scalars(stmt)
@@ -191,9 +162,7 @@ class ViewCog(commands.Cog, name="View Commands"):
await interaction.response.send_message(content=content, ephemeral=True)
@votes.on_autocomplete("election_name")
async def election_autocomplete(
self, interaction: nextcord.Interaction, election_name: str
):
async def election_autocomplete(self, interaction: nextcord.Interaction, election_name: str):
"""
Collects all elections that start with the election string.
@@ -212,16 +181,12 @@ class ViewCog(commands.Cog, name="View Commands"):
return
autofilled_elections: "list[str]" = [
election
for election in elections
if election.lower().startswith(election_name.lower())
election for election in elections if election.lower().startswith(election_name.lower())
]
await interaction.response.send_autocomplete(autofilled_elections[:25])
@event_points.on_autocomplete("quarter_name")
async def event_points_autocomplete(
self, interaction: nextcord.Interaction, quarter_name: str
):
async def event_points_autocomplete(self, interaction: nextcord.Interaction, quarter_name: str):
"""
Collects all elections that start with the election string.
@@ -240,8 +205,6 @@ class ViewCog(commands.Cog, name="View Commands"):
return
autofilled_quarters: "list[str]" = [
quarter
for quarter in quarters
if quarter.lower().startswith(quarter_name.lower())
quarter for quarter in quarters if quarter.lower().startswith(quarter_name.lower())
]
await interaction.response.send_autocomplete(autofilled_quarters[:25])