watchdog/watchdog.py

114 lines
3.8 KiB
Python
Raw Normal View History

2023-05-27 16:30:07 +00:00
import discord
import logging
import logging.handlers
import signal
import sys
from discord.ext import commands
from os import environ
2023-05-27 16:30:07 +00:00
from rich.console import Console
from rich import inspect
2023-05-27 16:47:03 +00:00
from datetime import datetime, timezone
2024-08-06 03:10:10 +00:00
application_id = environ["APPLICATION_ID"]
2023-05-27 16:30:07 +00:00
# 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 = application_id
2023-05-27 16:30:07 +00:00
)
# 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"User left",
2024-05-30 19:30:47 +00:00
icon_url=member.display_avatar.replace(size=64,
2023-05-27 16:30:07 +00:00
static_format="png")
)
2024-05-30 19:30:47 +00:00
embed.set_thumbnail(url=member.display_avatar.replace(size=1024,
2023-05-27 16:30:07 +00:00
static_format="png")
)
embed.add_field(name="**Registered:**", value=f"<t:{registered}>")
embed.add_field(name="**Joined:**", value=f"<t:{joined}>", 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"User joined",
2024-05-30 19:30:47 +00:00
icon_url=member.display_avatar.replace(size=64,
2023-05-27 16:30:07 +00:00
static_format="png")
)
2024-05-30 19:30:47 +00:00
embed.set_thumbnail(url=member.display_avatar.replace(size=1024,
2023-05-27 16:30:07 +00:00
static_format="png")
)
embed.add_field(name="**Registered:**", value=f"<t:{registered}>")
embed.add_field(name="**Joined:**", value=f"<t:{joined}>", 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")