import discord from discord.ext import commands, tasks import datetime import asyncio # Bot setup intents = discord.Intents.default() intents.messages = True intents.message_content = True bot = commands.Bot(command_prefix="$", intents=intents) # Configuration log_config = { "channels": {}, # Stores logging channel or webhook for each type "auto_message": True, # Whether to automatically post logs "logs": { "automod": [], "roblox_api": [], "commands": [], "deleted_messages": [], "warns": [], }, "bot_logs": { "errors": [], "handles": [], "bot_logs": [], "commands": [], }, } # Helper function to send logs async def send_logs(log_type, target, logs): """ Sends logs to a webhook or a channel based on the setup. """ if isinstance(target, discord.Webhook): # Send via webhook for log in logs: await target.send(log) elif isinstance(target, discord.TextChannel): # Send to channel for log in logs: await target.send(log) # Command: $log-setup @bot.command() async def log_setup(ctx, setup_type: str, target: str, log_type: str, webhook: str = None): """ Configures logging settings. """ if setup_type.lower() not in ["auto", "webhook", "image"]: await ctx.send("Invalid setup type. Options: `auto`, `webhook`, `image`.") return if setup_type.lower() == "auto": # Auto setup: Create a text channel guild = ctx.guild if not guild: await ctx.send("This command must be run in a server.") return channel = await guild.create_text_channel(target) log_config["channels"][log_type] = channel.id await ctx.send(f"Auto-setup complete. Logs for `{log_type}` will be sent to {channel.mention}.") elif setup_type.lower() == "webhook": # Webhook setup if webhook: log_config["channels"][log_type] = webhook await ctx.send(f"Webhook set up for `{log_type}`.") else: await ctx.send("Please provide a webhook URL for the setup.") else: # Image setup (not implemented, placeholder for future) await ctx.send("Image-based logging setup is not supported yet.") # Command: $logs @bot.command() async def logs(ctx, log_type: str): """ Sends logs of the specified type for the last hour. """ if log_type not in log_config["logs"]: await ctx.send("Invalid log type. Options: `automod`, `roblox_api`, `commands`, `deleted_messages`, `warns`.") return logs = log_config["logs"][log_type] logs_last_hour = [log for log in logs if (datetime.datetime.utcnow() - log["timestamp"]).seconds <= 3600] target = log_config["channels"].get(log_type) if not target: await ctx.send(f"No channel or webhook configured for `{log_type}`.") return if isinstance(target, str): # Webhook webhook = discord.Webhook.from_url(target, adapter=discord.RequestsWebhookAdapter()) await send_logs(log_type, webhook, logs_last_hour) else: # Channel channel = bot.get_channel(target) if channel: await send_logs(log_type, channel, logs_last_hour) else: await ctx.send(f"Invalid channel for `{log_type}`.") # Command: $botlogs @bot.command() async def botlogs(ctx, log_type: str): """ Sends bot logs of the specified type for the last hour. """ if log_type not in log_config["bot_logs"]: await ctx.send("Invalid bot log type. Options: `errors`, `handles`, `bot_logs`, `commands`.") return logs = log_config["bot_logs"][log_type] logs_last_hour = [log for log in logs if (datetime.datetime.utcnow() - log["timestamp"]).seconds <= 3600] target = log_config["channels"].get(log_type) if not target: await ctx.send(f"No channel or webhook configured for `{log_type}`.") return if isinstance(target, str): # Webhook webhook = discord.Webhook.from_url(target, adapter=discord.RequestsWebhookAdapter()) await send_logs(log_type, webhook, logs_last_hour) else: # Channel channel = bot.get_channel(target) if channel: await send_logs(log_type, channel, logs_last_hour) else: await ctx.send(f"Invalid channel for `{log_type}`.") # Event: Log Automod actions @bot.event async def on_message(message): if message.author.bot: return if "forbidden_word" in message.content.lower(): log_entry = { "content": f"{message.author} sent a forbidden message in {message.channel}.", "timestamp": datetime.datetime.utcnow() } log_config["logs"]["automod"].append(log_entry) await message.delete() await message.channel.send(f"{message.author.mention}, your message contained forbidden content.") await bot.process_commands(message) # Event: Log Command Use @bot.event async def on_command(ctx): log_entry = { "content": f"{ctx.author} used command `{ctx.command}` in {ctx.channel}.", "timestamp": datetime.datetime.utcnow() } log_config["logs"]["commands"].append(log_entry) # Event: Log Deleted Messages @bot.event async def on_message_delete(message): if message.author.bot: return log_entry = { "content": f"Message deleted from {message.author} in {message.channel}: {message.content}", "timestamp": datetime.datetime.utcnow() } log_config["logs"]["deleted_messages"].append(log_entry) # Background Task: Auto Log Posting @tasks.loop(hours=1) async def auto_post_logs(): for log_type, logs in log_config["logs"].items(): logs_last_hour = [log for log in logs if (datetime.datetime.utcnow() - log["timestamp"]).seconds <= 3600] target = log_config["channels"].get(log_type) if not target: continue if isinstance(target, str): # Webhook webhook = discord.Webhook.from_url(target, adapter=discord.RequestsWebhookAdapter()) await send_logs(log_type, webhook, logs_last_hour) else: # Channel channel = bot.get_channel(target) if channel: await send_logs(log_type, channel, logs_last_hour) # Start the background task @bot.event async def on_ready(): print(f"Bot is ready! Logged in as {bot.user}") auto_post_logs.start() # Run the bot bot.run("YOUR_BOT_TOKEN")