modified: bot.py
This commit is contained in:
246
bot.py
246
bot.py
@@ -2874,139 +2874,139 @@ async def on_message(message):
|
||||
guild_id = message.guild.id
|
||||
member = message.author # Das Member-Objekt für Datenaktualisierung
|
||||
|
||||
# ── Honeypot check ──────────────────────────────────────────────────────────
|
||||
guild_settings = get_guild_settings(guild_id)
|
||||
if guild_settings.get("honeypot_enabled") and guild_settings.get("honeypot_channel_id"):
|
||||
if message.channel.id == int(guild_settings["honeypot_channel_id"]):
|
||||
# Build ignore-role set
|
||||
ignore_role_ids = set()
|
||||
raw_ignore = guild_settings.get("honeypot_ignore_roles")
|
||||
if raw_ignore:
|
||||
try:
|
||||
ignore_role_ids = {int(r) for r in json.loads(raw_ignore)}
|
||||
except Exception:
|
||||
pass
|
||||
# ── Honeypot check ──────────────────────────────────────────────────────────
|
||||
guild_settings = get_guild_settings(guild_id)
|
||||
if guild_settings.get("honeypot_enabled") and guild_settings.get("honeypot_channel_id"):
|
||||
if message.channel.id == int(guild_settings["honeypot_channel_id"]):
|
||||
# Build ignore-role set
|
||||
ignore_role_ids = set()
|
||||
raw_ignore = guild_settings.get("honeypot_ignore_roles")
|
||||
if raw_ignore:
|
||||
try:
|
||||
ignore_role_ids = {int(r) for r in json.loads(raw_ignore)}
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
member_role_ids = {role.id for role in member.roles}
|
||||
if not (ignore_role_ids & member_role_ids):
|
||||
# Delete the honeypot message
|
||||
try:
|
||||
await message.delete()
|
||||
except Exception:
|
||||
pass
|
||||
member_role_ids = {role.id for role in member.roles}
|
||||
if not (ignore_role_ids & member_role_ids):
|
||||
# Delete the honeypot message
|
||||
try:
|
||||
await message.delete()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
action_taken = None
|
||||
acc_age_min = int(guild_settings.get("honeypot_acc_age_min") or 30)
|
||||
preserve = guild_settings.get("honeypot_preserve_old_accounts", False)
|
||||
action_taken = None
|
||||
acc_age_min = int(guild_settings.get("honeypot_acc_age_min") or 30)
|
||||
preserve = guild_settings.get("honeypot_preserve_old_accounts", False)
|
||||
|
||||
# Determine: old-account mute vs ban
|
||||
if preserve and member.joined_at:
|
||||
now_aware = datetime.now(member.joined_at.tzinfo)
|
||||
days_on_server = (now_aware - member.joined_at).days
|
||||
is_old_account = days_on_server >= acc_age_min
|
||||
else:
|
||||
is_old_account = False
|
||||
# Determine: old-account mute vs ban
|
||||
if preserve and member.joined_at:
|
||||
now_aware = datetime.now(member.joined_at.tzinfo)
|
||||
days_on_server = (now_aware - member.joined_at).days
|
||||
is_old_account = days_on_server >= acc_age_min
|
||||
else:
|
||||
is_old_account = False
|
||||
|
||||
if is_old_account:
|
||||
# Apply 1-year (365 days) mute via the mute system
|
||||
honeypot_role = None
|
||||
hp_role_id = guild_settings.get("honeypot_get_role")
|
||||
if hp_role_id:
|
||||
if is_old_account:
|
||||
# Apply 1-year (365 days) mute via the mute system
|
||||
honeypot_role = None
|
||||
hp_role_id = guild_settings.get("honeypot_get_role")
|
||||
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:
|
||||
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)"
|
||||
await message.guild.ban(
|
||||
member,
|
||||
reason="Honeypot: wrote in honeypot channel",
|
||||
delete_message_days=1
|
||||
)
|
||||
# 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"
|
||||
action_taken = "ban"
|
||||
except discord.Forbidden:
|
||||
logger.warning(f"Honeypot: no permission to mute {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}")
|
||||
logger.warning(f"Honeypot: no permission to ban {member.id} in guild {guild_id}")
|
||||
|
||||
# Send log embed
|
||||
hp_log_ch_id = guild_settings.get("honeypot_log_channel_id")
|
||||
if action_taken and hp_log_ch_id:
|
||||
try:
|
||||
log_ch = message.guild.get_channel(int(hp_log_ch_id))
|
||||
if log_ch:
|
||||
color = 0x8b0000 if action_taken == "ban" else 0xff9500
|
||||
action_label = "🔨 Banned" if action_taken == "ban" else "🔇 Muted (1 year)"
|
||||
embed = discord.Embed(
|
||||
title="🍯 Honeypot triggered",
|
||||
description=f"{member.mention} wrote in the honeypot channel.",
|
||||
color=color,
|
||||
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
|
||||
# Send log embed
|
||||
hp_log_ch_id = guild_settings.get("honeypot_log_channel_id")
|
||||
if action_taken and hp_log_ch_id:
|
||||
try:
|
||||
log_ch = message.guild.get_channel(int(hp_log_ch_id))
|
||||
if log_ch:
|
||||
color = 0x8b0000 if action_taken == "ban" else 0xff9500
|
||||
action_label = "🔨 Banned" if action_taken == "ban" else "🔇 Muted (1 year)"
|
||||
embed = discord.Embed(
|
||||
title="🍯 Honeypot triggered",
|
||||
description=f"{member.mention} wrote in the honeypot channel.",
|
||||
color=color,
|
||||
timestamp=datetime.now()
|
||||
)
|
||||
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}")
|
||||
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="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
|
||||
# ── End honeypot check ──────────────────────────────────────────────────────
|
||||
return # Never process further for honeypot channel messages
|
||||
# ── End honeypot check ──────────────────────────────────────────────────────
|
||||
cooldown_key = (user_id, guild_id)
|
||||
current_time = time.time()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user