From 61400aabf6d6d30d09f16e91eb43894fa2b56ff7 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 17:48:11 -0700 Subject: Source: raise BadArgument for dynamically-created objects Commands, cogs, etc. created via internal eval won't have a source file associated with them, making source retrieval impossible. Fixes #1083 Fixes BOT-7K --- bot/cogs/source.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index f1db745cd..89548613d 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -60,7 +60,11 @@ class BotSource(commands.Cog): await ctx.send(embed=embed) def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: - """Build GitHub link of source item, return this link, file location and first line number.""" + """ + Build GitHub link of source item, return this link, file location and first line number. + + Raise BadArgument if `source_item` is a dynamically-created object (e.g. via internal eval). + """ if isinstance(source_item, commands.HelpCommand): src = type(source_item) filename = inspect.getsourcefile(src) @@ -78,10 +82,17 @@ class BotSource(commands.Cog): filename = tags_cog._cache[source_item]["location"] else: src = type(source_item) - filename = inspect.getsourcefile(src) + try: + filename = inspect.getsourcefile(src) + except TypeError: + raise commands.BadArgument("Cannot get source for a dynamically-created object.") if not isinstance(source_item, str): - lines, first_line_no = inspect.getsourcelines(src) + try: + lines, first_line_no = inspect.getsourcelines(src) + except OSError: + raise commands.BadArgument("Cannot get source for a dynamically-created object.") + lines_extension = f"#L{first_line_no}-L{first_line_no+len(lines)-1}" else: first_line_no = None -- cgit v1.2.3 From bcb8f27cba8d1413d302d11e38d122f915f96e14 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 17:52:16 -0700 Subject: Source: remove redundant check for help commands The code is identical to the else block and there's no reason for help commands to have an explicit check. --- bot/cogs/source.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 89548613d..205e0ba81 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -65,10 +65,7 @@ class BotSource(commands.Cog): Raise BadArgument if `source_item` is a dynamically-created object (e.g. via internal eval). """ - if isinstance(source_item, commands.HelpCommand): - src = type(source_item) - filename = inspect.getsourcefile(src) - elif isinstance(source_item, commands.Command): + if isinstance(source_item, commands.Command): if source_item.cog_name == "Alias": cmd_name = source_item.callback.__name__.replace("_alias", "") cmd = self.bot.get_command(cmd_name.replace("_", " ")) -- cgit v1.2.3 From 59c62162e0e0abad53dfbaad0e197a0fbab2f22f Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 4 Aug 2020 18:09:13 -0700 Subject: HelpChannels: use more reliable check for claimed channel Using the channel's category isn't reliable since it may take Discord a while to actually move the channel once it's received a request from the bot. I suppose using redis technically has the same problem, but it should be much faster and less susceptible to lag than Discord. Fixes #1074 --- bot/cogs/help_channels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 1be980472..975043df9 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -694,7 +694,7 @@ class HelpChannels(commands.Cog): async with self.on_message_lock: log.trace(f"on_message lock acquired for {message.id}.") - if not self.is_in_category(channel, constants.Categories.help_available): + if await self.help_channel_claimants.contains(channel.id): log.debug( f"Message {message.id} will not make #{channel} ({channel.id}) in-use " f"because another message in the channel already triggered that." -- cgit v1.2.3 From bcd2ef98ab91a48ba7b8769f626ff7beb14db663 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Wed, 5 Aug 2020 15:26:54 +0200 Subject: Redis: remove erroneous `_redis` alias If a RedisCache instance was being accessed before bot has created the `redis_cache` instance, the `_redis` alias was being set to None, causing AttributeErrors in lookups. See: #1090 --- bot/utils/redis_cache.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/bot/utils/redis_cache.py b/bot/utils/redis_cache.py index 58cfe1df5..52b689b49 100644 --- a/bot/utils/redis_cache.py +++ b/bot/utils/redis_cache.py @@ -226,7 +226,6 @@ class RedisCache: for attribute in vars(instance).values(): if isinstance(attribute, Bot): self.bot = attribute - self._redis = self.bot.redis_session return self else: error_message = ( @@ -251,7 +250,7 @@ class RedisCache: value = self._value_to_typestring(value) log.trace(f"Setting {key} to {value}.") - await self._redis.hset(self._namespace, key, value) + await self.bot.redis_session.hset(self._namespace, key, value) async def get(self, key: RedisKeyType, default: Optional[RedisValueType] = None) -> Optional[RedisValueType]: """Get an item from the Redis cache.""" @@ -259,7 +258,7 @@ class RedisCache: key = self._key_to_typestring(key) log.trace(f"Attempting to retrieve {key}.") - value = await self._redis.hget(self._namespace, key) + value = await self.bot.redis_session.hget(self._namespace, key) if value is None: log.trace(f"Value not found, returning default value {default}") @@ -281,7 +280,7 @@ class RedisCache: key = self._key_to_typestring(key) log.trace(f"Attempting to delete {key}.") - return await self._redis.hdel(self._namespace, key) + return await self.bot.redis_session.hdel(self._namespace, key) async def contains(self, key: RedisKeyType) -> bool: """ @@ -291,7 +290,7 @@ class RedisCache: """ await self._validate_cache() key = self._key_to_typestring(key) - exists = await self._redis.hexists(self._namespace, key) + exists = await self.bot.redis_session.hexists(self._namespace, key) log.trace(f"Testing if {key} exists in the RedisCache - Result is {exists}") return exists @@ -314,7 +313,7 @@ class RedisCache: """ await self._validate_cache() items = self._dict_from_typestring( - await self._redis.hgetall(self._namespace) + await self.bot.redis_session.hgetall(self._namespace) ).items() log.trace(f"Retrieving all key/value pairs from cache, total of {len(items)} items.") @@ -323,7 +322,7 @@ class RedisCache: async def length(self) -> int: """Return the number of items in the Redis cache.""" await self._validate_cache() - number_of_items = await self._redis.hlen(self._namespace) + number_of_items = await self.bot.redis_session.hlen(self._namespace) log.trace(f"Returning length. Result is {number_of_items}.") return number_of_items @@ -335,7 +334,7 @@ class RedisCache: """Deletes the entire hash from the Redis cache.""" await self._validate_cache() log.trace("Clearing the cache of all key/value pairs.") - await self._redis.delete(self._namespace) + await self.bot.redis_session.delete(self._namespace) async def pop(self, key: RedisKeyType, default: Optional[RedisValueType] = None) -> RedisValueType: """Get the item, remove it from the cache, and provide a default if not found.""" @@ -364,7 +363,7 @@ class RedisCache: """ await self._validate_cache() log.trace(f"Updating the cache with the following items:\n{items}") - await self._redis.hmset_dict(self._namespace, self._dict_to_typestring(items)) + await self.bot.redis_session.hmset_dict(self._namespace, self._dict_to_typestring(items)) async def increment(self, key: RedisKeyType, amount: Optional[int, float] = 1) -> None: """ -- cgit v1.2.3 From 5a7ca92cf5d5ae7c7d4aa7ba086237586832af1a Mon Sep 17 00:00:00 2001 From: kwzrd Date: Wed, 5 Aug 2020 17:27:08 +0200 Subject: Revert "HelpChannels: use more reliable check for claimed channel" This reverts commit 59c62162 --- bot/cogs/help_channels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 975043df9..1be980472 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -694,7 +694,7 @@ class HelpChannels(commands.Cog): async with self.on_message_lock: log.trace(f"on_message lock acquired for {message.id}.") - if await self.help_channel_claimants.contains(channel.id): + if not self.is_in_category(channel, constants.Categories.help_available): log.debug( f"Message {message.id} will not make #{channel} ({channel.id}) in-use " f"because another message in the channel already triggered that." -- cgit v1.2.3