diff options
author | Arjun Satarkar <me@arjunsatarkar.net> | 2024-03-06 18:03:12 +0000 |
---|---|---|
committer | Arjun Satarkar <me@arjunsatarkar.net> | 2024-03-06 18:09:28 +0000 |
commit | 8b9610071d7cfb063499061a095ca088469f33ab (patch) | |
tree | 425bb9bcd31be36c5c780764f890f1c21a20f378 | |
parent | 187554f688cd7c7538b65908d6844cf3b5c818ef (diff) | |
download | aps-cogs-8b9610071d7cfb063499061a095ca088469f33ab.tar aps-cogs-8b9610071d7cfb063499061a095ca088469f33ab.tar.gz aps-cogs-8b9610071d7cfb063499061a095ca088469f33ab.zip |
markov: add ignored_string commands
-rw-r--r-- | markov/markov.py | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/markov/markov.py b/markov/markov.py index d5ed7bf..bb006df 100644 --- a/markov/markov.py +++ b/markov/markov.py @@ -8,6 +8,7 @@ import random import re from .errors import * +MAX_IGNORED_STRINGS_PER_GUILD = 50 MAX_TOKEN_GENERATION_ITERATIONS = 1000 MAX_WORD_LENGTH = 50 @@ -18,7 +19,7 @@ class Markov(commands.Cog): self.config = Config.get_conf( self, identifier="551742410770612234|085c218a-e850-4b07-9fc9-535c1b0d4c73" ) - self.config.register_guild(use_messages=False) + self.config.register_guild(use_messages=False, ignored_strings=[]) self.config.register_member(use_messages=True) self.config.register_channel(use_messages=False) @@ -47,6 +48,10 @@ class Markov(commands.Cog): ) async def process_message(self, clean_content: str, guild_id: int, member_id: int): + # Strip out ignored strings + for ignored_string in await self.config.guild_from_id(guild_id).ignored_strings(): + clean_content = clean_content.replace(ignored_string, "") + # Strip out URL-esque patterns - a run of characters without spaces that contains '://' within it clean_content = re.sub(r"(?: |^)\w+:\/\/[^ ]+(?: |$)", " ", clean_content) @@ -194,6 +199,62 @@ class Markov(commands.Cog): f"The markov cog is now {'enabled' if new_state else 'disabled'} in this guild." ) + @markov.group() + async def ignore_string(self, _ctx): + pass + + @ignore_string.command() + @commands.admin_or_permissions(manage_guild=True) + async def add(self, ctx, *, ignored_string: str): + """ + Replace every instance of this string with the empty string before further + processing a message. WARNING: this can mess up your tokenization if + misused. I recommend breaking on a word boundary. + """ + if not ignored_string: + await ctx.reply("Error: ignored_string must have length greater than 0") + return + async with self.config.guild(ctx.guild).ignored_strings() as ignored_strings: + if len(ignored_strings) >= MAX_IGNORED_STRINGS_PER_GUILD: + await ctx.reply( + "Error: you already have the maximum number of ignored strings in this guild" + f" ({MAX_IGNORED_STRINGS_PER_GUILD})." + ) + return + ignored_strings.append(ignored_string) + await ctx.reply(f"Added that to the excluded strings for this guild.") + + @ignore_string.command() + @commands.admin_or_permissions(manage_guild=True) + async def remove(self, ctx, num: int): + """ + Remove ignored string with ID num. You can see the IDs in `markov ignored_string list`. + """ + async with self.config.guild(ctx.guild).ignored_strings() as ignored_strings: + string = ignored_strings[num - 1] + try: + del ignored_strings[num - 1] + except IndexError: + await ctx.reply("Error: no ignored string with that ID existed!") + else: + await ctx.reply(f"Removed {repr(string)} from your ignored strings.") + + @ignore_string.command() + @commands.admin_or_permissions(manage_guild=True) + async def list(self, ctx): + text = "" + for i, question in enumerate( + await self.config.guild(ctx.guild).ignored_strings() + ): + text += f"{i + 1}. {question}\n" + pages = list(redbot.core.utils.chat_formatting.pagify(text)) + + if pages: + message = await ctx.reply(".") + await redbot.core.utils.menus.menu(ctx, pages, message=message) + else: + await ctx.reply("No ignored strings yet.") + @markov.command() async def generate(self, ctx, member: discord.Member | None): if not await self.config.guild(ctx.guild).use_messages(): |