Skip to content

Commit

Permalink
Logging to files now work
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalk0 committed Sep 15, 2023
1 parent 8fccf48 commit 75be162
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
11 changes: 8 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Ignore the dotenv
*.env

# Ignore IDE folder
.idea
.vscode
.idea/
.vscode/

# Ignore Python's cache
__pycache__
__pycache__/

# Ignore the logs folder
logs/
17 changes: 8 additions & 9 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import discord
from dotenv import dotenv_values

Expand Down Expand Up @@ -46,14 +48,11 @@ async def on_ready(self):
name=self.config['BOT_ACTIVITY_NAME'])
await self.change_presence(activity=activity, status=self.config['BOT_STATUS'])

# Set up logging
discord.utils.setup_logging()

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

# Prints in the console that the bot is ready
print(f'{self.user} is now online and ready!')
# Log in the console that the bot is ready
logging.info(f"{self.user} is now online and ready!")

# To react to messages sent in channels bot has access to
async def on_message(self, message):
Expand All @@ -68,12 +67,12 @@ async def on_message(self, message):

# Do a log on the Python console
if message.guild is not None:
print(f'{username} said: "{user_msg}" #{channel} in {message.guild.name}')
logging.info(f'{username} said: "{user_msg}" #{channel} in {message.guild.name}')
else:
print(f'{username} said: "{user_msg}" in Direct Message')
logging.info(f'{username} said: "{user_msg}" in Direct Message')

# Call responses with message of the user and responds if necessary
response = await responses(self, user_msg, channel)
if not response == '':
await channel.send(response)
print(f'{self.user} responded : "{response}"')
logging.info(f'{self.user} responded : "{response}"')
10 changes: 9 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import logging.handlers as handlers
import os

from bot import ChouetteBot


def main():
# Create an instance of the ChouetteBot
client = ChouetteBot()

# Setup the logging
os.makedirs('logs', exist_ok=True)
handler = handlers.RotatingFileHandler(filename='logs\\bot.log', encoding='utf-8', backupCount=3)
handler.doRollover()

# Run the client with the token
client.run(client.config['BOT_TOKEN'], reconnect=True)
client.run(client.config['BOT_TOKEN'], reconnect=True, root_logger=True, log_handler=handler)


if __name__ == '__main__':
Expand Down
4 changes: 3 additions & 1 deletion responses.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from src.latex_render import latex_process


Expand All @@ -10,7 +12,7 @@ async def responses(self, message: str, channel) -> str:
if message.count("$") > 1:
if (message.count("$") % 2) == 0:
await channel.send(file=await latex_process(message))
print(f'{self.user} responded : "equation.png"')
logging.info(f'{self.user} responded : "equation.png"')
return ''
return "Nombre de $ impair, " \
"veuillez en mettre un nombre pair pour que je puisse afficher les équations LaTeX !"
Expand Down

0 comments on commit 75be162

Please sign in to comment.