2023-05-27 16:30:07 +00:00
|
|
|
import discord
|
|
|
|
import logging
|
|
|
|
import logging.handlers
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from discord.ext import commands
|
2023-05-27 16:47:03 +00:00
|
|
|
from os import getenv
|
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
|
|
|
|
|
2023-05-27 16:30:07 +00:00
|
|
|
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')
|
2024-03-31 03:10:24 +00:00
|
|
|
embed.set_author(name=f"User left",
|
2023-05-27 16:30:07 +00:00
|
|
|
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"<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')
|
2024-03-31 03:10:24 +00:00
|
|
|
embed.set_author(name=f"User joined",
|
2023-05-27 16:30:07 +00:00
|
|
|
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"<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")
|
|
|
|
|