Files
.gitignore
bot.py
README.md
requirements.txt
Code
4 494
bot.py
import discord
from discord.ext import commands
from discord import app_commands
import json
import os
from dotenv import load_dotenv
from datetime import datetime, timedelta
from collections import defaultdict
import asyncio
load_dotenv()
intents = discord.Intents.default()
intents.guilds = True
intents.members = True
intents.bans = True
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
CONFIG_FILE = 'config.json'
ACTION_THRESHOLD = 3
ACTION_TIMEFRAME = 10
action_tracker = defaultdict(lambda: defaultdict(list))
def load_config():
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'r') as f:
return json.load(f)
return {}
def save_config(config):
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=4)
def get_guild_config(guild_id):
config = load_config()
guild_id_str = str(guild_id)
if guild_id_str not in config:
config[guild_id_str] = {
'enabled': False,
'whitelist': [],
'admins': [],
'punishment': 'ban'
}
save_config(config)
return config[guild_id_str]
def update_guild_config(guild_id, guild_config):
config = load_config()
config[str(guild_id)] = guild_config
save_config(config)
def is_authorized(guild: discord.Guild, member: discord.Member):
if guild.owner_id == member.id:
return True
if member.guild_permissions.administrator:
top_role = None
for m in guild.members:
if m.guild_permissions.administrator:
if top_role is None or (m.top_role.position > top_role.position):
top_role = m.top_role
if member.top_role == top_role:
return True
guild_config = get_guild_config(guild.id)
if member.id in guild_config['admins']:
return True
return False
def is_whitelisted(guild_id, member_id):
guild_config = get_guild_config(guild_id)
return member_id in guild_config['whitelist']
async def track_action(guild_id, user_id, action_type):
now = datetime.now()
actions = action_tracker[guild_id][user_id]
actions.append({'type': action_type, 'time': now})
actions[:] = [a for a in actions if now - a['time'] < timedelta(seconds=ACTION_TIMEFRAME)]
if len(actions) >= ACTION_THRESHOLD:
return True
return False
async def punish_user(guild: discord.Guild, member: discord.Member, punishment_type: str):
try:
if punishment_type == 'kick':
await member.kick(reason='Anti-nuke: Suspicious activity detected')
elif punishment_type == 'ban':
await member.ban(reason='Anti-nuke: Suspicious activity detected')
elif punishment_type == 'timeout':
await member.timeout(timedelta(hours=24), reason='Anti-nuke: Suspicious activity detected')
action_tracker[guild.id][member.id].clear()
owner = guild.owner
if owner:
try:
embed = discord.Embed(
title='🛡️ Anti-Nuke Action Taken',
description=f'**User:** {member.mention} ({member.name})\n**Action:** {punishment_type.upper()}\n**Reason:** Suspicious activity detected',
color=discord.Color.red(),
timestamp=datetime.now()
)
embed.set_footer(text='R.B.L.X. Security Bot by Rebolex')
await owner.send(embed=embed)
except:
pass
except Exception as e:
print(f'Error punishing user: {e}')
@bot.event
async def on_ready():
print(f'╔════════════════════════════════════════╗')
print(f'║ R.B.L.X. Security Bot is ONLINE! ║')
print(f'║ Developed by: Rebolex ║')
print(f'║ Logged in as: {bot.user.name:<20} ║')
print(f'╚════════════════════════════════════════╝')
try:
synced = await bot.tree.sync()
print(f'Synced {len(synced)} slash commands')
except Exception as e:
print(f'Error syncing commands: {e}')
@bot.event
async def on_audit_log_entry_create(entry: discord.AuditLogEntry):
guild = entry.guild
guild_config = get_guild_config(guild.id)
if not guild_config['enabled']:
return
if entry.user.bot:
return
if is_whitelisted(guild.id, entry.user.id):
return
if guild.owner_id == entry.user.id:
return
if is_authorized(guild, entry.user):
return
trigger_punishment = False
if entry.action == discord.AuditLogAction.kick:
trigger_punishment = await track_action(guild.id, entry.user.id, 'kick')
elif entry.action == discord.AuditLogAction.ban:
trigger_punishment = await track_action(guild.id, entry.user.id, 'ban')
elif entry.action == discord.AuditLogAction.channel_delete:
trigger_punishment = await track_action(guild.id, entry.user.id, 'channel_delete')
elif entry.action == discord.AuditLogAction.channel_create:
trigger_punishment = await track_action(guild.id, entry.user.id, 'channel_create')
elif entry.action == discord.AuditLogAction.channel_update:
trigger_punishment = await track_action(guild.id, entry.user.id, 'channel_update')
if trigger_punishment:
try:
member = await guild.fetch_member(entry.user.id)
await punish_user(guild, member, guild_config['punishment'])
except Exception as e:
print(f'Error fetching or punishing member: {e}')
@bot.tree.command(name='antinuke', description='Anti-nuke protection system management')
@app_commands.describe(
action='Choose an action',
subaction='Additional action parameter',
user='Target user',
punishment_type='Type of punishment (kick/ban/timeout)'
)
async def antinuke(
interaction: discord.Interaction,
action: str,
subaction: str = None,
user: discord.Member = None,
punishment_type: str = None
):
if not interaction.guild:
await interaction.response.send_message('❌ This command can only be used in a server!', ephemeral=True)
return
if not is_authorized(interaction.guild, interaction.user):
await interaction.response.send_message('❌ You do not have permission to use this command! Only server owner, top administrators, or added admins can manage anti-nuke settings.', ephemeral=True)
return
guild_config = get_guild_config(interaction.guild.id)
if action == 'enable':