Compare commits
92
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1814867c09 | ||
|
|
390bf8a489 | ||
|
|
74aa78c748 | ||
|
|
379f280c11 | ||
|
|
7d1e69c007 | ||
|
|
b905ce7216 | ||
|
|
4bb7db3f71 | ||
|
|
b3f48d4603 | ||
|
|
90763c73a7 | ||
|
|
635419dc7c | ||
|
|
2356d5edbf | ||
|
|
9eed0a1f4e | ||
|
|
a9a6ab4520 | ||
|
|
e08a764591 | ||
|
|
8b43f766b3 | ||
|
|
cb9cad9783 | ||
|
|
216becff6e | ||
|
|
836673198a | ||
|
|
e34739a0b1 | ||
|
|
7c592e6acc | ||
|
|
0f8637be23 | ||
|
|
3b597aeac7 | ||
|
|
c43b2297a4 | ||
|
|
1b337a291d | ||
|
|
16b662875f | ||
|
|
307f61934e | ||
|
|
00265f2f58 | ||
|
|
93d558edbc | ||
|
|
0f2b1aa2e6 | ||
|
|
3f6bac99ab | ||
|
|
46e2955642 | ||
|
|
ac9bf6b35f | ||
|
|
869b2ccb82 | ||
|
|
3ddfdfe9b0 | ||
|
|
3860e74579 | ||
|
|
3b49a6ec2a | ||
|
|
2f89588358 | ||
|
|
0513df944f | ||
|
|
ddf319b9d5 | ||
|
|
13f5bc0aab | ||
|
|
e6f3fcb604 | ||
|
|
eaa342d479 | ||
|
|
ac11f3e462 | ||
|
|
106b394c44 | ||
|
|
6c1505685b | ||
|
|
2e681be690 | ||
|
|
5448f2a426 | ||
|
|
7647c355af | ||
|
|
a95fca40f9 | ||
|
|
148ba253cf | ||
|
|
640d481018 | ||
|
|
4c18513661 | ||
|
|
37b4d2d62e | ||
|
|
9c762fb346 | ||
|
|
2c9e9e246a | ||
|
|
ab721c1625 | ||
|
|
2864f342cc | ||
|
|
92511db446 | ||
|
|
44c2720f80 | ||
|
|
35cebdebce | ||
|
|
e4437cc654 | ||
|
|
fe2c400a2a | ||
|
|
39875585ef | ||
|
|
7796ead644 | ||
|
|
531d2f3a0e | ||
|
|
8595a12f7f | ||
|
|
f76171ac84 | ||
|
|
1605c8ad50 | ||
|
|
63e77958fb | ||
|
|
0bae0ea5ff | ||
|
|
c67e9ae617 | ||
|
|
7e23ac14ff | ||
|
|
a0a7e37921 | ||
|
|
c01c888503 | ||
|
|
58dbe547bb | ||
|
|
3cf4fc6b33 | ||
|
|
411f8ab275 | ||
|
|
8952c4a352 | ||
|
|
93248797a0 | ||
|
|
dace72f5cc | ||
|
|
f468a028d0 | ||
|
|
a9b889234a | ||
|
|
88cec73644 | ||
|
|
91e57c6555 | ||
|
|
6c5f40f788 | ||
|
|
a9920db381 | ||
|
|
d04b8c5e32 | ||
|
|
3627419e12 | ||
|
|
3446ce9ca2 | ||
|
|
582b88e248 | ||
|
|
2db744d170 | ||
|
|
fa33be1b4a |
@@ -0,0 +1,20 @@
|
||||
name: build_develop
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Install Docker
|
||||
uses: https://github.com/papodaca/install-docker-action@v1
|
||||
- name: Build Docker Image
|
||||
run: docker build -t viker.cheezers.net:5000/agon:develop .
|
||||
- name: Push Docker Image
|
||||
run: docker push viker.cheezers.net:5000/agon:develop
|
||||
@@ -0,0 +1,24 @@
|
||||
name: build_release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags: ["v[0-9].[0-9].[0-9]"]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Install Docker
|
||||
uses: https://github.com/papodaca/install-docker-action@v1
|
||||
- name: Set env
|
||||
run: echo "GIT_TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
|
||||
- name: Build Docker Images
|
||||
run: docker build -t viker.cheezers.net:5000/agon:latest -t viker.cheezers.net:5000/agon:${{env.GIT_TAG}} .
|
||||
- name: Push Latest Image
|
||||
run: docker push viker.cheezers.net:5000/agon:latest
|
||||
- name: Push Tagged Image
|
||||
run: docker push viker.cheezers.net:5000/agon:${{env.GIT_TAG}}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
FROM python:3.8.10-slim
|
||||
|
||||
# install dependencies for mysqlclient
|
||||
# RUN apt-get update && apt-get -y upgrade && apt-get -y install python3-dev default-libmysqlclient-dev build-essential
|
||||
|
||||
RUN pip install --upgrade pip
|
||||
|
||||
WORKDIR /home
|
||||
|
||||
COPY classes classes/
|
||||
COPY cogs cogs/
|
||||
COPY utilities utilities/
|
||||
COPY highscores.py highscores.py
|
||||
COPY requirements.txt requirements.txt
|
||||
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
# remove build deps that aren't needed to run
|
||||
# RUN apt-get -y remove python3-dev build-essential && apt-get -y autoremove
|
||||
|
||||
CMD ["python3", "./highscores.py"]
|
||||
+11
-9
@@ -6,9 +6,10 @@ from enum import Enum
|
||||
|
||||
from nextcord import Embed
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_alchemy_connection import Category, CategoryGroup, session
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, engine
|
||||
|
||||
|
||||
class SubmissionState(Enum):
|
||||
@@ -199,14 +200,15 @@ class Submission:
|
||||
.where(CategoryGroup.GuildId == self.guild_id)
|
||||
.where(CategoryGroup.GroupId == self.group_id)
|
||||
)
|
||||
embed.add_field(name="Group", value=session.scalar(stmt), inline=False)
|
||||
if self.category_id:
|
||||
stmt = (
|
||||
select(Category.CategoryName)
|
||||
.where(Category.GroupId == self.group_id)
|
||||
.where(Category.CategoryId == self.category_id)
|
||||
)
|
||||
embed.add_field(name="Category", value=session.scalar(stmt), inline=False)
|
||||
with Session(engine) as session:
|
||||
embed.add_field(name="Group", value=session.scalar(stmt), inline=False)
|
||||
if self.category_id:
|
||||
stmt = (
|
||||
select(Category.CategoryName)
|
||||
.where(Category.GroupId == self.group_id)
|
||||
.where(Category.CategoryId == self.category_id)
|
||||
)
|
||||
embed.add_field(name="Category", value=session.scalar(stmt), inline=False)
|
||||
if self.score:
|
||||
embed.add_field(name="Score", value=self.score, inline=False)
|
||||
|
||||
|
||||
@@ -4,10 +4,13 @@ Cog loader. Adds all command cogs to the bot.
|
||||
|
||||
from nextcord.ext import commands
|
||||
|
||||
# Command classes
|
||||
from .data_management import DataManagementCog
|
||||
from .message_management import MessageManagementCog
|
||||
from .settings_management import SettingsManagementCog
|
||||
from .create import CreateCog
|
||||
from .delete import DeleteCog
|
||||
from .help import HelpCog
|
||||
from .refresh import RefreshCog
|
||||
from .set import SetCog
|
||||
from .update import UpdateCog
|
||||
from .view import ViewCog
|
||||
|
||||
|
||||
# This function will be called when this extension is loaded.
|
||||
@@ -16,6 +19,10 @@ def setup(bot: commands.Bot):
|
||||
"""
|
||||
Adds all command classes to bot.
|
||||
"""
|
||||
bot.add_cog(DataManagementCog(bot))
|
||||
bot.add_cog(MessageManagementCog(bot))
|
||||
bot.add_cog(SettingsManagementCog())
|
||||
bot.add_cog(CreateCog(bot))
|
||||
bot.add_cog(DeleteCog())
|
||||
bot.add_cog(HelpCog())
|
||||
bot.add_cog(RefreshCog(bot))
|
||||
bot.add_cog(SetCog())
|
||||
bot.add_cog(UpdateCog(bot))
|
||||
bot.add_cog(ViewCog())
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
"""
|
||||
Commands for creating items in bot.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import exc, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from cogs.submission.submission_views import SubmissionCreateButton
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, engine
|
||||
from utilities.utils import MAX_LINE_LENGTH, autofill_groups, send_highscore_message
|
||||
|
||||
|
||||
class CreateCog(commands.Cog, name="Create Cog"):
|
||||
"""
|
||||
Class holds commands for creating things.
|
||||
"""
|
||||
|
||||
def __init__(self, bot: commands.Bot) -> None:
|
||||
"""
|
||||
Default constructor for CreateCog class.
|
||||
"""
|
||||
self._bot = bot
|
||||
|
||||
@nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True))
|
||||
async def create(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for creating things. Never called.
|
||||
"""
|
||||
|
||||
# need all these arguments because discord command.
|
||||
# pylint: disable=too-many-arguments
|
||||
@create.subcommand(description="Create a new Category. Must not exist already in Group.")
|
||||
async def category(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Group to add Category to.", required=True),
|
||||
category_name: str = nextcord.SlashOption(description="Name of new Category.", required=True),
|
||||
is_ascending: bool = nextcord.SlashOption(description="If higher value is better score.", required=True),
|
||||
is_time_record: bool = nextcord.SlashOption(
|
||||
description="If the score should be shown in time format. Cannot be changed.", required=True
|
||||
),
|
||||
is_validated: bool = nextcord.SlashOption(
|
||||
description="If scores need to be validated before posted.", required=True
|
||||
),
|
||||
size: int = nextcord.SlashOption(
|
||||
description="How many scores to post in Category. Default is 3.", required=False, default=3, min_value=1
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Creates a new category to be placed into group_id group. Fails if the category name already exists within the
|
||||
group. Then calls send_highscore_message().
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction for the command.
|
||||
group_id (str): Id of group to add category to. Is SlashOption.
|
||||
category_name (str): Name of new category. Is SlashOption.
|
||||
is_ascending (bool): If the scores are sorted ascending or not. Is SlashOption.
|
||||
is_time_record (bool): If the scores are a time record or not. Is SlashOption.
|
||||
is_validated (bool): If the scores should be validated or not. Is SlashOption.
|
||||
size (int): How many sores to show in the category message. Is SlashOption.
|
||||
"""
|
||||
|
||||
new_category = Category(
|
||||
CategoryName=category_name,
|
||||
GroupId=int(group_id),
|
||||
Size=size,
|
||||
IsAscending=is_ascending,
|
||||
IsTimeRecord=is_time_record,
|
||||
IsValidated=is_validated,
|
||||
)
|
||||
|
||||
# Open connection to engine and add new category.
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
try:
|
||||
session.add(new_category)
|
||||
session.commit()
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
await interaction.response.send_message(
|
||||
f'"{category_name}" has been added to "{group.GroupName}"!', ephemeral=True
|
||||
)
|
||||
except exc.IntegrityError:
|
||||
# Category name already exists in group
|
||||
session.rollback()
|
||||
await interaction.response.send_message(
|
||||
f'"{category_name}" already exists in "{group.GroupName}". Please rename.', ephemeral=True
|
||||
)
|
||||
|
||||
# need all these arguments because discord command.
|
||||
# pylint: disable=too-many-arguments
|
||||
@create.subcommand(description="Create a new Category Group. Must not exist already for Guild.")
|
||||
async def group(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_name: str = nextcord.SlashOption(description="Name of new Group.", required=True),
|
||||
pad_length: int = nextcord.SlashOption(
|
||||
description="Number of characters wide a category is printed. Default is 26.",
|
||||
required=False,
|
||||
default=26,
|
||||
min_value=7,
|
||||
max_value=MAX_LINE_LENGTH,
|
||||
),
|
||||
username_length: int = nextcord.SlashOption(
|
||||
description="Number of characters of a username to display. Default is 12.",
|
||||
required=False,
|
||||
default=12,
|
||||
min_value=1,
|
||||
max_value=30,
|
||||
),
|
||||
channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption(
|
||||
description="Channel to post Group in. If left empty, defaults to current channel.",
|
||||
required=False,
|
||||
channel_types=[nextcord.ChannelType.text],
|
||||
default=None,
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Creates a new category group to be placed into the database with the guild's id. Fails if the category group
|
||||
name already exists within the guild. Then calls send_highscore_message().
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction for the command.
|
||||
group_name (str): Name of new Group. Is SlashOption.
|
||||
pad_length (int): Number of characters wide a category is printed. Is SlashOption.
|
||||
username_length (int): Number of characters of a username to display. Is SlashOption.
|
||||
channel (Optional[nextcord.abc.GuildChannel]): Channel to post group in. If None, defaults to current
|
||||
channel. Is SlashOption.
|
||||
"""
|
||||
|
||||
# if no channel provided, use current channel.
|
||||
if channel is None:
|
||||
channel = interaction.channel
|
||||
|
||||
new_group = CategoryGroup(
|
||||
GroupName=group_name,
|
||||
ChannelId=channel.id,
|
||||
GuildId=interaction.guild_id,
|
||||
PadLength=pad_length,
|
||||
UsernameLength=username_length,
|
||||
)
|
||||
|
||||
with Session(engine) as session:
|
||||
try:
|
||||
session.add(new_group)
|
||||
session.commit()
|
||||
message: nextcord.Message = await send_highscore_message(session, interaction.guild, new_group)
|
||||
await message.edit(view=SubmissionCreateButton(self._bot))
|
||||
await interaction.response.send_message(f'"{group_name}" has been created!', ephemeral=True)
|
||||
except exc.IntegrityError:
|
||||
# Category Group already exists in guild
|
||||
session.rollback()
|
||||
await interaction.response.send_message(
|
||||
f'"{group_name}" already exists in "{interaction.guild.name}". Please rename.', ephemeral=True
|
||||
)
|
||||
|
||||
@category.on_autocomplete("group_id")
|
||||
async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str):
|
||||
"""
|
||||
Collects list of table names and compares to input table name for autocomplete assistance.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction with command.
|
||||
group_id (str): String to run autocomplete compared to.
|
||||
"""
|
||||
|
||||
await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id))
|
||||
@@ -1,241 +0,0 @@
|
||||
"""
|
||||
Commands for managing the tables/categories/scores for a Guild.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import distinct, select
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, Score, session
|
||||
from utilities.utils import (
|
||||
autofill_categories,
|
||||
autofill_groups,
|
||||
autofill_users_in_group_category,
|
||||
autofill_users_in_guild,
|
||||
send_highscore_message,
|
||||
)
|
||||
|
||||
from .views.update_username_verify import UpdateUsernameVerifyView
|
||||
|
||||
|
||||
class DataManagementCog(commands.Cog, name="Table Management"):
|
||||
"""
|
||||
Class holds commands for managing the data storage of the highscores.
|
||||
"""
|
||||
|
||||
def __init__(self, bot: commands.Bot):
|
||||
"""
|
||||
Default constructor for DataManagement class. Sets internal bot variable.
|
||||
"""
|
||||
self._bot: commands.Bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
"""
|
||||
Called when cog is loaded. Adds views required for DataManagement commands.
|
||||
"""
|
||||
self._bot.add_view(UpdateUsernameVerifyView())
|
||||
|
||||
@nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True))
|
||||
async def delete(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for deleting things. Never called.
|
||||
"""
|
||||
|
||||
@delete.subcommand()
|
||||
async def user(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Subcommand parent for deleting user information. Never called.
|
||||
"""
|
||||
|
||||
@user.subcommand(
|
||||
description="Removes all of a user's scores. Warning: data is destroyed, use with case.",
|
||||
)
|
||||
async def from_all(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
user_name: str = nextcord.SlashOption(
|
||||
description="User to remove scores for. Note: This is not always a user's discord name.", required=True
|
||||
),
|
||||
):
|
||||
"""
|
||||
Searches all of guild's scores data for any score from the user. Removes. Updates table
|
||||
message is user was removed from that table.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
user_name (str): User to remove from highscores
|
||||
"""
|
||||
|
||||
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == user_name)
|
||||
category_ids: "list[int]" = list(session.scalars(stmt))
|
||||
|
||||
stmt = select(Score).where(Score.UserName == user_name)
|
||||
for score in session.scalars(stmt):
|
||||
session.delete(score)
|
||||
session.commit()
|
||||
|
||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids))
|
||||
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
||||
|
||||
for group in session.scalars(stmt):
|
||||
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
|
||||
|
||||
await interaction.send(f'User "{user_name}" was removed from all tables.', ephemeral=True)
|
||||
|
||||
@user.subcommand(description="Deletes a user's scores from a category.")
|
||||
async def from_category(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Group to delete user from.", required=True),
|
||||
category_id: str = nextcord.SlashOption(
|
||||
name="category", description="Category to delete user from.", required=True
|
||||
),
|
||||
user_name: str = nextcord.SlashOption(description="User to delete.", required=True),
|
||||
):
|
||||
"""
|
||||
Removes a user from a category. Uses autofill functions to fill out group_id, category_id, and user_name.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String representation of Group id to remove user from. Is SlashOption.
|
||||
category_id (str): String representation of Category id to remove user from. Is SlashOption.
|
||||
user_name (str): The user to remove. Is SlashOption.
|
||||
"""
|
||||
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
|
||||
stmt = select(Category.CategoryName).where(Category.CategoryId == int(category_id))
|
||||
category_name = session.scalar(stmt)
|
||||
|
||||
stmt = select(Score).where(Score.CategoryId == int(category_id)).where(Score.UserName == user_name)
|
||||
|
||||
# there should only be one because category id and username are a composite primary key in score
|
||||
session.delete(session.scalar(stmt))
|
||||
session.commit()
|
||||
|
||||
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
|
||||
await interaction.response.send_message(f'User "{user_name}" removed from "{category_name}".', ephemeral=True)
|
||||
|
||||
@user.subcommand(description="Deletes a user's scores from entire group.")
|
||||
async def from_group(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Group to delete user from.", required=True),
|
||||
user_name: str = nextcord.SlashOption(description="User to delete.", required=True),
|
||||
):
|
||||
"""
|
||||
Removes user from a goup. Uses autofill functions to fill out group_id and user_name.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String representation of Group id to remove user from. Is SlashOption.
|
||||
user_name (str): The user to remove. Is SlashOption.
|
||||
"""
|
||||
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
category_ids = select(Category.CategoryId).where(Category.GroupId == int(group_id))
|
||||
stmt = select(Score).where(Score.CategoryId.in_(category_ids)).where(Score.UserName == user_name)
|
||||
|
||||
for score in session.scalars(stmt):
|
||||
session.delete(score)
|
||||
session.commit()
|
||||
|
||||
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
|
||||
await interaction.response.send_message(
|
||||
f'User "{user_name}" removed from all categories in {group.GroupName}.', ephemeral=True
|
||||
)
|
||||
|
||||
@from_category.on_autocomplete("group_id")
|
||||
@from_group.on_autocomplete("group_id")
|
||||
async def autocomplete_group(self, interaction: nextcord.Interaction, group_id: str):
|
||||
"""
|
||||
Autocomplete function for table names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String to be autocompleted with Group name.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id))
|
||||
|
||||
@from_category.on_autocomplete("category_id")
|
||||
async def autocomplete_category(self, interaction: nextcord.Interaction, category_id: str, group_id: str):
|
||||
"""
|
||||
Autocomplete function for category names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
category_id (str): String to be autocompleted with Category name.
|
||||
group_id (str): Group to find Category from.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(
|
||||
autofill_categories(interaction.guild_id, int(group_id), category_id)
|
||||
)
|
||||
|
||||
@from_category.on_autocomplete("user_name")
|
||||
@from_group.on_autocomplete("user_name")
|
||||
async def autocomplete_user(
|
||||
self, interaction: nextcord.Interaction, user_name: str, group_id: str, category_id: Optional[str] = None
|
||||
):
|
||||
"""
|
||||
Autocomplete functions for user names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
user_name (str): String to be autocompleted with User name.
|
||||
group_id (str): String representation of Group id to find category from.
|
||||
category_id (Optional[str]): String representation of Category id to find User from, can be None.
|
||||
"""
|
||||
|
||||
await interaction.response.send_autocomplete(
|
||||
autofill_users_in_group_category(
|
||||
interaction.guild_id, int(group_id), category_id if category_id else 0, user_name
|
||||
)
|
||||
)
|
||||
|
||||
@nextcord.slash_command(description="Request a username update. Admins must approve.")
|
||||
async def update_username(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
old_username: str = nextcord.SlashOption(description="Username currently stored in highscores.", required=True),
|
||||
new_username: str = nextcord.SlashOption(description="Username to update to.", required=True),
|
||||
):
|
||||
"""
|
||||
Creates a request to send to admins for changing a username. Uses
|
||||
UpdateUsernameVerifyView to do the work.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
old_username (str): Username to replace.
|
||||
new_username (str): Username to replace old with.
|
||||
"""
|
||||
stmt = select(Guild.VerificationChannel).where(Guild.GuildId == interaction.guild_id)
|
||||
verify_channel = interaction.guild.get_channel(session.scalar(stmt))
|
||||
|
||||
embed: nextcord.Embed = nextcord.Embed(title="Username change request.")
|
||||
embed.add_field(name="Old Username", value=old_username)
|
||||
embed.add_field(name="New Username", value=new_username)
|
||||
await verify_channel.send(
|
||||
content=f"Username change request from <@{interaction.user.id}>.",
|
||||
embed=embed,
|
||||
view=UpdateUsernameVerifyView(),
|
||||
)
|
||||
await interaction.send("Request sent to admins for verification.", ephemeral=True)
|
||||
|
||||
@update_username.on_autocomplete("old_username")
|
||||
@from_all.on_autocomplete("user_name")
|
||||
async def username_autofill(self, interaction: nextcord.Interaction, username: str):
|
||||
"""
|
||||
Sends response from autofill_users_in_guild back to update_username for autocompletion of old_username.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction with command.
|
||||
old_username (str): String to run autocompletion against.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(autofill_users_in_guild(interaction.guild_id, username))
|
||||
@@ -0,0 +1,297 @@
|
||||
"""
|
||||
Commands for deleting groups/categories/scores in database and messages in discord..
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import distinct, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, Score, engine
|
||||
from utilities.utils import (
|
||||
autofill_categories,
|
||||
autofill_groups,
|
||||
autofill_users_in_group_category,
|
||||
autofill_users_in_guild,
|
||||
send_highscore_message,
|
||||
)
|
||||
|
||||
from .views.delete_confirm import DeleteConfirmView, create_delete_id
|
||||
|
||||
|
||||
class DeleteCog(commands.Cog, name="Table Management"):
|
||||
"""
|
||||
Class holds commands for deleting.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Default constructor for DeleteCog class.
|
||||
"""
|
||||
|
||||
@nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True))
|
||||
async def delete(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for deleting things. Never called.
|
||||
"""
|
||||
|
||||
@delete.subcommand()
|
||||
async def all(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Subcommand parent for deleting all of a thing. Never called.
|
||||
"""
|
||||
|
||||
@all.subcommand(description="Clears highscores channel of all table messages. Does not change data.")
|
||||
async def channel_messages(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption(
|
||||
description="Channel to clear of messages.",
|
||||
required=False,
|
||||
channel_types=[nextcord.ChannelType.text],
|
||||
default=None,
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Deletes all group messages from channel. Does not change data.
|
||||
Does not delete highscores channel id. Uses current channel if not supplied a channel.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
channel (Optional[nextcord.abc.GuildChannel]): Channel to clear of messages.
|
||||
Is a SlashOption. Limited to text channels. Defaults to None.
|
||||
"""
|
||||
|
||||
if not channel:
|
||||
channel = interaction.channel
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.ChannelId == channel.id)
|
||||
)
|
||||
# TODO test when nothing in channel
|
||||
groups = session.scalars(stmt)
|
||||
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
|
||||
for group in groups:
|
||||
try:
|
||||
table_message: nextcord.Message = await channel.fetch_message(group.MessageId)
|
||||
await table_message.delete()
|
||||
except nextcord.NotFound:
|
||||
# silently ignore that messages are not found. They got deleted already.
|
||||
pass
|
||||
|
||||
await interaction.send("Table messages deleted.", ephemeral=True)
|
||||
|
||||
@all.subcommand(
|
||||
description="Removes all of a user's scores. Warning: data is destroyed, use with case.",
|
||||
)
|
||||
async def users(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
user_name: str = nextcord.SlashOption(
|
||||
description="User to remove scores for. Note: This is not always a user's discord name.", required=True
|
||||
),
|
||||
):
|
||||
"""
|
||||
Searches all of guild's scores data for any score from the user. Removes. Updates table
|
||||
message is user was removed from that table.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
user_name (str): User to remove from highscores
|
||||
"""
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == user_name)
|
||||
category_ids: "list[int]" = list(session.scalars(stmt))
|
||||
|
||||
stmt = select(Score).where(Score.UserName == user_name)
|
||||
for score in session.scalars(stmt):
|
||||
session.delete(score)
|
||||
session.commit()
|
||||
|
||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids))
|
||||
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
||||
|
||||
for group in session.scalars(stmt):
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
|
||||
await interaction.send(f'User "{user_name}" was removed from all tables.', ephemeral=True)
|
||||
|
||||
@delete.subcommand(description="Delete a category from a group. Deletion is perminant.")
|
||||
async def category(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Group to delete from.", required=True),
|
||||
category_id: str = nextcord.SlashOption(name="category", description="Category to delete.", required=True),
|
||||
) -> None:
|
||||
"""
|
||||
Calls the delete confirmation view for the given group and category.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction of command.
|
||||
group_id (str): Group to delete from. Is SlashOption.
|
||||
category_id (str): Category to delete. Is SlashOption.
|
||||
"""
|
||||
await interaction.response.send_modal(
|
||||
DeleteConfirmView(custom_id=create_delete_id(int(group_id), int(category_id)))
|
||||
)
|
||||
|
||||
@delete.subcommand(description="Delete a group. Deletion is perminant.")
|
||||
async def group(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Group to delete.", required=True),
|
||||
) -> None:
|
||||
"""
|
||||
Calls the delete confirmation view for the given group.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction of command.
|
||||
group_id (str): Group to delete from. Is SlashOption.
|
||||
"""
|
||||
await interaction.response.send_modal(DeleteConfirmView(custom_id=create_delete_id(int(group_id))))
|
||||
|
||||
@delete.subcommand()
|
||||
async def user(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Subcommand parent for deleting user information. Never called.
|
||||
"""
|
||||
|
||||
@user.subcommand(description="Deletes a user's scores from a category.")
|
||||
async def from_category(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Group to delete user from.", required=True),
|
||||
category_id: str = nextcord.SlashOption(
|
||||
name="category", description="Category to delete user from.", required=True
|
||||
),
|
||||
user_name: str = nextcord.SlashOption(description="User to delete.", required=True),
|
||||
):
|
||||
"""
|
||||
Removes a user from a category. Uses autofill functions to fill out group_id, category_id, and user_name.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String representation of Group id to remove user from. Is SlashOption.
|
||||
category_id (str): String representation of Category id to remove user from. Is SlashOption.
|
||||
user_name (str): The user to remove. Is SlashOption.
|
||||
"""
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
|
||||
stmt = select(Category.CategoryName).where(Category.CategoryId == int(category_id))
|
||||
category_name = session.scalar(stmt)
|
||||
|
||||
stmt = select(Score).where(Score.CategoryId == int(category_id)).where(Score.UserName == user_name)
|
||||
|
||||
# there should only be one because category id and username are a composite primary key in score
|
||||
session.delete(session.scalar(stmt))
|
||||
session.commit()
|
||||
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
await interaction.response.send_message(
|
||||
f'User "{user_name}" removed from "{category_name}".', ephemeral=True
|
||||
)
|
||||
|
||||
@user.subcommand(description="Deletes a user's scores from entire group.")
|
||||
async def from_group(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Group to delete user from.", required=True),
|
||||
user_name: str = nextcord.SlashOption(description="User to delete.", required=True),
|
||||
):
|
||||
"""
|
||||
Removes user from a goup. Uses autofill functions to fill out group_id and user_name.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String representation of Group id to remove user from. Is SlashOption.
|
||||
user_name (str): The user to remove. Is SlashOption.
|
||||
"""
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
category_ids = select(Category.CategoryId).where(Category.GroupId == int(group_id))
|
||||
stmt = select(Score).where(Score.CategoryId.in_(category_ids)).where(Score.UserName == user_name)
|
||||
|
||||
for score in session.scalars(stmt):
|
||||
session.delete(score)
|
||||
session.commit()
|
||||
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
await interaction.response.send_message(
|
||||
f'User "{user_name}" removed from all categories in {group.GroupName}.', ephemeral=True
|
||||
)
|
||||
|
||||
@from_category.on_autocomplete("group_id")
|
||||
@from_group.on_autocomplete("group_id")
|
||||
@category.on_autocomplete("group_id")
|
||||
@group.on_autocomplete("group_id")
|
||||
async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str):
|
||||
"""
|
||||
Autocomplete function for table names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String to be autocompleted with Group name.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id))
|
||||
|
||||
@from_category.on_autocomplete("category_id")
|
||||
@category.on_autocomplete("category_id")
|
||||
async def category_autocomplete(self, interaction: nextcord.Interaction, category_id: str, group_id: str):
|
||||
"""
|
||||
Autocomplete function for category names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
category_id (str): String to be autocompleted with Category name.
|
||||
group_id (str): Group to find Category from.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(
|
||||
autofill_categories(interaction.guild_id, int(group_id), category_id)
|
||||
)
|
||||
|
||||
@from_category.on_autocomplete("user_name")
|
||||
@from_group.on_autocomplete("user_name")
|
||||
async def user_autocomplete(
|
||||
self, interaction: nextcord.Interaction, user_name: str, group_id: str, category_id: Optional[str] = None
|
||||
):
|
||||
"""
|
||||
Autocomplete functions for user names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
user_name (str): String to be autocompleted with User name.
|
||||
group_id (str): String representation of Group id to find category from.
|
||||
category_id (Optional[str]): String representation of Category id to find User from, can be None.
|
||||
"""
|
||||
|
||||
await interaction.response.send_autocomplete(
|
||||
autofill_users_in_group_category(
|
||||
interaction.guild_id, int(group_id), category_id if category_id else 0, user_name
|
||||
)
|
||||
)
|
||||
|
||||
@users.on_autocomplete("user_name")
|
||||
async def all_users_autocomplete(self, interaction: nextcord.Interaction, username: str):
|
||||
"""
|
||||
Sends response from autofill_users_in_guild back to update_username for autocompletion of old_username.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction with command.
|
||||
old_username (str): String to run autocompletion against.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(autofill_users_in_guild(interaction.guild_id, username))
|
||||
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
Commands for helping a user.
|
||||
"""
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
|
||||
|
||||
class HelpCog(commands.Cog, name="Help commands."):
|
||||
"""
|
||||
Class holds commands for getting help.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Default constructor for HelpCog class. Does nothing.
|
||||
"""
|
||||
|
||||
@nextcord.slash_command()
|
||||
async def help(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for help. Never called.
|
||||
"""
|
||||
|
||||
@help.subcommand(description="Describes how to get started with Agon.")
|
||||
async def setup(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Sends a message describing how to get started with Agon.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
"""
|
||||
|
||||
# I hate this but don't want indents in the string.
|
||||
# pylint: disable=line-too-long
|
||||
help_str: str = """Welcome to Agon! Three simple steps are needed to get started.
|
||||
1.) Set a channel to recieve submission verifications with "/set verify_channel."
|
||||
This channel is used to allow moderators to check if a submission is real/valid.
|
||||
|
||||
2.) Set a channel for submissions to be posted in with "/set submission_channel."
|
||||
This channel will be where all user submission messages are sent.
|
||||
|
||||
3.) Set up your highscore tables! Use "/create group" to first create a group message in the channel you would like and then use "/create category" to create a category in that group message!"""
|
||||
# pylint: enable=line-too-long
|
||||
print(help_str)
|
||||
|
||||
await interaction.response.send_message(content=help_str)
|
||||
@@ -1,152 +0,0 @@
|
||||
"""
|
||||
Commands for managing/displaying the highscores messages.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import select
|
||||
|
||||
# pylint: disable=import-error
|
||||
from cogs.submission.submission_views import SubmissionCreateButton
|
||||
from utilities.sql_alchemy_connection import CategoryGroup, session
|
||||
from utilities.utils import autofill_groups, send_highscore_message
|
||||
|
||||
|
||||
class MessageManagementCog(commands.Cog, name="Message Management"):
|
||||
"""
|
||||
Class holds commands for managing the messages that display highscores.
|
||||
"""
|
||||
|
||||
def __init__(self, bot: commands.Bot) -> None:
|
||||
"""
|
||||
Default constructor for MessageManagement class
|
||||
|
||||
Args:
|
||||
bot (commands.Bot): Bot object. Required to register submission button view.
|
||||
"""
|
||||
self.bot = bot
|
||||
|
||||
@nextcord.slash_command(
|
||||
description="Clears highscores channel of all table messages. Does not change data.",
|
||||
default_member_permissions=nextcord.Permissions(administrator=True),
|
||||
)
|
||||
async def delete_channel_messages(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption(
|
||||
description="Channel to clear of messages.",
|
||||
required=False,
|
||||
channel_types=[nextcord.ChannelType.text],
|
||||
default=None,
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Deletes all group messages from channel. Does not change data.
|
||||
Does not delete highscores channel id. Uses current channel if not supplied a channel.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
channel (Optional[nextcord.abc.GuildChannel]): Channel to clear of messages.
|
||||
Is a SlashOption. Limited to text channels. Defaults to None.
|
||||
"""
|
||||
|
||||
if not channel:
|
||||
channel = interaction.channel
|
||||
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.ChannelId == channel.id)
|
||||
)
|
||||
# TODO test when nothing in channel
|
||||
groups = session.scalars(stmt)
|
||||
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
|
||||
for group in groups:
|
||||
try:
|
||||
table_message: nextcord.Message = await channel.fetch_message(group.MessageId)
|
||||
await table_message.delete()
|
||||
except nextcord.NotFound:
|
||||
# silently ignore that messages are not found. They got deleted already.
|
||||
pass
|
||||
|
||||
await interaction.send("Table messages deleted.", ephemeral=True)
|
||||
|
||||
@nextcord.slash_command(
|
||||
description="Triggers post/update of highscores to registered highscore channel.",
|
||||
default_member_permissions=nextcord.Permissions(administrator=True),
|
||||
)
|
||||
async def post_highscore_groups(self, interaction: nextcord.Interaction) -> None:
|
||||
"""
|
||||
A command which posts the highscore in the registered channel.
|
||||
Sends warning if no channel has been registered.
|
||||
|
||||
Either creates new messages for each table if one doesn't exist
|
||||
or edits the existing message.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
"""
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
|
||||
# awaiting response because posting scores takes a while
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
|
||||
for group in session.scalars(stmt):
|
||||
channel = interaction.guild.get_channel(group.ChannelId)
|
||||
|
||||
# Error checking for registered highscore channel
|
||||
error_response = "Registered Highscores channel does not exist or was never registered. "
|
||||
error_response += 'Use "/register" to register channels.'
|
||||
assert channel is not None, await interaction.response(error_response, ephemeral=True)
|
||||
|
||||
message: nextcord.Message = await send_highscore_message(channel, group)
|
||||
await message.edit(view=SubmissionCreateButton(self.bot))
|
||||
|
||||
await interaction.send(f"Posted all groups in {interaction.guild.name}'s highscores.", ephemeral=True)
|
||||
|
||||
@nextcord.slash_command(
|
||||
description="Manually refresh a table. Use when changing settings or data manually changed.",
|
||||
default_member_permissions=nextcord.Permissions(administrator=True),
|
||||
)
|
||||
async def refresh_group(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Name of Group.", required=True),
|
||||
) -> None:
|
||||
"""
|
||||
Refreshes a table given by user.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): Str representation of Group id to refresh. Is SlashOption.
|
||||
"""
|
||||
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.GroupId == int(group_id))
|
||||
)
|
||||
group = session.scalar(stmt)
|
||||
|
||||
channel = interaction.guild.get_channel(group.ChannelId)
|
||||
|
||||
message: nextcord.Message = await send_highscore_message(channel, group)
|
||||
await message.edit(view=SubmissionCreateButton(self.bot))
|
||||
|
||||
await interaction.response.send_message(f"Updated table {group.GroupName}!", ephemeral=True)
|
||||
|
||||
@refresh_group.on_autocomplete("group_id")
|
||||
async def refresh_table_autofill(self, interaction: nextcord.Interaction, group_id: str):
|
||||
"""
|
||||
Collects list of table names and compares to input table name for autocomplete assistance.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction with command.
|
||||
group_id (str): String to run autocomplete compared to.
|
||||
"""
|
||||
|
||||
await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id))
|
||||
@@ -0,0 +1,104 @@
|
||||
"""
|
||||
Commands for refreshing highscore messages.
|
||||
"""
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from cogs.submission.submission_views import SubmissionCreateButton
|
||||
from utilities.sql_data_classes import CategoryGroup, engine
|
||||
from utilities.utils import autofill_groups, send_highscore_message
|
||||
|
||||
|
||||
class RefreshCog(commands.Cog, name="Refresh Cog"):
|
||||
"""
|
||||
Class holds commands for refreshing.
|
||||
"""
|
||||
|
||||
def __init__(self, bot: commands.Bot) -> None:
|
||||
"""
|
||||
Default constructor for RefreshCog class
|
||||
|
||||
Args:
|
||||
bot (commands.Bot): Bot object. Required to register submission button view.
|
||||
"""
|
||||
self._bot = bot
|
||||
|
||||
@nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True))
|
||||
async def refresh(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for refreshing things. Never called.
|
||||
"""
|
||||
|
||||
@refresh.subcommand(description="Manually refresh a table. Use when changing settings or data manually changed.")
|
||||
async def group(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(name="group", description="Name of Group.", required=True),
|
||||
) -> None:
|
||||
"""
|
||||
Refreshes a table given by user.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): Str representation of Group id to refresh. Is SlashOption.
|
||||
"""
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.GroupId == int(group_id))
|
||||
)
|
||||
group = session.scalar(stmt)
|
||||
|
||||
message: nextcord.Message = await send_highscore_message(session, interaction.guild, group)
|
||||
await message.edit(view=SubmissionCreateButton(self._bot))
|
||||
|
||||
await interaction.response.send_message(f"Updated table {group.GroupName}!", ephemeral=True)
|
||||
|
||||
@refresh.subcommand()
|
||||
async def all(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Subcommand parent for refreshing all things. Never called.
|
||||
"""
|
||||
|
||||
@all.subcommand(description="Triggers post/update of highscores to registered highscore channels.")
|
||||
async def groups(self, interaction: nextcord.Interaction) -> None:
|
||||
"""
|
||||
A command which posts the highscore in the registered channel.
|
||||
Sends warning if no channel has been registered.
|
||||
|
||||
Either creates new messages for each table if one doesn't exist
|
||||
or edits the existing message.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
"""
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
|
||||
# awaiting response because posting scores takes a while
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
|
||||
for group in session.scalars(stmt):
|
||||
message: nextcord.Message = await send_highscore_message(session, interaction.guild, group)
|
||||
await message.edit(view=SubmissionCreateButton(self._bot))
|
||||
|
||||
await interaction.send(f"Posted all groups in {interaction.guild.name}'s highscores.", ephemeral=True)
|
||||
|
||||
@group.on_autocomplete("group_id")
|
||||
async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str):
|
||||
"""
|
||||
Collects list of table names and compares to input table name for autocomplete assistance.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction with command.
|
||||
group_id (str): String to run autocomplete compared to.
|
||||
"""
|
||||
|
||||
await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id))
|
||||
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
Commands for managing the Guild's settings.
|
||||
Set command heirarchy.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
@@ -7,29 +7,30 @@ from typing import Optional
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_alchemy_connection import CategoryGroup, Guild, session
|
||||
from utilities.utils import autofill_groups
|
||||
from utilities.sql_data_classes import CategoryGroup, Guild, engine
|
||||
from utilities.utils import MAX_LINE_LENGTH, autofill_groups
|
||||
|
||||
|
||||
class SettingsManagementCog(commands.Cog, name="Settings Management"):
|
||||
class SetCog(commands.Cog, name="Set command."):
|
||||
"""
|
||||
Class holds commands for managing Guild's settings.
|
||||
Class holds commands for setting variables in guild's database.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Default constructor for SettingsManagement class. Does nothing.
|
||||
Default constructor for SetCog class. Does nothing.
|
||||
"""
|
||||
|
||||
@nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True))
|
||||
async def register(self, interaction: nextcord.Interaction):
|
||||
async def set(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command, never called.
|
||||
Parent slash command for setting settings. Never called.
|
||||
"""
|
||||
|
||||
@register.subcommand(description="Registers the channel for a Category Group's messages.")
|
||||
@set.subcommand(description="Registers the channel for a Category Group's messages.")
|
||||
async def group_output_channel(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
@@ -47,26 +48,67 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"):
|
||||
Command function to register a CategoryGroup's output channel.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String representation of Group id to register channel to. Is SlashOption.
|
||||
channel (nextcord.abc.GuildChannel, optional): Channel to register
|
||||
Is a SlashOption. Limited to text channels, Defaults to None.
|
||||
"""
|
||||
|
||||
if not channel:
|
||||
channel = interaction.channel
|
||||
channel_id = channel.id
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.GroupId == int(group_id))
|
||||
)
|
||||
group = session.scalar(stmt)
|
||||
group.ChannelId = channel_id
|
||||
session.commit()
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.GroupId == int(group_id))
|
||||
)
|
||||
group = session.scalar(stmt)
|
||||
group.ChannelId = channel_id
|
||||
session.commit()
|
||||
await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True)
|
||||
|
||||
@register.subcommand(description="Registers the channel for submission messages.")
|
||||
async def submission(
|
||||
@set.subcommand(
|
||||
description="Set character length for a category. Can cut off username/score. Affects all categories."
|
||||
)
|
||||
async def pad_length(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(
|
||||
name="group", description="Category Group to assign pad length to.", required=True
|
||||
),
|
||||
length: int = nextcord.SlashOption(
|
||||
description="Number of characters alloted to a category.",
|
||||
required=True,
|
||||
default=26,
|
||||
min_value=7,
|
||||
max_value=MAX_LINE_LENGTH,
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Changes guild's category pad length.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
group_id (str): String representation of Group id to set pad length in. Is SlashOption.
|
||||
length (int): Number of characters. Is SlashOption. Defaults to 26.
|
||||
"""
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.GroupId == int(group_id))
|
||||
)
|
||||
group = session.scalar(stmt)
|
||||
group.PadLength = length
|
||||
session.commit()
|
||||
await interaction.response.send_message(
|
||||
f"Category pad length set to {length} for {group.GroupName}", ephemeral=True
|
||||
)
|
||||
|
||||
@set.subcommand(description="Registers the channel for submission messages.")
|
||||
async def submission_channel(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption(
|
||||
@@ -84,17 +126,50 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"):
|
||||
channel Optional[nextcord.abc.GuildChannel], optional): Channel to register
|
||||
Is a SlashOption. Limited to text channels, Defaults to None.
|
||||
"""
|
||||
|
||||
if not channel:
|
||||
channel = interaction.channel
|
||||
channel_id = channel.id
|
||||
stmt = select(Guild).where(Guild.GuildId == interaction.guild_id)
|
||||
guild = session.scalar(stmt)
|
||||
guild.SubmissionChannel = channel_id
|
||||
session.commit()
|
||||
|
||||
with Session(engine) as session:
|
||||
channel_id = channel.id
|
||||
stmt = select(Guild).where(Guild.GuildId == interaction.guild_id)
|
||||
guild = session.scalar(stmt)
|
||||
guild.SubmissionChannel = channel_id
|
||||
session.commit()
|
||||
await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True)
|
||||
|
||||
@register.subcommand(description="Registers the channel for verification messages.")
|
||||
async def verify(
|
||||
@set.subcommand(description="Set username length for a Group of Categories.")
|
||||
async def username_length(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(
|
||||
name="group", description="Group to change username length of.", required=True
|
||||
),
|
||||
length: int = nextcord.SlashOption(
|
||||
description="Number of characters.", required=True, default=12, min_value=1, max_value=30
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Changes guild's username length limit.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
length (int): Number of characters. Is SlashOption. Defaults to 12.
|
||||
"""
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.GroupId == int(group_id))
|
||||
)
|
||||
group = session.scalar(stmt)
|
||||
group.UsernameLength = length
|
||||
session.commit()
|
||||
await interaction.response.send_message(f"Username length set to {length}.", ephemeral=True)
|
||||
|
||||
@set.subcommand(description="Registers the channel for verification messages.")
|
||||
async def verify_channel(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption(
|
||||
@@ -112,84 +187,21 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"):
|
||||
channel (Optional[nextcord.abc.GuildChannel]): Channel to register
|
||||
Is a SlashOption. Limited to text channels, Defaults to None.
|
||||
"""
|
||||
|
||||
if not channel:
|
||||
channel = interaction.channel
|
||||
channel_id = channel.id
|
||||
stmt = select(Guild).where(Guild.GuildId == interaction.guild_id)
|
||||
guild = session.scalar(stmt)
|
||||
guild.VerificationChannel = channel_id
|
||||
session.commit()
|
||||
with Session(engine) as session:
|
||||
stmt = select(Guild).where(Guild.GuildId == interaction.guild_id)
|
||||
guild = session.scalar(stmt)
|
||||
guild.VerificationChannel = channel_id
|
||||
session.commit()
|
||||
await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True)
|
||||
|
||||
@nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True))
|
||||
async def set(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for setting settings. Never called.
|
||||
"""
|
||||
|
||||
@set.subcommand(
|
||||
description="Set character length for a category. Can cut off username/score. Affects all categories."
|
||||
)
|
||||
async def category_pad_length(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(
|
||||
name="group", description="Category Group to assign pad length to.", required=True
|
||||
),
|
||||
length: int = nextcord.SlashOption(description="Number of characters.", required=True, default=26),
|
||||
) -> None:
|
||||
"""
|
||||
Changes guild's category pad length.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
group_id (str): String representation of Group id to set pad length in. Is SlashOption.
|
||||
length (int): Number of characters. Is SlashOption. Defaults to 26.
|
||||
"""
|
||||
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.GroupId == int(group_id))
|
||||
)
|
||||
group = session.scalar(stmt)
|
||||
group.PadLength = length
|
||||
session.commit()
|
||||
await interaction.response.send_message(
|
||||
f"Category pad length set to {length} for {group.GroupName}", ephemeral=True
|
||||
)
|
||||
|
||||
@set.subcommand(description="Set username length for a Group of Categories.")
|
||||
async def username_length(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(
|
||||
name="group", description="Group to change username length of.", required=True
|
||||
),
|
||||
length: int = nextcord.SlashOption(description="Number of characters.", required=True, default=12),
|
||||
) -> None:
|
||||
"""
|
||||
Changes guild's username length limit.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
length (int): Number of characters. Is SlashOption. Defaults to 12.
|
||||
"""
|
||||
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.GroupId == int(group_id))
|
||||
)
|
||||
group = session.scalar(stmt)
|
||||
group.UsernameLength = length
|
||||
session.commit()
|
||||
await interaction.response.send_message(f"Username length set to {length}.", ephemeral=True)
|
||||
|
||||
@group_output_channel.on_autocomplete("group_id")
|
||||
@category_pad_length.on_autocomplete("group_id")
|
||||
@pad_length.on_autocomplete("group_id")
|
||||
@username_length.on_autocomplete("group_id")
|
||||
async def group_id_autocomplete(self, interaction: nextcord.Interaction, group_id: str):
|
||||
async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str):
|
||||
"""
|
||||
Sends results from autofill_groups back to group_output_channel for autocompletion of group_id.
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
"""
|
||||
Commands for updating information/variables held in database.
|
||||
"""
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_data_classes import Guild, engine
|
||||
from utilities.utils import autofill_users_in_guild
|
||||
|
||||
from .views.update_username_verify import UpdateUsernameVerifyView
|
||||
|
||||
|
||||
class UpdateCog(commands.Cog, name="Update commands."):
|
||||
"""
|
||||
Class holds commands and views needed for updating things in bot.
|
||||
"""
|
||||
|
||||
def __init__(self, bot: commands.Bot):
|
||||
"""
|
||||
Default constructor for Update class. Sets internal bot variable.
|
||||
|
||||
Args:
|
||||
bot (commands.Bot): Bot object. Required to register update username button view.
|
||||
"""
|
||||
self._bot: commands.Bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
"""
|
||||
Called when cog is loaded. Adds views required for DataManagement commands.
|
||||
"""
|
||||
self._bot.add_view(UpdateUsernameVerifyView())
|
||||
|
||||
@nextcord.slash_command()
|
||||
async def update(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for updating things. Never called.
|
||||
"""
|
||||
|
||||
@update.subcommand(description="Request a username update. Admins must approve.")
|
||||
async def username(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
old_username: str = nextcord.SlashOption(description="Username currently stored in highscores.", required=True),
|
||||
new_username: str = nextcord.SlashOption(description="Username to update to.", required=True),
|
||||
):
|
||||
"""
|
||||
Creates a request to send to admins for changing a username. Uses
|
||||
UpdateUsernameVerifyView to do the work.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
old_username (str): Username to replace.
|
||||
new_username (str): Username to replace old with.
|
||||
"""
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = select(Guild.VerificationChannel).where(Guild.GuildId == interaction.guild_id)
|
||||
verify_channel = interaction.guild.get_channel(session.scalar(stmt))
|
||||
|
||||
embed: nextcord.Embed = nextcord.Embed(title="Username change request.")
|
||||
embed.add_field(name="Old Username", value=old_username)
|
||||
embed.add_field(name="New Username", value=new_username)
|
||||
await verify_channel.send(
|
||||
content=f"Username change request from <@{interaction.user.id}>.",
|
||||
embed=embed,
|
||||
view=UpdateUsernameVerifyView(),
|
||||
)
|
||||
await interaction.send("Request sent to admins for verification.", ephemeral=True)
|
||||
|
||||
@username.on_autocomplete("old_username")
|
||||
async def all_users_autocomplete(self, interaction: nextcord.Interaction, username: str):
|
||||
"""
|
||||
Sends response from autofill_users_in_guild back to update_username for autocompletion of old_username.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction with command.
|
||||
old_username (str): String to run autocompletion against.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(autofill_users_in_guild(interaction.guild_id, username))
|
||||
@@ -0,0 +1,112 @@
|
||||
"""
|
||||
View command heirarchy.
|
||||
"""
|
||||
|
||||
import nextcord
|
||||
import pandas as pd
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, Score, engine
|
||||
from utilities.utils import MAX_LINE_LENGTH, autofill_categories, autofill_groups, format_score_string
|
||||
|
||||
|
||||
class ViewCog(commands.Cog, name="View Command"):
|
||||
"""
|
||||
Class holds commands for viewing data.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Default constructor for ViewCog class. Does nothing.
|
||||
"""
|
||||
|
||||
@nextcord.slash_command()
|
||||
async def view(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for view. Never called.
|
||||
"""
|
||||
|
||||
@view.subcommand(description="Lists all scores for a given category.")
|
||||
async def leaderboard(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(
|
||||
name="group", description="Group to show leaderboard from.", required=True
|
||||
),
|
||||
category_id: str = nextcord.SlashOption(
|
||||
name="category", description="Category to show leaderboard.", required=True
|
||||
),
|
||||
):
|
||||
"""
|
||||
Gathers all users that have a submission in the group/category combo and returns a message with them
|
||||
in ranked order.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String representation of Group id to search from. Is SlashOption.
|
||||
category_id (str): String representation of Category id to show leaderboard. Is SlashOption.
|
||||
"""
|
||||
str_list: "list[str]" = []
|
||||
|
||||
# get group, category, and all scores sorted correctly.
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
|
||||
stmt = select(Category).where(Category.CategoryId == int(category_id))
|
||||
category = session.scalar(stmt)
|
||||
|
||||
d_f = pd.read_sql_query(
|
||||
sql=session.query(Score).where(Score.CategoryId == category_id).statement, con=engine
|
||||
)
|
||||
|
||||
scores = d_f.sort_values("Score", ascending=category.IsAscending).reset_index()
|
||||
|
||||
# get the longest string
|
||||
longest_str: int = scores.UserName.map(len).max()
|
||||
|
||||
for i, row in scores.iterrows():
|
||||
str_list.append(
|
||||
format_score_string(
|
||||
row["UserName"], row["Score"], longest_str, category.IsTimeRecord, i + 1, MAX_LINE_LENGTH
|
||||
)
|
||||
)
|
||||
|
||||
message_str: str = f"**{group.GroupName} {category.CategoryName} Leaderboard!**\n```"
|
||||
if len(str_list) == 0:
|
||||
message_str += "No Submissions!"
|
||||
else:
|
||||
for line in str_list:
|
||||
message_str += f"{line}\n"
|
||||
|
||||
message_str += "```"
|
||||
|
||||
await interaction.response.send_message(content=message_str)
|
||||
|
||||
@leaderboard.on_autocomplete("group_id")
|
||||
async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str):
|
||||
"""
|
||||
Autocomplete function for table names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String to be autocompleted with Group name.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id))
|
||||
|
||||
@leaderboard.on_autocomplete("category_id")
|
||||
async def category_autocomplete(self, interaction: nextcord.Interaction, category_id: str, group_id: str):
|
||||
"""
|
||||
Autocomplete function for category names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
category_id (str): String to be autocompleted with Category name.
|
||||
group_id (str): Group to find Category from.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(
|
||||
autofill_categories(interaction.guild_id, int(group_id), category_id)
|
||||
)
|
||||
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
Contains view and helper functions for confirming and processing deletes.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import nextcord
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, engine
|
||||
from utilities.utils import send_highscore_message
|
||||
|
||||
|
||||
class DeleteConfirmView(nextcord.ui.Modal):
|
||||
"""
|
||||
View modal popup for confirming deletions.
|
||||
"""
|
||||
|
||||
def __init__(self, custom_id: str):
|
||||
super().__init__(title="Warning, deletion is perminant.", custom_id=custom_id)
|
||||
|
||||
self.checker = nextcord.ui.TextInput(
|
||||
label='Type "delete" to confirm deletion.',
|
||||
required=True,
|
||||
placeholder="delete",
|
||||
style=nextcord.TextInputStyle.short,
|
||||
)
|
||||
self.add_item(self.checker)
|
||||
|
||||
async def callback(self, interaction: nextcord.Interaction):
|
||||
if self.checker.value.lower() != "delete":
|
||||
await interaction.response.send_message('Response was not "delete". Canceling delete.')
|
||||
return
|
||||
|
||||
del_vals = parse_delete_id(self.custom_id)
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == del_vals["group_id"])
|
||||
group = session.scalar(stmt)
|
||||
if del_vals["category_id"] is None:
|
||||
# delete entire group
|
||||
session.delete(group)
|
||||
# delete the message related to group
|
||||
channel: nextcord.TextChannel = interaction.guild.get_channel(group.ChannelId)
|
||||
message: nextcord.Message = await channel.fetch_message(group.MessageId)
|
||||
await message.delete()
|
||||
|
||||
else:
|
||||
stmt = (
|
||||
delete(Category)
|
||||
.where(Category.GroupId == del_vals["group_id"])
|
||||
.where(Category.CategoryId == del_vals["category_id"])
|
||||
)
|
||||
session.execute(stmt)
|
||||
# update the message
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
session.commit()
|
||||
|
||||
await interaction.response.send_message("Deletion complete.", ephemeral=True)
|
||||
|
||||
|
||||
def create_delete_id(group_id: int, category_id: Optional[int] = None) -> str:
|
||||
"""
|
||||
Creates a delete id string to tell the view what to delete.
|
||||
|
||||
Args:
|
||||
group_id (int): group id to delete
|
||||
category_id (Optional[int]): category id to delete. Can be None.
|
||||
|
||||
Returns:
|
||||
str: string representation of the delete id.
|
||||
"""
|
||||
return f"delete-{group_id}-{category_id}"
|
||||
|
||||
|
||||
def parse_delete_id(delete_id: str) -> dict:
|
||||
"""
|
||||
Returns a dict containing the values of a delete id.
|
||||
|
||||
Args:
|
||||
id (str): the delete id
|
||||
|
||||
Returns:
|
||||
dict: dict containing values from a delete id.
|
||||
"""
|
||||
vals = {}
|
||||
group_id = delete_id.split("-")[1]
|
||||
category_id = delete_id.split("-")[2]
|
||||
vals["group_id"] = group_id
|
||||
if category_id == "None":
|
||||
vals["category_id"] = None
|
||||
else:
|
||||
vals["category_id"] = category_id
|
||||
|
||||
return vals
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
import nextcord
|
||||
from sqlalchemy import distinct, exc, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_alchemy_connection import Category, CategoryGroup, Score, session
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, Score, engine
|
||||
from utilities.utils import send_highscore_message
|
||||
|
||||
|
||||
@@ -43,32 +44,33 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
|
||||
interaction (nextcord.Interaction): Interaction with the button
|
||||
"""
|
||||
|
||||
try:
|
||||
embed_content = interaction.message.embeds[0].to_dict()
|
||||
old_username: str = embed_content["fields"][0]["value"]
|
||||
new_username: str = embed_content["fields"][1]["value"]
|
||||
with Session(engine) as session:
|
||||
try:
|
||||
embed_content = interaction.message.embeds[0].to_dict()
|
||||
old_username: str = embed_content["fields"][0]["value"]
|
||||
new_username: str = embed_content["fields"][1]["value"]
|
||||
|
||||
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == old_username)
|
||||
category_ids: "list[int]" = list(session.scalars(stmt))
|
||||
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == old_username)
|
||||
category_ids: "list[int]" = list(session.scalars(stmt))
|
||||
|
||||
stmt = select(Score).where(Score.UserName == old_username)
|
||||
for score in session.scalars(stmt):
|
||||
score.UserName = new_username
|
||||
session.commit()
|
||||
stmt = select(Score).where(Score.UserName == old_username)
|
||||
for score in session.scalars(stmt):
|
||||
score.UserName = new_username
|
||||
session.commit()
|
||||
|
||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids))
|
||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids))
|
||||
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
||||
|
||||
for group in session.scalars(stmt):
|
||||
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
|
||||
for group in session.scalars(stmt):
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
|
||||
content = interaction.message.content
|
||||
content += f"\nApproved by <@{interaction.user.id}>."
|
||||
await interaction.message.edit(content=content, embeds=interaction.message.embeds, view=None)
|
||||
except exc.DataError:
|
||||
session.rollback()
|
||||
content = interaction.message.content
|
||||
content += "\nError, UserName is too long."
|
||||
await interaction.message.edit(content=content, embeds=interaction.message.embeds, view=None)
|
||||
await interaction.response.send_message(content="User must resubmit with shorter name.", ephemeral=True)
|
||||
content = interaction.message.content
|
||||
content += f"\nApproved by <@{interaction.user.id}>."
|
||||
await interaction.message.edit(content=content, embeds=interaction.message.embeds, view=None)
|
||||
except exc.DataError:
|
||||
session.rollback()
|
||||
content = interaction.message.content
|
||||
content += "\nError, UserName is too long."
|
||||
await interaction.message.edit(content=content, embeds=interaction.message.embeds, view=None)
|
||||
await interaction.response.send_message(content="User must resubmit with shorter name.", ephemeral=True)
|
||||
|
||||
@@ -4,10 +4,10 @@ Defines cog for guild listeners. Contains functions for various guild events.
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_alchemy_connection import Guild, session
|
||||
from utilities.utils import add_preset_configuration
|
||||
from utilities.sql_data_classes import Guild, engine
|
||||
|
||||
|
||||
class GuildListener(commands.Cog, name="Guild Listener"):
|
||||
@@ -18,19 +18,13 @@ class GuildListener(commands.Cog, name="Guild Listener"):
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_guild_join(self, guild: nextcord.Guild):
|
||||
"""Listener for Guild Join. Creates skeleton for new guild joined and saves it."""
|
||||
new_guild = Guild(GuildId=guild.id)
|
||||
session.add(new_guild)
|
||||
session.commit()
|
||||
# This add config will be removed eventually. Config adding will have to be controlled by commands.
|
||||
add_preset_configuration(guild.id, "runescape")
|
||||
"""Listener for Guild Join. Adds guild to database."""
|
||||
|
||||
welcome_message: str = "Hello, I keep track of Highscores for you!"
|
||||
welcome_message += " Currently only supporting RuneScape boss highscores,"
|
||||
welcome_message += " but will be more dynamic support in future."
|
||||
welcome_message += ' For assistance on getting started, use "?register help".'
|
||||
new_guild = Guild(GuildId=guild.id, GuildName=guild.name)
|
||||
|
||||
await guild.system_channel.send(content=welcome_message)
|
||||
with Session(engine) as session:
|
||||
session.add(new_guild)
|
||||
session.commit()
|
||||
|
||||
|
||||
# This function will be called when this extension is loaded.
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
Initializing code for all loops.
|
||||
"""
|
||||
|
||||
from nextcord.ext import commands
|
||||
|
||||
from .bot_presence import BotPresence
|
||||
from .guild_data import GuildData
|
||||
|
||||
|
||||
# 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 function to bot"""
|
||||
bot.add_cog(BotPresence(bot))
|
||||
bot.add_cog(GuildData(bot))
|
||||
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
Functions for controlling the bot's presence.
|
||||
Functions for controlling the bot's presence. Also contains the loop.
|
||||
"""
|
||||
|
||||
import random
|
||||
@@ -46,10 +46,3 @@ class BotPresence(commands.Cog, name="Bot Presence"):
|
||||
status += f" | {len(self.bot.guilds)} servers"
|
||||
|
||||
await self.bot.change_presence(activity=nextcord.Game(status))
|
||||
|
||||
|
||||
# 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 function to bot"""
|
||||
bot.add_cog(BotPresence(bot))
|
||||
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
Contains a loop that updates guild data in the database periodically.
|
||||
"""
|
||||
|
||||
from nextcord.ext import commands, tasks
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_data_classes import Guild, engine
|
||||
|
||||
|
||||
class GuildData(commands.Cog, name="Guild Data"):
|
||||
"""Class to contain loop."""
|
||||
|
||||
def __init__(self, bot: commands.bot) -> None:
|
||||
self.bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
"""Starts the loop when bot is ready."""
|
||||
await self.bot.wait_until_ready()
|
||||
# pylint doesn't recognize that periodic_update_guild_data is subclassed from tasks.loop.
|
||||
await self.periodic_update_guild_data.start() # pylint: disable=no-member
|
||||
|
||||
@tasks.loop(hours=24)
|
||||
async def periodic_update_guild_data(self):
|
||||
"""Loops every day, calls update_guild_data."""
|
||||
self.update_guild_data()
|
||||
|
||||
def update_guild_data(self):
|
||||
"""
|
||||
Updates all known guild's data in the database.
|
||||
"""
|
||||
with Session(engine) as session:
|
||||
for guild in self.bot.guilds:
|
||||
# update given guild name for guild id
|
||||
stmt = select(Guild).where(Guild.GuildId == guild.id)
|
||||
guild_data = session.scalar(stmt)
|
||||
|
||||
# just in case checking if the guild doesn't exist.
|
||||
if guild_data is None:
|
||||
new_guild = Guild(GuildId=guild.id, GuildName=guild.name)
|
||||
session.add(new_guild)
|
||||
else:
|
||||
guild_data.GuildName = guild.name
|
||||
|
||||
session.commit()
|
||||
@@ -7,12 +7,12 @@ import re
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from classes.submission import SubmissionState
|
||||
from highscores import submission_objects
|
||||
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, session
|
||||
from utilities.sql_data_classes import Category, Guild, engine
|
||||
|
||||
|
||||
class SubmissionListener(commands.Cog, name="Message Listener"):
|
||||
@@ -33,66 +33,68 @@ class SubmissionListener(commands.Cog, name="Message Listener"):
|
||||
if submission_objects.has_user(message.author.id):
|
||||
submission = submission_objects.get_user_submission(message.author.id)
|
||||
message_id = submission.message_id
|
||||
stmt = select(Guild.SubmissionChannel).where(Guild.GuildId == guild_id)
|
||||
submission_channel_id = session.scalar(stmt)
|
||||
submission_message: nextcord.Message = (
|
||||
await self.bot.get_guild(guild_id).get_channel(submission_channel_id).fetch_message(message_id)
|
||||
)
|
||||
|
||||
# need check if correct user and correct channel
|
||||
if message.channel.id == submission_channel_id:
|
||||
state = submission.submission_state
|
||||
if state == SubmissionState.SCORE and message.content:
|
||||
with Session(engine) as session:
|
||||
stmt = select(Guild.SubmissionChannel).where(Guild.GuildId == guild_id)
|
||||
submission_channel_id = session.scalar(stmt)
|
||||
submission_message: nextcord.Message = (
|
||||
await self.bot.get_guild(guild_id).get_channel(submission_channel_id).fetch_message(message_id)
|
||||
)
|
||||
|
||||
stmt = select(Category.IsTimeRecord).where(Category.CategoryId == submission.category_id)
|
||||
# need check if correct user and correct channel
|
||||
if message.channel.id == submission_channel_id:
|
||||
state = submission.submission_state
|
||||
if state == SubmissionState.SCORE and message.content:
|
||||
|
||||
# check with regex if score matches correctly
|
||||
is_time_record = session.scalar(stmt)
|
||||
stmt = select(Category.IsTimeRecord).where(Category.CategoryId == submission.category_id)
|
||||
|
||||
time_regex = re.search("^([0-9]{1,2}:[0-9]|[0-9])?[0-9](\\.[0-9])?$", message.content)
|
||||
int_regex = re.search("^[0-9]+$", message.content)
|
||||
if time_regex is None and is_time_record:
|
||||
embeds = submission_message.embeds[0]
|
||||
embeds.add_field(
|
||||
name="Error",
|
||||
value="Time record must follow format MM:SS.ms, SS.ms, or SS.",
|
||||
# check with regex if score matches correctly
|
||||
is_time_record = session.scalar(stmt)
|
||||
|
||||
time_regex = re.search("^([0-9]{1,2}:[0-9]|[0-9])?[0-9](\\.[0-9])?$", message.content)
|
||||
int_regex = re.search("^[0-9]+$", message.content)
|
||||
if time_regex is None and is_time_record:
|
||||
embeds = submission_message.embeds[0]
|
||||
embeds.add_field(
|
||||
name="Error",
|
||||
value="Time record must follow format MM:SS.ms, SS.ms, or SS.",
|
||||
)
|
||||
elif int_regex is None and not is_time_record:
|
||||
embeds = submission_message.embeds[0]
|
||||
embeds.add_field(name="Error", value="Number record must only be number.")
|
||||
else:
|
||||
score = message.content
|
||||
if is_time_record:
|
||||
# standardize time score format.
|
||||
if re.search("^[1-9]?[0-9]:[0-9]{2}(\\.[0-9])?$", score):
|
||||
# score is already fine.
|
||||
pass
|
||||
elif re.search("^[0-9]{2}(\\.[0-9])?$", score):
|
||||
# need to add leading colon and 0
|
||||
score = "0:" + score
|
||||
elif re.search("^[0-9](\\.[0-9])?$", score):
|
||||
# need to add 0 in seconds and leading colon and 0
|
||||
score = "0:0" + score
|
||||
elif re.search("^[0]?[0-9]:[0-9]{2}(\\.[0-9])?$", score):
|
||||
# if there is a minute 10ths place 0, remove
|
||||
score = score.lstrip("0")
|
||||
submission.score = score
|
||||
embeds = submission.get_embeds()
|
||||
await submission_message.edit(
|
||||
embeds=embeds,
|
||||
)
|
||||
elif int_regex is None and not is_time_record:
|
||||
embeds = submission_message.embeds[0]
|
||||
embeds.add_field(name="Error", value="Number record must only be number.")
|
||||
else:
|
||||
score = message.content
|
||||
if is_time_record:
|
||||
# standardize time score format.
|
||||
if re.search("^[1-9]?[0-9]:[0-9]{2}(\\.[0-9])?$", score):
|
||||
# score is already fine.
|
||||
pass
|
||||
elif re.search("^[0-9]{2}(\\.[0-9])?$", score):
|
||||
# need to add leading colon and 0
|
||||
score = "0:" + score
|
||||
elif re.search("^[0-9](\\.[0-9])?$", score):
|
||||
# need to add 0 in seconds and leading colon and 0
|
||||
score = "0:0" + score
|
||||
elif re.search("^[0]?[0-9]:[0-9]{2}(\\.[0-9])?$", score):
|
||||
# if there is a minute 10ths place 0, remove
|
||||
score = score.lstrip("0")
|
||||
submission.score = score
|
||||
elif state == SubmissionState.PROOF and len(message.attachments) > 0:
|
||||
await submission_message.edit(
|
||||
files=[await attch.to_file() for attch in message.attachments],
|
||||
)
|
||||
elif state == SubmissionState.NAME and message.content:
|
||||
submission.username = message.content
|
||||
embeds = submission.get_embeds()
|
||||
await submission_message.edit(
|
||||
embeds=embeds,
|
||||
)
|
||||
elif state == SubmissionState.PROOF and len(message.attachments) > 0:
|
||||
await submission_message.edit(
|
||||
files=[await attch.to_file() for attch in message.attachments],
|
||||
)
|
||||
elif state == SubmissionState.NAME and message.content:
|
||||
submission.username = message.content
|
||||
embeds = submission.get_embeds()
|
||||
await submission_message.edit(
|
||||
embeds=embeds,
|
||||
)
|
||||
await submission_message.edit(
|
||||
embeds=embeds,
|
||||
)
|
||||
|
||||
await message.delete()
|
||||
await message.delete()
|
||||
|
||||
|
||||
# This function will be called when this extension is loaded.
|
||||
|
||||
@@ -5,12 +5,12 @@ from typing import Optional
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.orm import Session, aliased
|
||||
|
||||
# pylint: disable=import-error
|
||||
from classes.submission import Submission, SubmissionState
|
||||
from utilities.sql_alchemy_connection import Category, Guild, CategoryGroup, session
|
||||
from highscores import submission_objects
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, Guild, engine
|
||||
|
||||
from .verification_view import VerificationView
|
||||
|
||||
@@ -18,12 +18,14 @@ from .verification_view import VerificationView
|
||||
def category_select_options(group_id: int) -> "list[nextcord.SelectOption]":
|
||||
"""Returns list of categories for a given boss."""
|
||||
options: "list[nextcord.SelectOption]" = []
|
||||
subq = select(CategoryGroup).where(CategoryGroup.GroupId == group_id).subquery()
|
||||
cat_group_alias = aliased(CategoryGroup, subq)
|
||||
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId)
|
||||
categories_list = session.scalars(stmt)
|
||||
for category in categories_list:
|
||||
options.append(nextcord.SelectOption(label=category.CategoryName, value=str(category.CategoryId)))
|
||||
|
||||
with Session(engine) as session:
|
||||
subq = select(CategoryGroup).where(CategoryGroup.GroupId == group_id).subquery()
|
||||
cat_group_alias = aliased(CategoryGroup, subq)
|
||||
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId)
|
||||
categories_list = session.scalars(stmt)
|
||||
for category in categories_list:
|
||||
options.append(nextcord.SelectOption(label=category.CategoryName, value=str(category.CategoryId)))
|
||||
|
||||
return options
|
||||
|
||||
@@ -110,8 +112,9 @@ class SubmitButton(nextcord.ui.Button):
|
||||
self._bot = bot
|
||||
|
||||
async def callback(self, interaction: nextcord.Interaction):
|
||||
stmt = select(Guild.VerificationChannel).where(Guild.GuildId == interaction.guild_id)
|
||||
verification_channel_id = session.scalar(stmt)
|
||||
with Session(engine) as session:
|
||||
stmt = select(Guild.VerificationChannel).where(Guild.GuildId == interaction.guild_id)
|
||||
verification_channel_id = session.scalar(stmt)
|
||||
channel = interaction.guild.get_channel(verification_channel_id)
|
||||
submission = submission_objects.get_user_submission(interaction.user.id)
|
||||
|
||||
@@ -148,20 +151,22 @@ class SubmissionCreateButton(nextcord.ui.View):
|
||||
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
|
||||
): # pylint: disable=unused-argument
|
||||
"""Button for score submission."""
|
||||
|
||||
# Don't allow a user to start a new submission if one already exists
|
||||
if submission_objects.has_user(interaction.user.id):
|
||||
await interaction.send("Please finish in progress submission first.", ephemeral=True)
|
||||
return
|
||||
|
||||
stmt = (
|
||||
select(CategoryGroup.GroupId)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.MessageId == interaction.message.id)
|
||||
)
|
||||
group_id = session.scalar(stmt)
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(CategoryGroup.GroupId)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.MessageId == interaction.message.id)
|
||||
)
|
||||
group_id = session.scalar(stmt)
|
||||
|
||||
stmt = select(Guild.SubmissionChannel).where(Guild.GuildId == interaction.guild_id)
|
||||
channel = interaction.guild.get_channel(session.scalar(stmt))
|
||||
stmt = select(Guild.SubmissionChannel).where(Guild.GuildId == interaction.guild_id)
|
||||
channel = interaction.guild.get_channel(session.scalar(stmt))
|
||||
|
||||
# a user should only have 1 submission active at once.
|
||||
# split username on pipe. Decided to store whole username...
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
version: "3"
|
||||
services:
|
||||
sqldb:
|
||||
restart: unless-stopped
|
||||
image: mysql:latest
|
||||
container_name: "Highscores-DataBase"
|
||||
volumes:
|
||||
- highscores_data:/var/lib/mysql
|
||||
ports:
|
||||
- 3306:3306
|
||||
environment:
|
||||
- MYSQL_ROOT_PASSWORD=${SQL_PASS}
|
||||
- MYSQL_ROOT_USERNAME=${SQL_USER}
|
||||
|
||||
bot:
|
||||
restart: unless-stopped
|
||||
build: .
|
||||
container_name: "Highscores-Bot"
|
||||
depends_on:
|
||||
- sqldb
|
||||
environment:
|
||||
- DISCORD_TOKEN=${DISCORD_TOKEN}
|
||||
- SQL_DB=${SQL_DB}
|
||||
- SQL_HOST=${SQL_HOST}
|
||||
- SQL_PASS=${SQL_PASS}
|
||||
- SQL_USER=${SQL_USER}
|
||||
|
||||
volumes:
|
||||
highscores_data:
|
||||
@@ -1,3 +0,0 @@
|
||||
FROM mysql:latest
|
||||
|
||||
COPY highscores_database.sql /docker-entrypoint-initdb.d/
|
||||
+2
-1
@@ -9,7 +9,8 @@ import sys
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
|
||||
import utilities.config as config
|
||||
# pylint: disable=import-error
|
||||
from utilities import config
|
||||
from classes.submission import SubmissionMap
|
||||
|
||||
logger = logging.getLogger("nextcord")
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
version: "3"
|
||||
services:
|
||||
sqldb:
|
||||
image: "highscores_db"
|
||||
container_name: "Highscores-DataBase"
|
||||
volumes:
|
||||
- highscores_data:/var/lib/mysql
|
||||
ports:
|
||||
- 3306:3306
|
||||
environment:
|
||||
- MYSQL_ROOT_PASSWORD=${SQL_PASS}
|
||||
- MYSQL_ROOT_USERNAME=${SQL_USER}
|
||||
|
||||
volumes:
|
||||
highscores_data:
|
||||
@@ -3,6 +3,7 @@ use highscores;
|
||||
|
||||
CREATE TABLE Guilds(
|
||||
GuildId bigint unsigned not null unique,
|
||||
GuildName varchar(95) not null,
|
||||
SubmissionChannel bigint unsigned,
|
||||
VerificationChannel bigint unsigned,
|
||||
PRIMARY KEY (GuildId)
|
||||
@@ -17,7 +18,7 @@ CREATE TABLE CategoryGroups(
|
||||
PadLength smallint not null DEFAULT 26,
|
||||
UsernameLength smallint DEFAULT 12,
|
||||
CONSTRAINT UniqueGuildCategoryGroup UNIQUE (GuildId, GroupName),
|
||||
FOREIGN KEY (GuildId) REFERENCES Guilds(GuildId),
|
||||
FOREIGN KEY (GuildId) REFERENCES Guilds(GuildId) ON DELETE CASCADE,
|
||||
PRIMARY KEY (GroupId)
|
||||
);
|
||||
|
||||
@@ -30,7 +31,7 @@ CREATE TABLE Categories(
|
||||
IsTimeRecord boolean not null DEFAULT false,
|
||||
IsValidated boolean not null DEFAULT true,
|
||||
CONSTRAINT UniqueCategoryInGroup UNIQUE (GroupId, CategoryName),
|
||||
FOREIGN KEY (GroupId) REFERENCES CategoryGroups(GroupId),
|
||||
FOREIGN KEY (GroupId) REFERENCES CategoryGroups(GroupId) ON DELETE CASCADE,
|
||||
PRIMARY Key (CategoryId)
|
||||
);
|
||||
|
||||
@@ -38,6 +39,6 @@ CREATE TABLE Scores(
|
||||
UserName varchar(30) not null,
|
||||
Score float not null, -- Maybe should have time and int scores?? timestamp type?
|
||||
CategoryId int unsigned not null,
|
||||
FOREIGN KEY (CategoryId) REFERENCES Categories(CategoryId),
|
||||
FOREIGN KEY (CategoryId) REFERENCES Categories(CategoryId) ON DELETE CASCADE,
|
||||
PRIMARY KEY (CategoryId, UserName)
|
||||
);
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
nextcord>=2.3.2
|
||||
python-dotenv>=0.20.0
|
||||
pandas>=1.5.2
|
||||
sqlalchemy>=1.4.44
|
||||
mysqlclient>-2.1.1
|
||||
sqlalchemy<2.0
|
||||
pymysql>=1.0.2
|
||||
cryptography>=2.8
|
||||
@@ -3,17 +3,15 @@ Simple file to set up the sqlalchemy connection to database.
|
||||
"""
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.automap import automap_base
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
import utilities.config as config
|
||||
# pylint: disable=import-error
|
||||
from utilities import config
|
||||
|
||||
# connect with sql alchemy and autoload schema
|
||||
url = f"mysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
||||
url = f"mysql+pymysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
||||
engine = create_engine(url)
|
||||
connection = engine.connect()
|
||||
Base = automap_base()
|
||||
Base.prepare(autoload_with=engine)
|
||||
session = Session(engine)
|
||||
|
||||
# define the classes
|
||||
Guild = Base.classes.Guilds
|
||||
+136
-107
@@ -8,10 +8,10 @@ import time
|
||||
import nextcord
|
||||
import pandas as pd
|
||||
from sqlalchemy import distinct, select
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.orm import Session, aliased
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, Score, engine, session
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, Guild, Score, engine
|
||||
|
||||
MAX_LINE_LENGTH = 142
|
||||
|
||||
@@ -30,16 +30,17 @@ def autofill_categories(guild_id: int, group_id: int, category_begin: str) -> "d
|
||||
dict[str, str]: List of all category names that start with category_begin.
|
||||
"""
|
||||
|
||||
# search database for all category names
|
||||
subq = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == guild_id)
|
||||
.where(CategoryGroup.GroupId == group_id)
|
||||
.subquery()
|
||||
)
|
||||
cat_group_alias = aliased(CategoryGroup, subq)
|
||||
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId)
|
||||
categories_dict: dict = {cat.CategoryName: str(cat.CategoryId) for cat in session.scalars(stmt)}
|
||||
with Session(engine) as session:
|
||||
# search database for all category names
|
||||
subq = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == guild_id)
|
||||
.where(CategoryGroup.GroupId == group_id)
|
||||
.subquery()
|
||||
)
|
||||
cat_group_alias = aliased(CategoryGroup, subq)
|
||||
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId)
|
||||
categories_dict: dict = {cat.CategoryName: str(cat.CategoryId) for cat in session.scalars(stmt)}
|
||||
|
||||
# if string empty, return all category names
|
||||
if not category_begin:
|
||||
@@ -65,11 +66,12 @@ def autofill_groups(guild_id: int, group_begin: str) -> "dict[str, str]":
|
||||
dict[str, str]: Dict of group names and ids where group name start with group_begin. Key is name, value is id.
|
||||
"""
|
||||
|
||||
# Select all groups in guild.
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == guild_id)
|
||||
with Session(engine) as session:
|
||||
# Select all groups in guild.
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == guild_id)
|
||||
|
||||
# dict has to have key values of same type for discord.
|
||||
group_dict: dict = {group.GroupName: str(group.GroupId) for group in session.scalars(stmt)}
|
||||
# dict has to have key values of same type for discord.
|
||||
group_dict: dict = {group.GroupName: str(group.GroupId) for group in session.scalars(stmt)}
|
||||
|
||||
# if string empty, return all group names
|
||||
if not group_begin:
|
||||
@@ -98,32 +100,33 @@ def autofill_users_in_group_category(guild_id: int, group_id: int, category_id:
|
||||
list[str]: List of users that start with user_begin.
|
||||
"""
|
||||
|
||||
# get all categories in the group
|
||||
subq = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == guild_id)
|
||||
.where(CategoryGroup.GroupId == group_id)
|
||||
.subquery()
|
||||
)
|
||||
category_group = aliased(CategoryGroup, subq)
|
||||
|
||||
# If no category id, get all users in group.
|
||||
if category_id == 0:
|
||||
subq = select(Category.CategoryId).where(Category.GroupId == category_group.GroupId)
|
||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId.in_(subq))
|
||||
|
||||
else:
|
||||
# get all users in category_id
|
||||
with Session(engine) as session:
|
||||
# get all categories in the group
|
||||
subq = (
|
||||
select(Category)
|
||||
.where(Category.GroupId == category_group.GroupId)
|
||||
.where(Category.CategoryId == category_id)
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == guild_id)
|
||||
.where(CategoryGroup.GroupId == group_id)
|
||||
.subquery()
|
||||
)
|
||||
category = aliased(Category, subq)
|
||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId == category.CategoryId)
|
||||
category_group = aliased(CategoryGroup, subq)
|
||||
|
||||
users_list: "list[str]" = list(session.scalars(stmt))
|
||||
# If no category id, get all users in group.
|
||||
if category_id == 0:
|
||||
subq = select(Category.CategoryId).where(Category.GroupId == category_group.GroupId)
|
||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId.in_(subq))
|
||||
|
||||
else:
|
||||
# get all users in category_id
|
||||
subq = (
|
||||
select(Category)
|
||||
.where(Category.GroupId == category_group.GroupId)
|
||||
.where(Category.CategoryId == category_id)
|
||||
.subquery()
|
||||
)
|
||||
category = aliased(Category, subq)
|
||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId == category.CategoryId)
|
||||
|
||||
users_list: "list[str]" = list(session.scalars(stmt))
|
||||
|
||||
# if string empty, return all users
|
||||
if not user_begin:
|
||||
@@ -146,10 +149,12 @@ def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]":
|
||||
Returns:
|
||||
list[str]: All users with submissions in the guild.
|
||||
"""
|
||||
group_ids = select(CategoryGroup.GroupId).where(CategoryGroup.GuildId == guild_id)
|
||||
category_ids = select(Category.CategoryId).where(Category.GroupId.in_(group_ids))
|
||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId.in_(category_ids))
|
||||
users_list: "list[str]" = list(session.scalars(stmt))
|
||||
|
||||
with Session(engine) as session:
|
||||
group_ids = select(CategoryGroup.GroupId).where(CategoryGroup.GuildId == guild_id)
|
||||
category_ids = select(Category.CategoryId).where(Category.GroupId.in_(group_ids))
|
||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId.in_(category_ids))
|
||||
users_list: "list[str]" = list(session.scalars(stmt))
|
||||
|
||||
# if string empty, return all users
|
||||
if not user_begin:
|
||||
@@ -161,18 +166,20 @@ def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]":
|
||||
return users
|
||||
|
||||
|
||||
def format_highscore_message(group: CategoryGroup) -> str:
|
||||
def format_highscore_message(session: Session, group: CategoryGroup) -> str:
|
||||
"""
|
||||
Creates a formatted string for a boss's highscore discord message.
|
||||
This formatted string is in discord's code block to ensure monospace text.
|
||||
Loops through each category to build the message one place at a time.
|
||||
|
||||
Args:
|
||||
group (group): group object that contains the contents of the message.
|
||||
session (Session): the sqlalchemy session used for this function call.
|
||||
group (CategoryGroup): group object that contains the contents of the message.
|
||||
|
||||
Returns:
|
||||
str: Formatted string for discord message
|
||||
"""
|
||||
|
||||
ret_string = f"**{group.GroupName}**\n```"
|
||||
|
||||
stmt = select(Guild).where(Guild.GuildId == group.GuildId)
|
||||
@@ -224,20 +231,12 @@ def format_highscore_message(group: CategoryGroup) -> str:
|
||||
# add each score to list
|
||||
for ind in range(max_cat_size): # need to subtract to deal with category name string
|
||||
if ind in processed_group.index:
|
||||
user_length = group.UsernameLength
|
||||
username = processed_group["UserName"][ind][:user_length].ljust(user_length, " ")
|
||||
username = processed_group["UserName"][ind]
|
||||
score = processed_group["Score"][ind]
|
||||
|
||||
# get score, pad to correct length
|
||||
if category.IsTimeRecord:
|
||||
# MM:SS.ms is 7 characters long
|
||||
# if there are single digit minutes, add a space for formatting.
|
||||
score = get_time_str(processed_group["Score"][ind])
|
||||
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
|
||||
score = " " + score
|
||||
else:
|
||||
score = str(int(processed_group["Score"][ind])).rjust(7, " ")
|
||||
|
||||
str_list[ind + 1] += f"{ind + 1}: {username} - {score}".ljust(pad_length, " ")
|
||||
str_list[ind + 1] += format_score_string(
|
||||
username, score, group.UsernameLength, category.IsTimeRecord, ind + 1, pad_length
|
||||
)
|
||||
else:
|
||||
# if not enough entries, pad out
|
||||
if ind + 1 <= category.Size:
|
||||
@@ -255,7 +254,6 @@ def format_highscore_message(group: CategoryGroup) -> str:
|
||||
# need to use index to allow for modifying the list
|
||||
for ind, line in enumerate(str_list):
|
||||
if len(line) > MAX_LINE_LENGTH:
|
||||
|
||||
split_loc = int(MAX_LINE_LENGTH / pad_length) * pad_length
|
||||
new_line = line[split_loc:]
|
||||
str_list[ind] = line[:split_loc]
|
||||
@@ -276,6 +274,38 @@ def format_highscore_message(group: CategoryGroup) -> str:
|
||||
return ret_string + "```"
|
||||
|
||||
|
||||
def format_score_string(
|
||||
username: str, score: float, username_length: int, is_time_record: bool, rank: int, pad_length: int
|
||||
) -> str:
|
||||
"""
|
||||
Builds the string to show a user's score.
|
||||
|
||||
Args:
|
||||
username (str): username.
|
||||
score (int): score in int form.
|
||||
username_length (float): limit to how long username is allowed to be.
|
||||
is_time_record (bool): if the score represents an number or a time record.
|
||||
rank (int): the user's rank.
|
||||
pad_length (int): limit to how long the string can be.
|
||||
|
||||
Returns:
|
||||
str: string to show user's score.
|
||||
"""
|
||||
username = username[:username_length].ljust(username_length, " ")
|
||||
|
||||
# get score, pad to correct length
|
||||
if is_time_record:
|
||||
# MM:SS.ms is 7 characters long
|
||||
# if there are single digit minutes, add a space for formatting.
|
||||
score = get_time_str(score)
|
||||
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
|
||||
score = " " + score
|
||||
else:
|
||||
score = str(int(score)).rjust(7, " ")
|
||||
|
||||
return f"{rank}: {username} - {score}".ljust(pad_length, " ")
|
||||
|
||||
|
||||
def get_time_str(time_in: float) -> str:
|
||||
"""
|
||||
Formats a float value into the desired string format for a timestamp.
|
||||
@@ -291,28 +321,31 @@ def get_time_str(time_in: float) -> str:
|
||||
return time.strftime(f"%-M:%S.{milliseconds}", time.gmtime(time_in))
|
||||
|
||||
|
||||
async def send_highscore_message(channel: nextcord.TextChannel, group: CategoryGroup) -> nextcord.Message:
|
||||
async def send_highscore_message(session: Session, guild: nextcord.Guild, group: CategoryGroup) -> nextcord.Message:
|
||||
"""
|
||||
Function takes care of checking if a message exists for a group.
|
||||
Creates and sends/edits the message.
|
||||
|
||||
Args:
|
||||
channel (nextcord.TextChannel): Discord channel object where message is sent
|
||||
group (CategoryGroup): group to post to the message
|
||||
session (Session): The sqlalchemy session being used for this function call.
|
||||
guild (nextcord.Guild): the nextcord.Guild object requesting this message.
|
||||
group (CategoryGroup): group to post to the message.
|
||||
|
||||
Returns:
|
||||
nextcord.Message: Message object that was posted with group contents
|
||||
nextcord.Message: Message object that was posted with group contents.
|
||||
"""
|
||||
highscore_string = format_highscore_message(group)
|
||||
|
||||
try:
|
||||
message: nextcord.Message = await channel.fetch_message(group.MessageId)
|
||||
await message.edit(content=highscore_string)
|
||||
except nextcord.NotFound:
|
||||
highscore_string = format_highscore_message(session, group)
|
||||
channel = guild.get_channel(group.ChannelId)
|
||||
|
||||
if group.MessageId is None:
|
||||
# message doesn't exist.
|
||||
message: nextcord.Message = await channel.send(highscore_string)
|
||||
group.MessageId = message.id
|
||||
session.commit()
|
||||
else:
|
||||
message: nextcord.Message = await channel.fetch_message(group.MessageId)
|
||||
await message.edit(content=highscore_string)
|
||||
|
||||
return message
|
||||
|
||||
@@ -332,37 +365,31 @@ async def submit_score(
|
||||
score (str): Score to submit. Need to convert to correct type.
|
||||
"""
|
||||
|
||||
stmt = select(Category.IsTimeRecord).where(Category.CategoryId == category_id)
|
||||
is_time_record = session.scalar(stmt)
|
||||
with Session(engine) as session:
|
||||
stmt = select(Category.IsTimeRecord).where(Category.CategoryId == category_id)
|
||||
is_time_record = session.scalar(stmt)
|
||||
|
||||
if is_time_record:
|
||||
minutes = float(score.split(":", maxsplit=1)[0])
|
||||
seconds = float(score.split(":", maxsplit=1)[1])
|
||||
score = (minutes * 60) + seconds
|
||||
else:
|
||||
score = float(score)
|
||||
if is_time_record:
|
||||
minutes = float(score.split(":", maxsplit=1)[0])
|
||||
seconds = float(score.split(":", maxsplit=1)[1])
|
||||
score = (minutes * 60) + seconds
|
||||
else:
|
||||
score = float(score)
|
||||
|
||||
stmt = select(Score).where(Score.UserName == username).where(Score.CategoryId == category_id)
|
||||
existing_score = session.scalar(stmt)
|
||||
stmt = select(Score).where(Score.UserName == username).where(Score.CategoryId == category_id)
|
||||
existing_score = session.scalar(stmt)
|
||||
|
||||
if existing_score:
|
||||
existing_score.Score = score
|
||||
else:
|
||||
score_obj = Score(UserName=username, Score=score, CategoryId=category_id)
|
||||
session.add(score_obj)
|
||||
session.commit()
|
||||
if existing_score:
|
||||
existing_score.Score = score
|
||||
else:
|
||||
score_obj = Score(UserName=username, Score=score, CategoryId=category_id)
|
||||
session.add(score_obj)
|
||||
session.commit()
|
||||
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == group_id)
|
||||
group = session.scalar(stmt)
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == group_id)
|
||||
group = session.scalar(stmt)
|
||||
|
||||
# edit message
|
||||
channel = interaction.guild.get_channel(group.ChannelId)
|
||||
|
||||
error_response = "Registered Highscores channel does not exist or was never registered."
|
||||
error_response += 'Register with "/register" command.'
|
||||
assert channel is not None, await interaction.send(error_response, ephemeral=True)
|
||||
|
||||
await send_highscore_message(channel, group)
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
|
||||
|
||||
def add_preset_configuration(guild_id: int, config_str: str) -> None:
|
||||
@@ -374,19 +401,21 @@ def add_preset_configuration(guild_id: int, config_str: str) -> None:
|
||||
guild_id (int): Guild id to add config to
|
||||
config_str (str): Config name to add
|
||||
"""
|
||||
with open(f"example_config/{config_str}.json", encoding="utf-8", mode="r") as file:
|
||||
config = json.load(file)
|
||||
|
||||
for group_name, categorys in config["tables"].items():
|
||||
group_obj = CategoryGroup(GroupName=group_name, GuildId=guild_id)
|
||||
session.add(group_obj)
|
||||
for category_name in categorys:
|
||||
cat_obj = Category(
|
||||
CategoryName=category_name,
|
||||
Size=config["table_size"],
|
||||
IsTimeRecord=config["categories"][category_name]["is_time_record"],
|
||||
IsAscending=config["categories"][category_name]["ascending"],
|
||||
)
|
||||
session.add(cat_obj)
|
||||
with Session(engine) as session:
|
||||
with open(f"example_config/{config_str}.json", encoding="utf-8", mode="r") as file:
|
||||
config = json.load(file)
|
||||
|
||||
session.commit()
|
||||
for group_name, categorys in config["tables"].items():
|
||||
group_obj = CategoryGroup(GroupName=group_name, GuildId=guild_id)
|
||||
session.add(group_obj)
|
||||
for category_name in categorys:
|
||||
cat_obj = Category(
|
||||
CategoryName=category_name,
|
||||
Size=config["table_size"],
|
||||
IsTimeRecord=config["categories"][category_name]["is_time_record"],
|
||||
IsAscending=config["categories"][category_name]["ascending"],
|
||||
)
|
||||
session.add(cat_obj)
|
||||
|
||||
session.commit()
|
||||
|
||||
Reference in New Issue
Block a user