Skip to content

Commit

Permalink
Migrated invitational collection to ODM
Browse files Browse the repository at this point in the history
  • Loading branch information
Nydauron committed May 31, 2024
1 parent bbcb5f3 commit 5c7a7ce
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 245 deletions.
3 changes: 2 additions & 1 deletion src/discord/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Holds global variables shared between cogs and variables that are initialized when
the bot is first setup.
"""
from src.mongo.models import Invitational

##############
# CONSTANTS
Expand Down Expand Up @@ -121,7 +122,7 @@
CENSOR = {}
EVENT_INFO = []
PING_INFO = []
INVITATIONAL_INFO = []
INVITATIONAL_INFO: list[Invitational] = []
REPORTS = []
TAGS = []
CURRENT_WIKI_PAGE = None
42 changes: 13 additions & 29 deletions src/discord/invitationals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import TYPE_CHECKING

import discord
from beanie import SortDirection
from beanie.odm.operators.update.array import Push
from discord.ext import commands

from env import env
Expand All @@ -18,6 +20,7 @@
ROLE_AT,
ROLE_GM,
)
from src.mongo.models import Invitational

if TYPE_CHECKING:
from bot import PiBot
Expand All @@ -28,24 +31,6 @@
logger = logging.getLogger(__name__)


class Invitational:
official_name: str
voters: list

def __init__(self, objects):
self._properties = objects
self.doc_id = objects.get("_id")
self.official_name = objects.get("official_name")
self.channel_name = objects.get("channel_name")
self.emoji = objects.get("emoji")
self.aliases = objects.get("aliases")
self.tourney_date = objects.get("tourney_date")
self.open_days = objects.get("open_days")
self.closed_days = objects.get("closed_days")
self.voters = objects.get("voters")
self.status = objects.get("status")


class AllInvitationalsView(discord.ui.View):
"""
A view class for holding the button to toggle visibility of all invitationals for a user.
Expand Down Expand Up @@ -144,7 +129,7 @@ async def callback(self, interaction: discord.Interaction):

else:
# This dropdown is being used for voting
need_to_update = []
need_to_update: list[Invitational] = []
already_voted_for = []

for value in self.values:
Expand All @@ -153,6 +138,8 @@ async def callback(self, interaction: discord.Interaction):
self.invitationals,
official_name=value,
)
if not invitational:
continue
if member.id in invitational.voters:
# This user has already voted for this invitational.
already_voted_for.append(invitational)
Expand All @@ -164,13 +151,9 @@ async def callback(self, interaction: discord.Interaction):
# Update invitationals DB
if len(need_to_update) > 0:
# Some docs need to be updated
docs_to_update = [t._properties for t in need_to_update]
await self.bot.mongo_database.update_many(
"data",
"invitationals",
docs_to_update,
{"$push": {"voters": member.id}},
)

for invy in need_to_update:
invy.update(Push({Invitational.voters: member.id}))

# Format output
result_string = ""
Expand Down Expand Up @@ -201,9 +184,10 @@ async def update_invitational_list(bot: PiBot, rename_dict: dict = {}) -> None:
:param rename_dict: A dictionary containing renames of channels and roles that need to be completed.
"""
# Fetch invitationals
invitationals = await bot.mongo_database.get_invitationals()
invitationals = [Invitational(t) for t in invitationals]
invitationals.sort(key=lambda t: t.official_name)
invitationals = await Invitational.find_all(
sort=[(Invitational.official_name, SortDirection.ASCENDING)],
ignore_cache=True,
).to_list()

# Update global invitational info
global INVITATIONAL_INFO
Expand Down
17 changes: 4 additions & 13 deletions src/discord/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

from env import env
from src.discord.globals import CHANNEL_CLOSED_REPORTS
from src.discord.invitationals import Invitational, update_invitational_list
from src.discord.invitationals import update_invitational_list
from src.mongo.models import Invitational

if TYPE_CHECKING:
from bot import PiBot
Expand Down Expand Up @@ -207,12 +208,7 @@ async def callback(self, interaction: discord.Interaction):
await interaction.message.delete()

# Update the invitationals database
await self.report_view.bot.mongo_database.update(
"data",
"invitationals",
self.report_view.invitational_obj.doc_id,
{"$set": {"status": "archived"}},
)
self.report_view.invitational_obj.set({Invitational.status: "archived"})

# Send an informational message about the report being updated
closed_reports = discord.utils.get(
Expand Down Expand Up @@ -251,12 +247,7 @@ async def callback(self, interaction: discord.Interaction):
await interaction.message.delete()

# Update the invitationals database
await self.report_view.bot.mongo_database.update(
"data",
"invitationals",
self.report_view.invitational_obj.doc_id,
{"$inc": {"closed_days": 15}},
)
await self.report_view.invitational_obj.inc({Invitational.closed_days: 15})

# Send an informational message about the report being updated
closed_reports = discord.utils.get(
Expand Down
Loading

0 comments on commit 5c7a7ce

Please sign in to comment.