import discord import logging import logging.handlers import signal import sys from discord.ext import commands from os import getenv from rich.console import Console from rich import inspect from datetime import datetime, timezone application_id = getenv("APPLICATION_ID") # Console object for rich text content console = Console() # Configure log handling logger = logging.getLogger('discord') logger.setLevel(logging.INFO) handler = logging.handlers.RotatingFileHandler( filename='discord.log', encoding='utf-8', maxBytes=32 * 1024 * 1024, # 32 MiB backupCount=5, # Rotate through 5 files ) logger.addHandler(handler) # Exit quietly when KeyboardInterrupt is triggered. signal.signal(signal.SIGINT, lambda x, y: sys.exit(0)) class Watchdog(commands.AutoShardedBot): def __init__(self): intents = discord.Intents.default() intents.members = True super().__init__( command_prefix = "%", intents = intents, application_id = int(application_id) ) # Output to log when bot is ready async def on_ready(self): logger.info(f"Bot {self.user.name} ({self.user.id}) ready.") async def on_member_remove(self, member): inspect(member) system_channel = member.guild.system_channel if member.discriminator == "0": name = member.name else: name = member.name + "#" + member.discriminator embed = discord.Embed(description=f"### {name}\n* {member.mention}", timestamp=datetime.now(timezone.utc), color=0xf23f43) joined = member.joined_at.strftime('%s') registered = member.created_at.strftime('%s') embed.set_author(name=f"{member.display_name} left", icon_url=member.avatar.replace(size=64, static_format="png") ) embed.set_thumbnail(url=member.avatar.replace(size=1024, static_format="png") ) embed.add_field(name="**Registered:**", value=f"") embed.add_field(name="**Joined:**", value=f"", inline=False) embed.set_footer(text=f"User ID: {member.id}") await system_channel.send(embed=embed) async def on_member_join(self, member): inspect(member) system_channel = member.guild.system_channel if member.discriminator == "0": name = member.name else: name = member.name + "#" + member.discriminator embed = discord.Embed(description=f"### {name}\n* {member.mention}", timestamp=datetime.now(timezone.utc), color=0x23a55a) joined = member.joined_at.strftime('%s') registered = member.created_at.strftime('%s') embed.set_author(name=f"{member.display_name} joined", icon_url=member.avatar.replace(size=64, static_format="png") ) embed.set_thumbnail(url=member.avatar.replace(size=1024, static_format="png") ) embed.add_field(name="**Registered:**", value=f"") embed.add_field(name="**Joined:**", value=f"", inline=False) embed.set_footer(text=f"User ID: {member.id}") await system_channel.send(embed=embed) bot = Watchdog() if __name__ == "__main__": try: bot.run(getenv("BOT_TOKEN")) except discord.errors.LoginFailure: console.print("Token invalid. Check your .env file", style="bold red")