FEAT: Added a Logger

REF: Changed Spotify Module to use the logger now
This commit is contained in:
Dan 2024-06-21 13:08:45 -04:00
parent ecea3b93e0
commit 15ed750f8b
4 changed files with 104 additions and 6 deletions

17
main.py
View File

@ -8,17 +8,22 @@ class Selena(discord.Client):
self.tree = discord.app_commands.CommandTree(self)
async def setup_hook(self):
guild = discord.Object(id=config.DISCORD_GUILD_ID)
modules = ["modules.media.spotify_module"]
for module in modules:
await self.load_extension(module)
self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)
await self.load_extensions()
await self.tree.sync()
async def load_extension(self, name):
module = __import__(name, fromlist=["setup"])
await module.setup(self)
async def load_extensions(self):
extensions = [
"modules.admin.logger_module",
"modules.media.spotify_module",
# Add other modules here
]
for extension in extensions:
await self.load_extension(extension)
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')

View File

@ -0,0 +1,29 @@
# modules/admin/logger_module.py
import discord
import logging
import logging.config
from .logging_config import logging_config
class LoggerModule:
def __init__(self, bot):
self.bot = bot
logging.config.dictConfig(logging_config)
self.logger = logging.getLogger(__name__)
def add_logging_commands(self):
@self.bot.tree.command(name="log_test",
description="Test the logging system")
async def log_test(interaction: discord.Interaction):
self.logger.debug("This is a debug message")
self.logger.info("This is an info message")
self.logger.warning("This is a warning message")
self.logger.error("This is an error message")
self.logger.critical("This is a critical message")
await interaction.response.send_message("Logging test completed."
"Check the logs!")
async def setup(bot):
logger_module = LoggerModule(bot)
logger_module.add_logging_commands()

View File

@ -0,0 +1,40 @@
# modules/admin/logging_config.py
import logging
import logging.handlers
import os
LOG_DIR = "logs"
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
LOG_FILE = os.path.join(LOG_DIR, "selena.log")
logging_config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {
"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"
},
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "standard",
},
"file_handler": {
"level": "DEBUG",
"class": "logging.handlers.RotatingFileHandler",
"formatter": "standard",
"filename": LOG_FILE,
"maxBytes": 1024*1024*5, # 5 MB
"backupCount": 3,
},
},
"root": {
"handlers": ["console", "file_handler"],
"level": "DEBUG",
},
}

View File

@ -1,9 +1,14 @@
import logging
import discord
from discord import app_commands
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import config
# Set up logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class SpotifyModule:
def __init__(self, bot):
@ -37,6 +42,7 @@ class SpotifyModule:
color=discord.Color.red()
)
)
logger.info("No song is currently playing")
else:
track = current["item"]
artist = ", ".join([a["name"] for a in track["artists"]])
@ -51,8 +57,10 @@ class SpotifyModule:
)
embed.set_thumbnail(url=track['album']['images'][0]['url'])
await interaction.followup.send(embed=embed)
logger.info(f"Currently playing: {track['name']} by {artist}") # noqa: E501
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in current_track command: {e}")
@app_commands.command(
name="play_track", description="Play a track by searching for it"
@ -69,6 +77,7 @@ class SpotifyModule:
color=discord.Color.red()
)
)
logger.info(f"No results found for query: {query}")
return
track = results["tracks"]["items"][0]
@ -84,6 +93,7 @@ class SpotifyModule:
color=discord.Color.red()
)
)
logger.info("No active devices found for playback")
return
self.sp.start_playback(uris=[uri])
@ -97,8 +107,10 @@ class SpotifyModule:
)
embed.set_thumbnail(url=track['album']['images'][0]['url'])
await interaction.followup.send(embed=embed)
logger.info(f"Now playing: {track['name']} by {', '.join([a['name'] for a in track['artists']])}") # noqa: E501
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in play_track command: {e}")
@app_commands.command(
name="play_playlist",
@ -123,6 +135,7 @@ class SpotifyModule:
color=discord.Color.red()
)
)
logger.info(f"No results found for query: {query}")
return
playlist = results["playlists"]["items"][0]
uri = playlist["uri"]
@ -137,6 +150,7 @@ class SpotifyModule:
color=discord.Color.red()
)
)
logger.info("No active devices found for playback")
return
self.sp.start_playback(context_uri=uri)
@ -148,8 +162,10 @@ class SpotifyModule:
if not query.startswith("https://open.spotify.com/playlist/"):
embed.set_thumbnail(url=playlist['images'][0]['url'])
await interaction.followup.send(embed=embed)
logger.info(f"Now playing playlist: {playlist['name']} by {playlist['owner']['display_name']}") # noqa: E501
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in play_playlist command: {e}")
@app_commands.command(
name="pause", description="Pause the currently playing track"
@ -165,8 +181,10 @@ class SpotifyModule:
color=discord.Color.green()
)
)
logger.info("Playback paused")
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in pause command: {e}")
@app_commands.command(
name="resume", description="Resume the currently playing track"
@ -182,8 +200,10 @@ class SpotifyModule:
color=discord.Color.green()
)
)
logger.info("Playback resumed")
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in resume command: {e}")
@app_commands.command(
name="next", description="Skip to the next track"
@ -199,8 +219,10 @@ class SpotifyModule:
color=discord.Color.green()
)
)
logger.info("Skipped to the next track")
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in next_track command: {e}")
@app_commands.command(
name="previous", description="Go back to the previous track"
@ -216,8 +238,10 @@ class SpotifyModule:
color=discord.Color.green()
)
)
logger.info("Returned to the previous track")
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
logger.error(f"Error in previous_track command: {e}")
self.bot.tree.add_command(current_track)
self.bot.tree.add_command(play_track)