modified: bot.py

This commit is contained in:
SimolZimol
2026-06-15 21:21:36 +02:00
parent d0a3954f0a
commit 7a81efefcd

246
bot.py
View File

@@ -2874,139 +2874,139 @@ async def on_message(message):
guild_id = message.guild.id guild_id = message.guild.id
member = message.author # Das Member-Objekt für Datenaktualisierung member = message.author # Das Member-Objekt für Datenaktualisierung
# ── Honeypot check ────────────────────────────────────────────────────────── # ── Honeypot check ──────────────────────────────────────────────────────────
guild_settings = get_guild_settings(guild_id) guild_settings = get_guild_settings(guild_id)
if guild_settings.get("honeypot_enabled") and guild_settings.get("honeypot_channel_id"): if guild_settings.get("honeypot_enabled") and guild_settings.get("honeypot_channel_id"):
if message.channel.id == int(guild_settings["honeypot_channel_id"]): if message.channel.id == int(guild_settings["honeypot_channel_id"]):
# Build ignore-role set # Build ignore-role set
ignore_role_ids = set() ignore_role_ids = set()
raw_ignore = guild_settings.get("honeypot_ignore_roles") raw_ignore = guild_settings.get("honeypot_ignore_roles")
if raw_ignore: if raw_ignore:
try: try:
ignore_role_ids = {int(r) for r in json.loads(raw_ignore)} ignore_role_ids = {int(r) for r in json.loads(raw_ignore)}
except Exception: except Exception:
pass pass
member_role_ids = {role.id for role in member.roles} member_role_ids = {role.id for role in member.roles}
if not (ignore_role_ids & member_role_ids): if not (ignore_role_ids & member_role_ids):
# Delete the honeypot message # Delete the honeypot message
try: try:
await message.delete() await message.delete()
except Exception: except Exception:
pass pass
action_taken = None action_taken = None
acc_age_min = int(guild_settings.get("honeypot_acc_age_min") or 30) acc_age_min = int(guild_settings.get("honeypot_acc_age_min") or 30)
preserve = guild_settings.get("honeypot_preserve_old_accounts", False) preserve = guild_settings.get("honeypot_preserve_old_accounts", False)
# Determine: old-account mute vs ban # Determine: old-account mute vs ban
if preserve and member.joined_at: if preserve and member.joined_at:
now_aware = datetime.now(member.joined_at.tzinfo) now_aware = datetime.now(member.joined_at.tzinfo)
days_on_server = (now_aware - member.joined_at).days days_on_server = (now_aware - member.joined_at).days
is_old_account = days_on_server >= acc_age_min is_old_account = days_on_server >= acc_age_min
else: else:
is_old_account = False is_old_account = False
if is_old_account: if is_old_account:
# Apply 1-year (365 days) mute via the mute system # Apply 1-year (365 days) mute via the mute system
honeypot_role = None honeypot_role = None
hp_role_id = guild_settings.get("honeypot_get_role") hp_role_id = guild_settings.get("honeypot_get_role")
if hp_role_id: if hp_role_id:
try:
honeypot_role = message.guild.get_role(int(hp_role_id))
except Exception:
pass
if honeypot_role is None:
honeypot_role = await get_or_create_mute_role(message.guild, guild_settings)
if honeypot_role:
try:
await member.add_roles(
honeypot_role,
reason="Honeypot: wrote in honeypot channel (account protected as old member)"
)
# Persist mute record for 365 days
start_time = datetime.now()
end_time = start_time + timedelta(days=365)
process_data = {
"user_id": member.id,
"guild_id": guild_id,
"channel_id": message.channel.id,
"reason": "Honeypot trigger",
"moderator_id": client.user.id,
"mute_role_id": honeypot_role.id
}
process_uuid = create_active_process(
process_type="mute",
guild_id=guild_id,
channel_id=message.channel.id,
user_id=member.id,
target_id=member.id,
end_time=end_time,
data=process_data
)
await save_mute_to_database(
user_id=member.id,
guild_id=guild_id,
moderator_id=client.user.id,
reason="Honeypot: wrote in honeypot channel (account protected as old member)",
duration="365d",
start_time=start_time,
end_time=end_time,
process_uuid=process_uuid,
channel_id=message.channel.id,
mute_role_id=honeypot_role.id
)
action_taken = "mute"
except discord.Forbidden:
logger.warning(f"Honeypot: no permission to mute {member.id} in guild {guild_id}")
else:
try: try:
honeypot_role = message.guild.get_role(int(hp_role_id)) await message.guild.ban(
except Exception: member,
pass reason="Honeypot: wrote in honeypot channel",
if honeypot_role is None: delete_message_days=1
honeypot_role = await get_or_create_mute_role(message.guild, guild_settings)
if honeypot_role:
try:
await member.add_roles(
honeypot_role,
reason="Honeypot: wrote in honeypot channel (account protected as old member)"
) )
# Persist mute record for 365 days action_taken = "ban"
start_time = datetime.now()
end_time = start_time + timedelta(days=365)
process_data = {
"user_id": member.id,
"guild_id": guild_id,
"channel_id": message.channel.id,
"reason": "Honeypot trigger",
"moderator_id": client.user.id,
"mute_role_id": honeypot_role.id
}
process_uuid = create_active_process(
process_type="mute",
guild_id=guild_id,
channel_id=message.channel.id,
user_id=member.id,
target_id=member.id,
end_time=end_time,
data=process_data
)
await save_mute_to_database(
user_id=member.id,
guild_id=guild_id,
moderator_id=client.user.id,
reason="Honeypot: wrote in honeypot channel (account protected as old member)",
duration="365d",
start_time=start_time,
end_time=end_time,
process_uuid=process_uuid,
channel_id=message.channel.id,
mute_role_id=honeypot_role.id
)
action_taken = "mute"
except discord.Forbidden: except discord.Forbidden:
logger.warning(f"Honeypot: no permission to mute {member.id} in guild {guild_id}") logger.warning(f"Honeypot: no permission to ban {member.id} in guild {guild_id}")
else:
try:
await message.guild.ban(
member,
reason="Honeypot: wrote in honeypot channel",
delete_message_days=1
)
action_taken = "ban"
except discord.Forbidden:
logger.warning(f"Honeypot: no permission to ban {member.id} in guild {guild_id}")
# Send log embed # Send log embed
hp_log_ch_id = guild_settings.get("honeypot_log_channel_id") hp_log_ch_id = guild_settings.get("honeypot_log_channel_id")
if action_taken and hp_log_ch_id: if action_taken and hp_log_ch_id:
try: try:
log_ch = message.guild.get_channel(int(hp_log_ch_id)) log_ch = message.guild.get_channel(int(hp_log_ch_id))
if log_ch: if log_ch:
color = 0x8b0000 if action_taken == "ban" else 0xff9500 color = 0x8b0000 if action_taken == "ban" else 0xff9500
action_label = "🔨 Banned" if action_taken == "ban" else "🔇 Muted (1 year)" action_label = "🔨 Banned" if action_taken == "ban" else "🔇 Muted (1 year)"
embed = discord.Embed( embed = discord.Embed(
title="🍯 Honeypot triggered", title="🍯 Honeypot triggered",
description=f"{member.mention} wrote in the honeypot channel.", description=f"{member.mention} wrote in the honeypot channel.",
color=color, color=color,
timestamp=datetime.now() timestamp=datetime.now()
)
embed.add_field(name="Action", value=action_label, inline=True)
embed.add_field(name="User", value=f"{member} (`{member.id}`)", inline=True)
embed.add_field(name="Channel", value=f"<#{message.channel.id}>", inline=True)
if preserve:
embed.add_field(
name="Account age on server",
value=f"{days_on_server if is_old_account else 'N/A'} days (Minimum: {acc_age_min}d)",
inline=False
) )
if message.content: embed.add_field(name="Action", value=action_label, inline=True)
embed.add_field( embed.add_field(name="User", value=f"{member} (`{member.id}`)", inline=True)
name="Message content", embed.add_field(name="Channel", value=f"<#{message.channel.id}>", inline=True)
value=message.content[:500], if preserve:
inline=False embed.add_field(
) name="Account age on server",
embed.set_thumbnail(url=member.display_avatar.url) value=f"{days_on_server if is_old_account else 'N/A'} days (Minimum: {acc_age_min}d)",
await log_ch.send(embed=embed) inline=False
except Exception as e: )
logger.error(f"Honeypot: error sending log: {e}") if message.content:
embed.add_field(
name="Message content",
value=message.content[:500],
inline=False
)
embed.set_thumbnail(url=member.display_avatar.url)
await log_ch.send(embed=embed)
except Exception as e:
logger.error(f"Honeypot: error sending log: {e}")
return # Never process further for honeypot channel messages return # Never process further for honeypot channel messages
# ── End honeypot check ────────────────────────────────────────────────────── # ── End honeypot check ──────────────────────────────────────────────────────
cooldown_key = (user_id, guild_id) cooldown_key = (user_id, guild_id)
current_time = time.time() current_time = time.time()