From 22c5d1c8e7bede00ac361a6e9dae113c532b6d31 Mon Sep 17 00:00:00 2001 From: Cameron Brown Date: Mon, 12 Feb 2024 21:50:14 -0500 Subject: [PATCH] Setup resource caching style --- bot.py | 46 +++++++++++++++++++++---------------------- src/discord/censor.py | 16 ++++++--------- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/bot.py b/bot.py index 76b4441..a06c856 100644 --- a/bot.py +++ b/bot.py @@ -25,7 +25,6 @@ CHANNEL_DELETEDM, CHANNEL_DMLOG, CHANNEL_EDITEDM, - CHANNEL_RULES, DEV_TOKEN, SERVER_ID, TOKEN, @@ -141,6 +140,10 @@ class PiBot(commands.Bot): "invitational_season": None, } + active_guild: discord.Guild + + staff_category: discord.CategoryChannel + def __init__(self): super().__init__( command_prefix=BOT_PREFIX, @@ -198,34 +201,29 @@ async def update_setting(self, values: dict[str, Any]): {"$set": values}, ) + def fetch_resources(self) -> None: + """ + Fetches resources from the database and sets them to the bot. + """ + active_guild = self.get_guild(SERVER_ID) + if active_guild is None: + raise ValueError("The active guild could not be found.") + self.active_guild = active_guild + + staff_category = discord.utils.get( + self.active_guild.categories, + name="staff", + ) + if staff_category is None: + raise ValueError("The staff category could not be found.") + self.staff_category = staff_category + async def on_ready(self) -> None: """ Called when the bot is enabled and ready to be run. """ - # try: - # await self.tree.sync(guild=discord.Object(749057176756027414)) - # except: - # import traceback - # traceback.print_exc() logger.info(f"{self.user} has connected!") - - # Add message to rules channel - server = self.get_guild(SERVER_ID) - assert isinstance(server, discord.Guild) - rules_channel = discord.utils.get(server.text_channels, name=CHANNEL_RULES) - assert isinstance(rules_channel, discord.TextChannel) - rules_message = [m async for m in rules_channel.history(limit=1)] - if rules_message: - rules_message = rules_message[0] - view = discord.ui.View() - view.add_item( - discord.ui.Button( - url="https://scioly.org/rules", - label="Complete Scioly.org rules", - style=discord.ButtonStyle.link, - ), - ) - await rules_message.edit(view=view) + self.fetch_resources() async def on_message(self, message: discord.Message) -> None: # Nothing needs to be done to the bot's own messages diff --git a/src/discord/censor.py b/src/discord/censor.py index 5915739..994a2bd 100644 --- a/src/discord/censor.py +++ b/src/discord/censor.py @@ -15,7 +15,6 @@ import src.discord.globals from src.discord.globals import ( - CATEGORY_STAFF, CHANNEL_SUPPORT, DISCORD_INVITE_ENDINGS, ROLE_UC, @@ -47,16 +46,13 @@ async def on_message(self, message: discord.Message) -> None: """ # Type checking - Assume messages come from a text channel where the author # is a member of the server - if not isinstance(message.channel, discord.TextChannel) or not isinstance( - message.author, - discord.Member, - ): - return - - # Do not act on messages in staff channels if ( - message.channel.category is not None - and message.channel.category.name == CATEGORY_STAFF + not isinstance(message.channel, discord.TextChannel) + or not isinstance( + message.author, + discord.Member, + ) + or message.channel.category == self.bot.staff_category ): return