From 1fc0aec8f60baec38a47a839012f1f0d15f58d6b Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Sun, 20 Mar 2022 00:26:54 -0600 Subject: [PATCH] Add thread option to autoreact --- jarvis/client.py | 5 +++ jarvis/cogs/autoreact.py | 74 +++++++++++++++++++++++++--------------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/jarvis/client.py b/jarvis/client.py index 80c1c9d..c0ebc81 100644 --- a/jarvis/client.py +++ b/jarvis/client.py @@ -181,6 +181,11 @@ class Jarvis(Snake): if autoreact: for reaction in autoreact.reactions: await message.add_reaction(reaction) + if autoreact.thread: + name = message.content + if len(name) > 100: + name = name[:97] + "..." + await message.create_thread(name=message.content, reason="Autoreact") async def checks(self, message: Message) -> None: """Other message checks.""" diff --git a/jarvis/cogs/autoreact.py b/jarvis/cogs/autoreact.py index d5d127b..b5caa19 100644 --- a/jarvis/cogs/autoreact.py +++ b/jarvis/cogs/autoreact.py @@ -26,7 +26,7 @@ class AutoReactCog(Scale): self.custom_emote = re.compile(r"^<:\w+:(\d+)>$") async def create_autoreact( - self, ctx: InteractionContext, channel: GuildText + self, ctx: InteractionContext, channel: GuildText, thread: bool ) -> Tuple[bool, Optional[str]]: """ Create an autoreact monitor on a channel. @@ -34,6 +34,7 @@ class AutoReactCog(Scale): Args: ctx: Interaction context of command channel: Channel to monitor + thread: Create a thread Returns: Tuple of success? and error message @@ -42,12 +43,13 @@ class AutoReactCog(Scale): if exists: return False, f"Autoreact already exists for {channel.mention}." - _ = Autoreact( + await Autoreact( guild=ctx.guild.id, channel=channel.id, reactions=[], + thread=thread, admin=ctx.author.id, - ).save() + ).commit() return True, None @@ -62,7 +64,11 @@ class AutoReactCog(Scale): Returns: Success? """ - return Autoreact.objects(guild=ctx.guild.id, channel=channel.id).delete() is not None + ar = await Autoreact.find_one(q(guild=ctx.guild.id, channel=channel.id)) + if ar: + await ar.delete() + return True + return False @slash_command( name="autoreact", @@ -76,43 +82,55 @@ class AutoReactCog(Scale): required=True, ) @slash_option( - name="emote", description="Emote to add", opt_type=OptionTypes.STRING, required=True + name="thread", description="Create a thread?", opt_type=OptionTypes.BOOLEAN, required=False + ) + @slash_option( + name="emote", description="Emote to add", opt_type=OptionTypes.STRING, required=False ) @check(admin_or_permissions(Permissions.MANAGE_GUILD)) - async def _autoreact_add(self, ctx: InteractionContext, channel: GuildText, emote: str) -> None: + async def _autoreact_add( + self, ctx: InteractionContext, channel: GuildText, thread: bool = True, emote: str = None + ) -> None: await ctx.defer() - custom_emoji = self.custom_emote.match(emote) - standard_emoji = emote in emoji_list - if not custom_emoji and not standard_emoji: - await ctx.send( - "Please use either an emote from this server or a unicode emoji.", - ephemeral=True, - ) - return - if custom_emoji: - emoji_id = int(custom_emoji.group(1)) - if not find(lambda x: x.id == emoji_id, ctx.guild.emojis): - await ctx.send("Please use a custom emote from this server.", ephemeral=True) + if emote: + custom_emoji = self.custom_emote.match(emote) + standard_emoji = emote in emoji_list + if not custom_emoji and not standard_emoji: + await ctx.send( + "Please use either an emote from this server or a unicode emoji.", + ephemeral=True, + ) return + if custom_emoji: + emoji_id = int(custom_emoji.group(1)) + if not find(lambda x: x.id == emoji_id, await ctx.guild.fetch_all_custom_emojis()): + await ctx.send("Please use a custom emote from this server.", ephemeral=True) + return autoreact = await Autoreact.find_one(q(guild=ctx.guild.id, channel=channel.id)) if not autoreact: - self.create_autoreact(ctx, channel) + await self.create_autoreact(ctx, channel, thread) autoreact = await Autoreact.find_one(q(guild=ctx.guild.id, channel=channel.id)) - if emote in autoreact.reactions: + if emote and emote in autoreact.reactions: await ctx.send( f"Emote already added to {channel.mention} autoreactions.", ephemeral=True, ) return - if len(autoreact.reactions) >= 5: + if emote and len(autoreact.reactions) >= 5: await ctx.send( "Max number of reactions hit. Remove a different one to add this one", ephemeral=True, ) return - autoreact.reactions.append(emote) - autoreact.save() - await ctx.send(f"Added {emote} to {channel.mention} autoreact.") + if emote: + autoreact.reactions.append(emote) + autoreact.thread = thread + await autoreact.commit() + message = "" + if emote: + message += f" Added {emote} to {channel.mention} autoreact." + message += f" Set autoreact thread creation to {thread} in {channel.mention}" + await ctx.send(message) @slash_command( name="autoreact", @@ -143,7 +161,7 @@ class AutoReactCog(Scale): ) return if emote.lower() == "all": - self.delete_autoreact(ctx, channel) + await self.delete_autoreact(ctx, channel) await ctx.send(f"Autoreact removed from {channel.mention}") elif emote not in autoreact.reactions: await ctx.send( @@ -153,9 +171,9 @@ class AutoReactCog(Scale): return else: autoreact.reactions.remove(emote) - autoreact.save() - if len(autoreact.reactions) == 0: - self.delete_autoreact(ctx, channel) + await autoreact.commit() + if len(autoreact.reactions) == 0 and not autoreact.thread: + await self.delete_autoreact(ctx, channel) await ctx.send(f"Removed {emote} from {channel.mention} autoreact.") @slash_command(