From bfb32b95c12a465b60943e527e83bd7d5fdc4b95 Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Wed, 25 Sep 2019 13:34:16 -0500 Subject: Free Command Fix - Fixed bug where if two channels had the same last message timestamp the command would error out. Signed-off-by: Daniel Brown --- bot/cogs/free.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/free.py b/bot/cogs/free.py index 167fab319..ec8834240 100644 --- a/bot/cogs/free.py +++ b/bot/cogs/free.py @@ -7,7 +7,6 @@ from discord.ext.commands import Bot, Cog, Context, command from bot.constants import Categories, Channels, Free, STAFF_ROLES from bot.decorators import redirect_output - log = logging.getLogger(__name__) TIMEOUT = Free.activity_timeout @@ -51,10 +50,10 @@ class Free(Cog): # the command was invoked in if channel.id == ctx.channel.id: messages = await channel.history(limit=seek).flatten() - msg = messages[seek-1] + msg = messages[seek - 1] # Otherwise get last message else: - msg = await channel.history(limit=1).next() # noqa (False positive) + msg = await channel.history(limit=1).next() # noqa (False positive) inactive = (datetime.utcnow() - msg.created_at).seconds if inactive > TIMEOUT: @@ -80,7 +79,8 @@ class Free(Cog): # Sort channels in descending order by seconds # Get position in list, inactivity, and channel object # For each channel, add to embed.description - for i, (inactive, channel) in enumerate(sorted(free_channels, reverse=True), 1): + for i, (inactive, channel) in enumerate( + sorted(free_channels, key=lambda free_channel: free_channel[0], reverse=True), 1): minutes, seconds = divmod(inactive, 60) if minutes > 59: hours, minutes = divmod(minutes, 60) -- cgit v1.2.3 From e02accae0730b92b4630ababfdcc5f5931effaff Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Wed, 25 Sep 2019 14:49:34 -0500 Subject: Free Command Fix - Moved the sorted function to its own line and instead passed the generated list for code clarity. Signed-off-by: Daniel Brown --- bot/cogs/free.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/free.py b/bot/cogs/free.py index ec8834240..bc84aae3f 100644 --- a/bot/cogs/free.py +++ b/bot/cogs/free.py @@ -79,8 +79,8 @@ class Free(Cog): # Sort channels in descending order by seconds # Get position in list, inactivity, and channel object # For each channel, add to embed.description - for i, (inactive, channel) in enumerate( - sorted(free_channels, key=lambda free_channel: free_channel[0], reverse=True), 1): + sorted_channels = sorted(free_channels, key=lambda free_channel: free_channel[0], reverse=True) + for i, (inactive, channel) in enumerate(sorted_channels, 1): minutes, seconds = divmod(inactive, 60) if minutes > 59: hours, minutes = divmod(minutes, 60) -- cgit v1.2.3 From 5c1f56dd756da8e1e5d4e6f53c5cd4c8569b459a Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Wed, 25 Sep 2019 15:40:39 -0500 Subject: Swapped Lambda for itemgetter - For the sake of code style and consistency, the lambda has been swapped with operator.itemgetter Signed-off-by: Daniel Brown --- bot/cogs/free.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/cogs/free.py b/bot/cogs/free.py index bc84aae3f..269c5c1b9 100644 --- a/bot/cogs/free.py +++ b/bot/cogs/free.py @@ -1,5 +1,6 @@ import logging from datetime import datetime +from operator import itemgetter from discord import Colour, Embed, Member, utils from discord.ext.commands import Bot, Cog, Context, command @@ -79,7 +80,7 @@ class Free(Cog): # Sort channels in descending order by seconds # Get position in list, inactivity, and channel object # For each channel, add to embed.description - sorted_channels = sorted(free_channels, key=lambda free_channel: free_channel[0], reverse=True) + sorted_channels = sorted(free_channels, key=itemgetter(0), reverse=True) for i, (inactive, channel) in enumerate(sorted_channels, 1): minutes, seconds = divmod(inactive, 60) if minutes > 59: -- cgit v1.2.3