From 6fe7115a8569530c79508f9d614c4b8c94bd66d8 Mon Sep 17 00:00:00 2001 From: bobloy Date: Thu, 30 Jul 2020 09:45:31 -0400 Subject: [PATCH] Fix duplicate emojis, formatting, caching --- stealemoji/stealemoji.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/stealemoji/stealemoji.py b/stealemoji/stealemoji.py index caab005..cfe011b 100644 --- a/stealemoji/stealemoji.py +++ b/stealemoji/stealemoji.py @@ -1,13 +1,9 @@ -from typing import Any - -import aiohttp import discord -from aiohttp.typedefs import StrOrURL -from redbot.core import Config, commands, checks +from redbot.core import Config, checks, commands from redbot.core.bot import Red - from redbot.core.commands import Cog + # Replaced with discord.Asset.read() # async def fetch_img(session: aiohttp.ClientSession, url: StrOrURL): # async with session.get(url) as response: @@ -50,6 +46,8 @@ class StealEmoji(Cog): self.config.register_global(**default_global) + self.is_on = await self.config.on() + @commands.group() async def stealemoji(self, ctx: commands.Context): """ @@ -86,6 +84,9 @@ class StealEmoji(Cog): """Toggles whether emoji's are collected or not""" curr_setting = await self.config.on() await self.config.on.set(not curr_setting) + + self.is_on = await self.config.on() + await ctx.send("Collection is now " + str(not curr_setting)) @checks.is_owner() @@ -138,13 +139,13 @@ class StealEmoji(Cog): # print("Not a custom emoji") return - if not (await self.config.on()): # TODO: Make this cached - print("Collecting is off") + if not self.is_on: + # print("Collecting is off") return emoji: discord.Emoji = reaction.emoji if emoji in self.bot.emojis: - print("Emoji already in bot.emojis") + # print("Emoji already in bot.emojis") return # This is now a custom emoji that the bot doesn't have access to, time to steal it @@ -160,18 +161,24 @@ class StealEmoji(Cog): break if guildbank is None: - print("No guildbank to store emoji") + # print("No guildbank to store emoji") # Eventually make a new banklist return # Next, have I saved this emoji before (because uploaded emoji != orignal emoji) - stolemojis = await self.config.stolemoji() - - if emoji.id in stolemojis: # TODO: This is not preventing duplicates - print("Emoji has already been stolen") + if str(emoji.id) in await self.config.stolemoji(): + # print("Emoji has already been stolen") return + # stolemojis = await self.config.stolemoji() + # + # print(stolemojis.keys()) + # + # if emoji.id in stolemojis: + # print("Emoji has already been stolen") + # return + # Alright, time to steal it for real # path = urlparse(emoji.url).path # ext = os.path.splitext(path)[1] @@ -192,10 +199,10 @@ class StealEmoji(Cog): name=emoji.name, image=img, reason="Stole from " + str(user) ) except discord.Forbidden as e: - print("PermissionError - no permission to add emojis") + # print("PermissionError - no permission to add emojis") raise PermissionError("No permission to add emojis") from e except discord.HTTPException as e: - print("HTTPException exception") + # print("HTTPException exception") raise e # Unhandled error # If you get this far, YOU DID IT