Skip to content

Commit

Permalink
fix: code cleanup
Browse files Browse the repository at this point in the history
- improve ruff workflow output
- add speed to discord.py (contains libraries for speedups of aiohttp too)
- reformat comments to keep lines short
- use setup_hook instead of boolean hack in on_ready
  • Loading branch information
Zalk0 committed Mar 28, 2024
1 parent c1c1232 commit fba15a0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ruff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ jobs:
uses: actions/checkout@v4
- name: Lint
uses: chartboost/ruff-action@v1
with:
args: "check"
- name: Format
uses: chartboost/ruff-action@v1
with:
args: "format"
args: "format --diff"
36 changes: 17 additions & 19 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def __init__(self):

# Set intents for the bot
intents = discord.Intents.all()
intents.presences = False
intents.typing = False
intents.voice_states = False

# Set activity of the bot
activity_type = {
Expand All @@ -54,33 +57,28 @@ def __init__(self):
# Declare command tree
self.tree = discord.app_commands.CommandTree(self)

# Used to check the first time the bot does the on_ready event
self.first = True
# First declaration to be able to add commands to the guild
self.hypixel_guild = discord.Object(int(self.config["HYPIXEL_GUILD_ID"]))

async def setup_hook(self):
# Call commands and import tasks
await commands(self)
await tasks.tasks_list(self)

# Start web server
await self.start_server()

# Wait until bot is ready
async def on_ready(self):
# Waits until internal cache is ready
await self.wait_until_ready()

# Executed once when bot is ready
if self.first:
# Hypixel guild
hypixel_guild = self.get_guild(int(self.config["HYPIXEL_GUILD_ID"]))

# Call commands and import tasks
await commands(self.tree, hypixel_guild)
await tasks.tasks_list(self)

# Start web server
await self.start_server()
# Hypixel guild with all information
self.hypixel_guild = self.get_guild(int(self.config["HYPIXEL_GUILD_ID"]))

self.first = False

# Check the number of servers the bot is a part of
self.bot_logger.info(f"Number of servers I'm in : {len(self.guilds)}")

# Log in the console that the bot is ready
# Log that the bot is ready and the number of guilds the bot is in
self.bot_logger.info(f"{self.user} is now online and ready!")
self.bot_logger.info(f"Number of servers I'm in : {len(self.guilds)}")

# To react to messages sent in channels bot has access to
async def on_message(self, message: discord.Message):
Expand Down
8 changes: 4 additions & 4 deletions commands_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@


# List of commands to add to the command tree
async def commands(tree: discord.app_commands.CommandTree, hypixel_guild: discord.Guild):
async def commands(client: ChouetteBot):
# Add the commands to the Tree
for command in COMMANDS_LIST:
tree.add_command(command)
client.tree.add_command(command)

# Add the Skyblock command group to my Hypixel guild
tree.add_command(Skyblock(), guild=hypixel_guild)
client.tree.add_command(Skyblock(), guild=client.hypixel_guild)

# Create a global commands error handler
@tree.error
@client.tree.error
async def on_command_error(
interaction: discord.Interaction[ChouetteBot],
error: discord.app_commands.AppCommandError,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
aiohttp~=3.9.3
discord.py~=2.3.2
discord.py[speed]~=2.3.2
python-dotenv~=1.0.1
8 changes: 5 additions & 3 deletions utils/latex_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
# Then send the image as a file
async def latex_render(equation: str) -> discord.File:
options = r"\dpi{200} \bg_black \color[RGB]{240, 240, 240} \pagecolor[RGB]{49, 51, 56}"
# bg_black is for putting a black background (custom command of the site) instead of a transparent one
# only then a custom background color can be used with pagecolor. color is for the text color
# bg_black is for putting a black background (custom command of the site)
# instead of the transparent one. Then a custom background color can be used with pagecolor.
# color is for the text color
url = f"https://latex.codecogs.com/png.latex?{options} {equation}".replace(" ", "%20")
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
Expand All @@ -29,7 +30,8 @@ async def latex_process(message: str):
else: # It's text
if parts[i].count("\n") > 0:
linebreak = r"} \\ \textrm{".join(parts[i].split("\n"))
# Not using splitlines method because I need to keep linebreaks at the end of the text
# Not using splitlines method
# Because I need to keep linebreaks at the end of the text
equation += rf" \textrm{{{linebreak}}}"
else:
equation += rf" \textrm{{{parts[i]}}}"
Expand Down

0 comments on commit fba15a0

Please sign in to comment.