From f1d359f81632c3e73bd29922a7734e1bd28474de Mon Sep 17 00:00:00 2001 From: Red-GitHubBot <88117545+Red-GitHubBot@users.noreply.github.com> Date: Sun, 5 Jun 2022 16:02:32 +0200 Subject: [PATCH] [3.4] Use Guild.fetch_ban() over Guild.bans() (#5656) (#5748) (cherry picked from commit d932abad166749b95a69ab2af9096f72bc32d8da) Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> --- redbot/cogs/mod/kickban.py | 41 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/redbot/cogs/mod/kickban.py b/redbot/cogs/mod/kickban.py index 668cf4fd6..d31fa92fb 100644 --- a/redbot/cogs/mod/kickban.py +++ b/redbot/cogs/mod/kickban.py @@ -172,8 +172,11 @@ class KickBanMixin(MixinMeta): else: tempbans = await self.config.guild(guild).current_tempbans() - ban_list = [ban.user.id for ban in await guild.bans()] - if user.id in ban_list: + try: + await guild.fetch_ban(user) + except discord.NotFound: + pass + else: if user.id in tempbans: async with self.config.guild(guild).current_tempbans() as tempbans: tempbans.remove(user.id) @@ -474,17 +477,18 @@ class KickBanMixin(MixinMeta): tempbans = await self.config.guild(guild).current_tempbans() - ban_list = await guild.bans() - for entry in ban_list: - for user_id in user_ids: - if entry.user.id == user_id: - if user_id in tempbans: - # We need to check if a user is tempbanned here because otherwise they won't be processed later on. - continue - else: - errors[user_id] = _("User with ID {user_id} is already banned.").format( - user_id=user_id - ) + for user_id in user_ids: + if user_id in tempbans: + # We need to check if a user is tempbanned here because otherwise they won't be processed later on. + continue + try: + await guild.fetch_ban(discord.Object(user_id)) + except discord.NotFound: + pass + else: + errors[user_id] = _("User with ID {user_id} is already banned.").format( + user_id=user_id + ) user_ids = remove_processed(user_ids) @@ -907,14 +911,13 @@ class KickBanMixin(MixinMeta): guild = ctx.guild author = ctx.author audit_reason = get_audit_reason(ctx.author, reason, shorten=True) - bans = await guild.bans() - bans = [be.user for be in bans] - user = discord.utils.get(bans, id=user_id) - if not user: + try: + ban_entry = await guild.fetch_ban(discord.Object(user_id)) + except discord.NotFound: await ctx.send(_("It seems that user isn't banned!")) return try: - await guild.unban(user, reason=audit_reason) + await guild.unban(ban_entry.user, reason=audit_reason) except discord.HTTPException: await ctx.send(_("Something went wrong while attempting to unban that user.")) return @@ -924,7 +927,7 @@ class KickBanMixin(MixinMeta): guild, ctx.message.created_at.replace(tzinfo=timezone.utc), "unban", - user, + ban_entry.user, author, reason, until=None,