Skip to content

Commit

Permalink
Setup resource caching style
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrxyz committed Feb 13, 2024
1 parent 46476e5 commit 22c5d1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
46 changes: 22 additions & 24 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
CHANNEL_DELETEDM,
CHANNEL_DMLOG,
CHANNEL_EDITEDM,
CHANNEL_RULES,
DEV_TOKEN,
SERVER_ID,
TOKEN,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions src/discord/censor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import src.discord.globals
from src.discord.globals import (
CATEGORY_STAFF,
CHANNEL_SUPPORT,
DISCORD_INVITE_ENDINGS,
ROLE_UC,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 22c5d1c

Please sign in to comment.