import discord
from discord.ext import commands
from discord import app_commands
import sqlite3
import os
from datetime import datetime
class ModLogs(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.database_path = './database'
os.makedirs(self.database_path, exist_ok=True)
self.modlogs_db = os.path.join(self.database_path, 'modlogs.db')
conn = sqlite3.connect(self.modlogs_db)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS modlogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
action TEXT,
user_id INTEGER,
moderator_id INTEGER,
reason TEXT,
timestamp TEXT
)''')
conn.commit()
conn.close()
@app_commands.command(name="modlogs", description="Display moderation logs")
async def modlogs(self, interaction: discord.Interaction):
warnings = self.fetch_logs('warnings.db')
timeouts = self.fetch_logs('timeouts.db')
kicks = self.fetch_logs('kicks.db')
bans = self.fetch_logs('bans.db')
embed = discord.Embed(title="Moderation Logs", color=discord.Color.blue(), timestamp=datetime.utcnow())
if warnings:
embed.add_field(name="Warnings", value="\n".join(warnings), inline=False)
if timeouts:
embed.add_field(name="Timeouts", value="\n".join(timeouts), inline=False)
if kicks:
embed.add_field(name="Kicks", value="\n".join(kicks), inline=False)
if bans:
embed.add_field(name="Bans", value="\n".join(bans), inline=False)
await interaction.response.send_message(embed=embed)
def fetch_logs(self, db_name):
path = os.path.join(self.database_path, db_name)
if not os.path.exists(path):
return []
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute("SELECT user_id, moderator_id, reason, timestamp FROM logs")
rows = c.fetchall()
conn.close()
log_list = [f"User ID: {row[0]}, Moderator ID: {row[1]}, Reason: {row[2]}, Time: {row[3]}" for row in rows]
return log_list
@commands.Cog.listener()
async def on_member_join(self, member):
conn = sqlite3.connect(self.modlogs_db)
c = conn.cursor()
c.execute("INSERT INTO modlogs (action, user_id, moderator_id, reason, timestamp) VALUES (?, ?, ?, ?, ?)",
("rejoin", member.id, 0, "User rejoined the server", datetime.utcnow().isoformat()))
conn.commit()
conn.close()
@commands.Cog.listener()
async def on_guild_remove(self, guild):
pass
@commands.Cog.listener()
async def on_command_completion(self, ctx):
pass
async def setup(bot):
await bot.add_cog(ModLogs(bot))