diff --git a/cogs/automod.py b/cogs/automod.py index dd5eb4f..17cc9fc 100644 --- a/cogs/automod.py +++ b/cogs/automod.py @@ -28,12 +28,13 @@ async def automod(self, ctx: ApplicationContext): localembed.set_footer(text="More automod features will come soon!") await ctx.respond(embed=localembed) + # Swear-filter Commands @commands.slash_command( name="automod_swearfilter", description="Turn on or off automod's swear-filter in your server" ) @option(name="toggle", description="Do you want to turn it on or off?", type=bool) - async def automod_swearfilter(self, ctx: ApplicationContext, toggle:bool): + async def automod_swearfilter(self, ctx: ApplicationContext, toggle: bool): if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) if automod.fetch_config(ctx.guild.id)["swear_filter"]["enabled"] == toggle: return await ctx.respond(f"That automod option is already set to `{toggle}`.", ephemeral=True) automod.swearfilter_enabled(ctx.guild.id, toggle) @@ -45,7 +46,7 @@ async def automod_swearfilter(self, ctx: ApplicationContext, toggle:bool): description="Choose whether or not you want to use the default keywords for automod's swear-filter" ) @option(name="toggle", description="Do you want to turn it on or off?", type=bool) - async def automod_use_default_keywords(self, ctx: ApplicationContext, toggle:bool): + async def automod_use_default_keywords(self, ctx: ApplicationContext, toggle: bool): if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) if automod.fetch_config(ctx.guild.id)["swear_filter"]["keywords"]["use_default"] == toggle: return await ctx.respond(f"That automod option is already set to `{toggle}`.", ephemeral=True) automod.swearfilter_usedefaultkeywords(ctx.guild.id, toggle) @@ -63,7 +64,7 @@ async def automod_view_custom_keywords(self, ctx: ApplicationContext): i = 0 for x in loaded_config["swear_filter"]["keywords"]["custom"]: i += 1 - out += f"**{i})** {x}\n" + out += f"{i}. {x}\n" else: out = "*No custom keywords are set for your server.*" localembed = discord.Embed(title=f"Custom Swear-filter keywords for {ctx.guild.name}", description=out, color=color) localembed.set_footer(icon_url=ctx.author.avatar_url, text=f"Requested by {ctx.author}") @@ -87,11 +88,121 @@ async def automod_add_custom_keyword(self, ctx: ApplicationContext, keyword:str) name="automod_remove_custom_keyword", description="Removes a custom keyword (matching its id) from your server's swear-filter" ) - @option(name="id", description="What's the id of the keyword to remove (can be found in bold through /automod_view_custom_keywords", type=int) + @option(name="id", description="What's the id of the keyword to remove (can be found as serial number through /automod_view_custom_keywords", type=int) async def automod_remove_custom_keyword(self, ctx: ApplicationContext, id: int): + if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) try: automod.swearfilter_removekeyword(ctx.guild.id, id) return await ctx.respond(f"Keyword (id: `{id}`) successfully removed from swear-filter configuration.") except IndexError: await ctx.respond("That keyword id doesn't exist. Please specify a valid id and try again.", ephemeral=True) + # Link Blocker Commands + @commands.slash_command( + name="automod_linkblocker", + description="Turn on or off automod's link blocker in your server" + ) + @option(name="toggle", description="Do you want to turn it on or off?", type=bool) + async def automod_linkblocker(self, ctx: ApplicationContext, toggle: bool): + if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) + if automod.fetch_config(ctx.guild.id)["link_blocker"]["enabled"] == toggle: return await ctx.respond(f"That automod option is already set to `{toggle}`.", ephemeral=True) + automod.linkblocker_enabled(ctx.guild.id, toggle) + if toggle is True: await ctx.respond("Link blocker successfully **enabled**.", ephemeral=True) + elif toggle is False: await ctx.respond("Link blocker successfully **disabled**.", ephemeral=True) + + @commands.slash_command( + name="automod_linkblocker_only_whitelisted_links", + description="Only allows whitelisted links in the server and blocks all other links" + ) + @option(name="toggle", description="Do you want to turn it on or off?", type=bool) + async def automod_linkblocker_only_whitelisted_links(self, ctx: ApplicationContext, toggle: bool): + if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) + if automod.fetch_config(ctx.guild.id)["link_blocker"]["use_whitelist_only"] == toggle: return await ctx.respond(f"That automod option is already set to `{toggle}`.", ephemeral=True) + automod.linkblocker_enabled(ctx.guild.id, toggle) + if toggle is True: await ctx.respond("Link blocker successfully **enabled**.", ephemeral=True) + elif toggle is False: await ctx.respond("Link blocker successfully **disabled**.", ephemeral=True) + + @commands.slash_command( + name="automod_linkblocker_add_whitelist", + description="Adds a link to your server link blocker's whitelist." + ) + @option(name="link", description="The link that you want to add (must be in form of https://{url})", type=str) + async def automod_linkblocker_add_whitelist(self, ctx: ApplicationContext, link: str): + if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) + if link in automod.fetch_config(ctx.guild.id)["link_blocker"]["whitelist"]: return await ctx.respond("This link is already in your server's link blocker whitelist.", ephemeral=True) + if "https://" in link or "http://" in link: + automod.linkblocker_add_whitelisted(link) + await ctx.respond(f"Link `{link}` has successfully been added to whitelist.", ephemeral=True) + else: return await ctx.respond(":warning: The link you entered is not formatted correctly. All added links must contain `https://`.") + + @commands.slash_command( + name="automod_linkblocker_add_blacklist", + description="Adds a link to your server link blocker's blacklist." + ) + @option(name="link", description="The link that you want to add (must be in form of https://{url})", type=str) + async def automod_linkblocker_add_blacklist(self, ctx: ApplicationContext, link: str): + if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) + if link in automod.fetch_config(ctx.guild.id)["link_blocker"]["blacklist"]: return await ctx.respond("This link is already in your server's link blocker blacklist.", ephemeral=True) + if "https://" in link or "http://" in link: + automod.linkblocker_add_blacklisted(link) + await ctx.respond(f"Link `{link}` has successfully been added to blacklist.", ephemeral=True) + else: return await ctx.respond(":warning: The link you entered is not formatted correctly. All added links must contain `https://`.") + + @commands.slash_command( + name="automod_view_whitelisted_links", + description="Shows a list of the whitelisted links set for this server", + ) + async def automod_view_custom_keywords(self, ctx: ApplicationContext): + loaded_config = automod.fetch_config(ctx.guild.id) + out = "" + if loaded_config["link_blocker"]["whitelisted"] != []: + i = 0 + for x in loaded_config["link_blocker"]["whitelisted"]: + i += 1 + out += f"{i}. {x}\n" + else: out = "*No whitelisted links are set for your server.*" + localembed = discord.Embed(title=f"Whitelisted links for {ctx.guild.name}", description=out, color=color) + localembed.set_footer(icon_url=ctx.author.avatar_url, text=f"Requested by {ctx.author}") + await ctx.respond(embed=localembed) + + @commands.slash_command( + name="automod_view_blacklisted_links", + description="Shows a list of the blacklisted links set for this server", + ) + async def automod_view_custom_keywords(self, ctx: ApplicationContext): + loaded_config = automod.fetch_config(ctx.guild.id) + out = "" + if loaded_config["link_blocker"]["blacklisted"] != []: + i = 0 + for x in loaded_config["link_blocker"]["blacklisted"]: + i += 1 + out += f"{i}. {x}\n" + else: out = "*No blacklisted links are set for your server.*" + localembed = discord.Embed(title=f"Blacklisted links for {ctx.guild.name}", description=out, color=color) + localembed.set_footer(icon_url=ctx.author.avatar_url, text=f"Requested by {ctx.author}") + await ctx.respond(embed=localembed) + + @commands.slash_command( + name="automod_linkblocker_remove_blacklist", + description="Removes a blacklisted link (matching its id) from this server's link blocker" + ) + @option(name="id", description="What's the id of the link to remove? (can be found as serial number through /automod_view_blacklisted_links", type=int) + async def automod_linkblocker_remove_blacklist(self, ctx: ApplicationContext, id: int): + if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) + try: + automod.linkblocker_remove_blacklisted(ctx.guild.id, id) + return await ctx.respond(f"Blacklisted link (id: `{id}`) successfully removed from link blocker.") + except IndexError: await ctx.respond("That blacklisted link id doesn't exist. Please specify a valid id and try again.", ephemeral=True) + + @commands.slash_command( + name="automod_linkblocker_remove_whitelist", + description="Removes a whitelisted link (matching its id) from this server's link blocker" + ) + @option(name="id", description="What's the id of the link to remove? (can be found as serial number through /automod_view_whitelisted_links", type=int) + async def automod_linkblocker_remove_whitelist(self, ctx: ApplicationContext, id: int): + if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True) + try: + automod.linkblocker_remove_whitelisted(ctx.guild.id, id) + return await ctx.respond(f"Whitelisted link (id: `{id}`) successfully removed from link blocker.") + except IndexError: await ctx.respond("That whitelisted link id doesn't exist. Please specify a valid id and try again.", ephemeral=True) + def setup(bot): bot.add_cog(Automod(bot)) diff --git a/config/commands.json b/config/commands.json index 4152106..af283c3 100644 --- a/config/commands.json +++ b/config/commands.json @@ -529,6 +529,16 @@ "disabled": false, "bugged": false }, + "automod_linkblocker": { + "name": "Toggle Automod Link Blocker", + "description": "Lets you turn on or off the automod link-blocker for your server.", + "type": "automod", + "cooldown": null, + "args": ["toggle"], + "usable_by": "server admins", + "disabled": false, + "bugged": false + }, "squareroot": { "name": "Square Root", "description": "Finds the square root of any positive number.", diff --git a/framework/isobot/db/automod.py b/framework/isobot/db/automod.py index 564e773..a929be1 100644 --- a/framework/isobot/db/automod.py +++ b/framework/isobot/db/automod.py @@ -32,6 +32,12 @@ def generate(self, server_id: int) -> int: "default": ["fuck", "shit", "pussy", "penis", "cock", "vagina", "sex", "moan", "bitch", "hoe", "nigga", "nigger", "xxx", "porn"], "custom": [] } + }, + "link_blocker": { + "enabled": False, + "use_whitelist_only": True, + "whitelisted": [], + "blacklisted": [] } } self.save(automod_config) @@ -69,3 +75,47 @@ def swearfilter_removekeyword(self, server_id: int, keyword_id: int) -> int: data.pop(id-1) self.save(automod_config) return 0 + + def linkblocker_enabled(self, server_id: int, value: bool) -> int: + """Sets a `bool` value to define whether the server's link blocker is enabled or not.""" + automod_config = self.load() + automod_config[str(server_id)]["link_blocker"]["enabled"] = value + self.save(automod_config) + return 0 + + def linkblocker_only_whitelisted_links(self, server_id: int, value: bool) -> int: + """Sets a `bool` value to define whether the server's link blocker only accepts whitelisted links.""" + automod_config = self.load() + automod_config[str(server_id)]["link_blocker"]["use_whitelisted_only"] = value + self.save(automod_config) + return 0 + + def linkblocker_add_whitelisted(self, server_id: int, link: str) -> int: + """Adds a specified link to the server links whitelist. (only works if `use_whitelisted_only = True`)""" + automod_config = self.load() + automod_config[str(server_id)]["link_blocker"]["whitelisted"].append(link) + self.save(automod_config) + return 0 + + def linkblocker_add_blacklisted(self, server_id: int, link: str) -> int: + """Adds a specified link to the server links blacklist. (only works if `use_whitelisted_only = False`)""" + automod_config = self.load() + automod_config[str(server_id)]["link_blocker"]["blacklisted"].append(link) + self.save(automod_config) + return 0 + + def linkblocker_remove_whitelisted(self, server_id: int, keyword_id: int) -> int: + """Removes a whitelisted link (using id) from the server's automod configuration.""" + automod_config = self.load() + data: dict = automod_config[str(server_id)]["link_blocker"]["whitelisted"] + data.pop(keyword_id-1) + self.save(automod_config) + return 0 + + def linkblocker_remove_blacklisted(self, server_id: int, keyword_id: int) -> int: + """Removes a blacklisted link (using id) from the server's automod configuration.""" + automod_config = self.load() + data: dict = automod_config[str(server_id)]["link_blocker"]["blacklisted"] + data.pop(keyword_id-1) + self.save(automod_config) + return 0 diff --git a/main.py b/main.py index 3e395ad..460ae28 100644 --- a/main.py +++ b/main.py @@ -178,6 +178,7 @@ async def on_message(ctx): except discord.errors.Forbidden: logger.warn("Unable to send level up message to {ctx.author} ({ctx.author.id}), as they are not accepting DMs from isobot. This ID has been added to `levelup_messages` blacklist.", module="main/Levelling") settings.edit_setting(ctx.author.id, "levelup_messages", False) + # Swear-filter try: automod_config = automod.fetch_config(ctx.guild.id) if automod_config["swear_filter"]["enabled"] is True: @@ -186,6 +187,23 @@ async def on_message(ctx): await ctx.channel.send(f'{ctx.author.mention} watch your language.', delete_after=5) except AttributeError: pass + # Link Blocker + try: + if ("http://" in ctx.content.lower()) or ("https://" in ctx.content.lower()): + automod_config = automod.fetch_config(ctx.guild.id) + if automod_config["link_blocker"]["enabled"] is True: + if automod_config["link_blocker"]["use_whitelist_only"]: + if not any(x in ctx.content.lower() for x in automod_config["link_blocker"]["whitelisted"]["default"]): + await ctx.delete() + await ctx.channel.send(f'{ctx.author.mention} This link is not allowed in this server.', delete_after=5) + else: + if any(x in ctx.content.lower() for x in automod_config["link_blocker"]["blacklisted"]["default"]): + await ctx.delete() + await ctx.channel.send(f'{ctx.author.mention} This link is not allowed in this server.', delete_after=5) + except AttributeError: pass + + + @client.event async def after_invoke(ctx): logger.info(f"A command has been successfully run by {ctx.author.display_name}", module="main/Client", timestamp=True)