From 4b5d6816b01a3c0a503d4f65b6b2429c1ce5bcf1 Mon Sep 17 00:00:00 2001 From: Alex <42661490+ThatRedox@users.noreply.github.com> Date: Sun, 17 Mar 2024 14:35:51 -0700 Subject: [PATCH] Implement blocking @ everyone and here. Add custom block regexes. (#33) --- config.ini | 2 ++ src/main.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/config.ini b/config.ini index 96814d2..4995300 100644 --- a/config.ini +++ b/config.ini @@ -18,3 +18,5 @@ suspicious = https://raw.githubusercontent.com/nikolaischunk/discord-phishing-li # Update lists every 24 * 60 * 60 seconds update = 86400 enabled = true + +[BLOCK] diff --git a/src/main.py b/src/main.py index cb946b3..77e476e 100644 --- a/src/main.py +++ b/src/main.py @@ -42,13 +42,16 @@ class Bot(discord.Client): GH_REGEX = re.compile(r"(\\)?(([a-zA-Z\d]{1}[-a-zA-Z\d]+)/)?([\-\w]+)?#(\d+)") def __init__(self, gh: github.Github, default_org: str, default_repo: str, - image_only: List[Tuple[int, str]], *args, **kwargs): + image_only: List[Tuple[int, str]], + block_regex: List[Tuple[str, re.Pattern]], + *args, **kwargs): super().__init__(*args, **kwargs) self._gh = gh self._default_org = default_org self._default_repo = default_repo self._image_only = image_only self._logger = logging.getLogger("bot") + self._blocks = block_regex async def on_ready(self): await BOT_LOG.register(self) @@ -122,6 +125,29 @@ async def on_message(self, message: discord.Message): f"{message.content}") await BOT_LOG.log(lambda: self._log_spam(message, False)) + # Check for @everyone (and failed) + if not message.mention_everyone and ("@everyone" in message.content or "@here" in message.content): + self._logger.info(f"Removing message {message.id} by " + f"{message.author.name} " + f"#{message.author.discriminator} " + f"({message.author.id}) for spam (@everyone/@here): " + f"{message.content}") + await BOT_LOG.log(lambda: self._log_spam(message, True)) + await message.delete() + return + + # Check any block regexes + for name, regex in self._blocks: + if regex.match(message.content): + self._logger.info(f"Removing message {message.id} by " + f"{message.author.name} " + f"#{message.author.discriminator} " + f"({message.author.id}) for spam regex ({name}): " + f"{message.content}") + await BOT_LOG.log(lambda: self._log_spam(message, True)) + await message.delete() + return + # Check if we are in the renderers channel for channel, warn in self._image_only: if message.channel.id == channel: @@ -312,6 +338,12 @@ def main(): config = configparser.ConfigParser() config.read(args.config) + # Block regexes + block_regex = [] + if "BLOCK" in config: + for tag, regex in config["BLOCK"].items(): + block_regex.append((tag, re.compile(regex),)) + # Spam lists if "SPAM" in config: if config["SPAM"].get("enabled", "false").lower() == "true": @@ -360,7 +392,7 @@ def main(): logging.getLogger("bot").warning("Config does not contain an [IMAGE_ONLY] " "section. Bot will not filter any channels.") - bot = Bot(gh, config["GITHUB"]["organization"], config["GITHUB"]["repository"], image_only) + bot = Bot(gh, config["GITHUB"]["organization"], config["GITHUB"]["repository"], image_only, block_regex) _slash = Slash(gh, config["GITHUB"]["organization"], config["GITHUB"]["repository"], image_only, client=bot, debug_guild=args.debug_guild, sync_commands=True)