147 lines
4.9 KiB
Python
147 lines
4.9 KiB
Python
import os
|
|
import datetime
|
|
import discord
|
|
from discord.ext import commands, tasks
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
HONEYPOT_CHANNEL_ID = os.getenv("HONEYPOT_CHANNEL_ID", "").strip()
|
|
PURGE_MINUTES = 15
|
|
|
|
|
|
def parse_channel_id(raw_value: str) -> int | None:
|
|
if not raw_value:
|
|
return None
|
|
|
|
try:
|
|
return int(raw_value)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
class Honeypot(commands.Cog):
|
|
def __init__(self, bot: commands.Bot):
|
|
self.bot = bot
|
|
self.channel_id = parse_channel_id(HONEYPOT_CHANNEL_ID)
|
|
|
|
async def cog_load(self):
|
|
if self.channel_id is None:
|
|
print("Honeypot disabled: HONEYPOT_CHANNEL_ID is missing or invalid.")
|
|
return
|
|
|
|
print(f"Honeypot watching channel ID {self.channel_id}")
|
|
self.startup.start()
|
|
|
|
def cog_unload(self):
|
|
self.startup.cancel()
|
|
|
|
@tasks.loop(count=1)
|
|
async def startup(self):
|
|
await self.bot.wait_until_ready()
|
|
|
|
channel = self.bot.get_channel(self.channel_id)
|
|
if channel is None:
|
|
try:
|
|
channel = await self.bot.fetch_channel(self.channel_id)
|
|
except discord.HTTPException as error:
|
|
print(f"Honeypot: could not fetch channel {self.channel_id}: {error}")
|
|
return
|
|
|
|
if not isinstance(channel, discord.TextChannel):
|
|
print(f"Honeypot: channel {self.channel_id} is not a text channel.")
|
|
return
|
|
|
|
try:
|
|
await channel.purge(limit=None, reason="Honeypot startup cleanup")
|
|
|
|
embed = discord.Embed(
|
|
title="Nicht hier schreiben / Do not post here",
|
|
description=(
|
|
"Dieser Kanal ist ein honeypot. Jede Nachricht wird gelöscht und "
|
|
"das Mitglied wird gekickt.\n\n"
|
|
"This channel is a honeypot. Any message posted here will be "
|
|
"deleted and the member will be kicked.\n\n"
|
|
"**Nicht in diesen Kanal schreiben. / Do not write in this channel.**"
|
|
),
|
|
color=discord.Color.red(),
|
|
)
|
|
|
|
await channel.send(embed=embed)
|
|
except discord.HTTPException as error:
|
|
print(f"Honeypot: failed to prepare channel {self.channel_id}: {error}")
|
|
|
|
async def purge_recent_user_messages(self, guild: discord.Guild, user_id: int):
|
|
cutoff = discord.utils.utcnow() - datetime.timedelta(minutes=PURGE_MINUTES)
|
|
channels = [
|
|
*guild.text_channels,
|
|
*guild.voice_channels,
|
|
*guild.stage_channels,
|
|
*guild.threads,
|
|
]
|
|
|
|
for channel in channels:
|
|
if guild.me is None:
|
|
return
|
|
|
|
permissions = channel.permissions_for(guild.me)
|
|
if not (
|
|
permissions.view_channel
|
|
and permissions.read_message_history
|
|
and permissions.manage_messages
|
|
):
|
|
continue
|
|
|
|
try:
|
|
deleted = await channel.purge(
|
|
limit=None,
|
|
after=cutoff,
|
|
check=lambda message: message.author.id == user_id,
|
|
reason="Honeypot cleanup",
|
|
)
|
|
if deleted:
|
|
print(f"Honeypot: removed {len(deleted)} message(s) in #{channel}")
|
|
except discord.HTTPException as error:
|
|
print(f"Honeypot: could not purge messages in #{channel}: {error}")
|
|
|
|
@commands.Cog.listener()
|
|
async def on_message(self, message: discord.Message):
|
|
if self.channel_id is None or message.guild is None:
|
|
return
|
|
|
|
parent_id = getattr(message.channel, "parent_id", None)
|
|
if message.channel.id != self.channel_id and parent_id != self.channel_id:
|
|
return
|
|
|
|
if self.bot.user is not None and message.author.id == self.bot.user.id:
|
|
return
|
|
|
|
reason = f"Honeypot triggered in #{message.channel.name}"
|
|
|
|
try:
|
|
await message.delete()
|
|
except discord.HTTPException:
|
|
pass
|
|
|
|
member = message.author
|
|
if not isinstance(member, discord.Member):
|
|
try:
|
|
member = await message.guild.fetch_member(member.id)
|
|
except discord.HTTPException:
|
|
print(f"Honeypot: could not resolve member {message.author.id}")
|
|
return
|
|
|
|
try:
|
|
await member.kick(reason=reason)
|
|
print(f"Honeypot: kicked {member} ({member.id}) for posting in #{message.channel}")
|
|
except discord.Forbidden:
|
|
print(f"Honeypot: missing permissions to kick {member} ({member.id})")
|
|
except discord.HTTPException as error:
|
|
print(f"Honeypot: failed to kick {member} ({member.id}): {error}")
|
|
|
|
await self.purge_recent_user_messages(message.guild, member.id)
|
|
|
|
|
|
async def setup(bot: commands.Bot):
|
|
await bot.add_cog(Honeypot(bot))
|