Skip to content

Commit

Permalink
Migrated event 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 f158605
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 41 deletions.
2 changes: 2 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from discord.ext import commands
from rich.logging import RichHandler

import src.mongo.models
from env import env
from src.discord.globals import (
CHANNEL_BOTSPAM,
Expand Down Expand Up @@ -164,6 +165,7 @@ async def setup_hook(self) -> None:
await init_beanie(
database=self.mongo_database.client["data"],
document_models=[
src.mongo.models.Event,
# TODO
],
)
Expand Down
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 Event

##############
# CONSTANTS
Expand Down Expand Up @@ -119,7 +120,7 @@
##############
fish_now = 0
CENSOR = {}
EVENT_INFO = []
EVENT_INFO: list[Event] = []
PING_INFO = []
INVITATIONAL_INFO = []
REPORTS = []
Expand Down
6 changes: 3 additions & 3 deletions src/discord/membercommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ async def events(
event_ten,
]
param_list = [p for p in param_list if p is not None]
event_names = [e["name"] for e in src.discord.globals.EVENT_INFO]
event_names = [e.name for e in src.discord.globals.EVENT_INFO]

selected_roles = [
discord.utils.get(member.guild.roles, name=e)
Expand Down Expand Up @@ -1246,9 +1246,9 @@ async def events_autocomplete(
List[app_commands.Choice[str]]: A list of string choices to choose from.
"""
return [
app_commands.Choice(name=e["name"], value=e["name"])
app_commands.Choice(name=e.name, value=e.name)
for e in src.discord.globals.EVENT_INFO
if current.lower() in e["name"].lower()
if current.lower() in e.name.lower()
][:25]

@app_commands.command(description="Gets a tag.")
Expand Down
11 changes: 6 additions & 5 deletions src/discord/staff/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ROLE_STAFF,
ROLE_VIP,
)
from src.mongo.models import Event

if TYPE_CHECKING:
from bot import PiBot
Expand Down Expand Up @@ -55,7 +56,7 @@ async def event_add(
)

# Check to see if event has already been added.
if event_name in [e["name"] for e in src.discord.globals.EVENT_INFO]:
if event_name in [e.name for e in src.discord.globals.EVENT_INFO]:
return await interaction.edit_original_response(
content=f"The `{event_name}` event has already been added.",
)
Expand All @@ -65,11 +66,11 @@ async def event_add(
aliases_array = []
if event_aliases:
aliases_array = re.findall(r"\w+", event_aliases)
new_dict = {"name": event_name, "aliases": aliases_array}
new_dict = Event(name=event_name, aliases=aliases_array, emoji=None)

# Add dict into events container
await new_dict.insert()
src.discord.globals.EVENT_INFO.append(new_dict)
await self.bot.mongo_database.insert("data", "events", new_dict)

# Create role on server
server = self.bot.get_guild(env.server_id)
Expand Down Expand Up @@ -110,7 +111,7 @@ async def event_remove(

# Check to make sure event has previously been added
event_not_in_list = event_name not in [
e["name"] for e in src.discord.globals.EVENT_INFO
e.name for e in src.discord.globals.EVENT_INFO
]

# Check to see if role exists on server
Expand Down Expand Up @@ -138,8 +139,8 @@ async def event_remove(
event = next(
e for e in src.discord.globals.EVENT_INFO if e["name"] == event_name
)
await event.delete()
src.discord.globals.EVENT_INFO.remove(event)
await self.bot.mongo_database.delete("data", "events", event["_id"])

# Notify staff member of completion
if delete_role == "yes":
Expand Down
3 changes: 2 additions & 1 deletion src/discord/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from env import env
from src.discord.invitationals import update_invitational_list
from src.discord.views import UnselfmuteView
from src.mongo.models import Event

if TYPE_CHECKING:
from bot import PiBot
Expand Down Expand Up @@ -77,7 +78,7 @@ async def pull_prev_info(self):
src.discord.globals.REPORTS = await self.bot.mongo_database.get_reports()
src.discord.globals.PING_INFO = await self.bot.mongo_database.get_pings()
src.discord.globals.TAGS = await self.bot.mongo_database.get_tags()
src.discord.globals.EVENT_INFO = await self.bot.mongo_database.get_events()
src.discord.globals.EVENT_INFO = await Event.find_all().to_list()
self.bot.settings = await self.bot.mongo_database.get_settings()
assert isinstance(self.bot.settings, dict)

Expand Down
2 changes: 1 addition & 1 deletion src/discord/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ async def request_access(
event_options: list[discord.SelectOption] = []
for event in src.discord.globals.EVENT_INFO:
event_options.append(
discord.SelectOption(label=event["name"], emoji=event["emoji"]),
discord.SelectOption(label=event.name, emoji=event.emoji),
)
event_options.sort(key=lambda x: x.label)
event_chooser = Chooser(
Expand Down
12 changes: 12 additions & 0 deletions src/mongo/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
"""
Contains all database models
"""

from beanie import Document


class Event(Document):
name: str
aliases: list[str]
emoji: str | None

class Settings:
name = "events"
use_cache = True
54 changes: 24 additions & 30 deletions src/mongo/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,48 +83,42 @@ async def get_entire_collection(
result.append(doc)
return result

async def get_invitationals(self):
"""
Gets all documents in the invitationals collection.
"""
return await self.get_entire_collection("data", "invitationals")

async def get_cron(self):
"""
Gets all documents in the CRON collection.
"""
return await self.get_entire_collection("data", "cron")

# async def get_invitationals(self):
# """
# Gets all documents in the invitationals collection.
# """
# return await self.get_entire_collection("data", "invitationals")

# async def get_cron(self):
# """
# Gets all documents in the CRON collection.
# """
# return await self.get_entire_collection("data", "cron")
#
async def get_censor(self):
"""
Gets the document containing censor information from the censor collection.
"""
return await self.get_entire_collection("data", "censor", return_one=True)

async def get_pings(self):
"""
Gets all documents in the pings collection.
"""
return await self.get_entire_collection("data", "pings")

async def get_tags(self):
"""
Gets all documents in the tags collection.
"""
return await self.get_entire_collection("data", "tags")

# async def get_pings(self):
# """
# Gets all documents in the pings collection.
# """
# return await self.get_entire_collection("data", "pings")
#
# async def get_tags(self):
# """
# Gets all documents in the tags collection.
# """
# return await self.get_entire_collection("data", "tags")
#
async def get_reports(self):
"""
Gets all documents in the reports collection.
"""
return await self.get_entire_collection("data", "reports")

async def get_events(self):
"""
Gets all documents in the events collection.
"""
return await self.get_entire_collection("data", "events")

async def get_settings(self):
"""
Gets the one document containing settings information from the settings
Expand Down

0 comments on commit f158605

Please sign in to comment.