From 183f8b29b00ae5ff1f600722245c6aafc6164933 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Tue, 8 Aug 2023 10:51:25 -0400 Subject: [PATCH] feat: added unicode emoji support for coco --- galaxy/galaxy.py | 55 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/galaxy/galaxy.py b/galaxy/galaxy.py index 7646d18..d475e0b 100644 --- a/galaxy/galaxy.py +++ b/galaxy/galaxy.py @@ -70,7 +70,11 @@ class Galaxy(commands.Cog): @commands.Cog.listener('on_message') async def cocoreact(self, message): if message.guild is not None: - emoji = self.bot.get_emoji(await self.config.guild(message.guild).cocoemoji()) + emoji_id = await self.config.guild(message.guild).cocoemoji() + if self.check_if_discord_unicode_emoji(emoji_id) is False: + emoji = self.bot.get_emoji(emoji_id) + elif self.check_if_discord_unicode_emoji(emoji_id) is True: + emoji = emoji_id cocotarget = await self.config.guild(message.guild).cocotarget() if cocotarget == 0: return @@ -82,13 +86,32 @@ class Galaxy(commands.Cog): @commands.guild_only() async def coco_list(self, ctx: commands.Context): """Checks who Coco is currently set to.""" - emoji = self.bot.get_emoji(await self.config.guild(ctx.guild).cocoemoji()) + emoji_id = await self.config.guild(ctx.guild).cocoemoji() cocotarget = await self.config.guild(ctx.guild).cocotarget() - embed = discord.Embed(color=await self.bot.get_embed_color(None), description=f"Coco is currently set to <@{cocotarget}> ({cocotarget}).\nCoco's emoji is currently set to {emoji} ({await self.config.guild(ctx.guild).cocoemoji()}).") + if self.check_if_discord_unicode_emoji(emoji_id) is True: + emoji = emoji_id + embed = discord.Embed(color=await self.bot.get_embed_color(None), description=f"Coco is currently set to <@{cocotarget}> ({cocotarget}).\nCoco's emoji is currently set to {emoji}.") + else: + emoji = self.bot.get_emoji(emoji_id) + embed = discord.Embed(color=await self.bot.get_embed_color(None), description=f"Coco is currently set to <@{cocotarget}> ({cocotarget}).\nCoco's emoji is currently set to {emoji} ({await self.config.guild(ctx.guild).cocoemoji()}).") await ctx.send(embed=embed) coco = app_commands.Group(name='coco', guild_only=True, description="This group handles the autoreact functionality.") + def check_if_discord_unicode_emoji(self, emoji: str): + emoji_ranges = [ + (0x1F600, 0x1F64F), # Emoticons + (0x1F300, 0x1F5FF), # Miscellaneous symbols and pictographs + (0x1F680, 0x1F6FF), # Transport and map symbols + (0x1F700, 0x1F77F), # Alchemical symbols + ] + for char in emoji: + code_point = ord(char) + for start, end in emoji_ranges: + if start <= code_point <= end: + return True + return False + def extract_id(self, input_string): match = re.search(r'(?<=:)\d+(?=>)', input_string) if match: @@ -101,19 +124,21 @@ class Galaxy(commands.Cog): async def coco_emoji(self, interaction: discord.Interaction, emoji: str = None): """Sets Coco's emoji.""" if emoji: - emoji_id = self.extract_id(input_string=emoji) - try: - emoji_obj = await interaction.guild.fetch_emoji(emoji_id) - except discord.NotFound or discord.HTTPException as error: - await interaction.response.send_message(content="You're trying to set the coco emoji to an emoji I don't have access to!", ephemeral=True) - return - await self.config.guild(interaction.guild).cocoemoji.set(emoji_obj.id) - embed=discord.Embed(color=await self.bot.get_embed_color(None), description=f"Coco's emoji has been set to {emoji_obj} ({emoji_obj.id}).") - await interaction.response.send_message(embed=embed) + if self.check_if_discord_unicode_emoji(emoji) is True: + await self.config.guild(interaction.guild).cocoemoji.set(emoji) + embed=discord.Embed(color=await self.bot.get_embed_color(None), description=f"Coco's emoji has been set to {emoji}.") + await interaction.response.send_message(embed=embed) + else: + emoji_id = self.extract_id(input_string=emoji) + emoji_obj = self.bot.get_emoji(emoji_id) + if emoji_obj is None: + await interaction.response.send_message(content="You're trying to set the coco emoji to an emoji I don't have access to!", ephemeral=True) + await self.config.guild(interaction.guild).cocoemoji.set(emoji_obj.id) + embed=discord.Embed(color=await self.bot.get_embed_color(None), description=f"Coco's emoji has been set to {emoji_obj} ({emoji_obj.id}).") + await interaction.response.send_message(embed=embed) else: - await self.config.guild(interaction.guild).cocoemoji.set(1028535684757209118) - emoji = self.bot.get_emoji(1028535684757209118) - embed=discord.Embed(color=await self.bot.get_embed_color(None), description=f"Coco's emoji has been set to {emoji} (1028535684757209118).") + await self.config.guild(interaction.guild).cocoemoji.set('💀') + embed=discord.Embed(color=await self.bot.get_embed_color(None), description=f"Coco's emoji has been set to 💀.") await interaction.response.send_message(embed=embed) @coco.command(name="set")