Repository: Cog-Creators/Red-DiscordBot

Issue:
[V3 Mod] Tempban doesn't remove the user from settings on unban
# Command bugs

<!-- 
Did you find a bug with a command? Fill out the following:
-->

#### Command name

tempban

#### What cog is this command from?

Mod

#### What were you expecting to happen?

When the user is unbanned and then permanently banned, they remain permanently banned.

#### What actually happened?

The user is unbanned by the bot on the next loop due to their previous tempban having expired.

#### How can we reproduce this issue?

1. Tempban a user.
2. User is unbanned and rejoins.
3. User is permanently banned. Another tempban overrides the settings, so an extended tempban sidesteps the issue.
4. User is unbanned by the bot when the unban task loop runs again.



Discussion:

Resolving patch:
diff --git a/redbot/cogs/mod/mod.py b/redbot/cogs/mod/mod.py
--- a/redbot/cogs/mod/mod.py
+++ b/redbot/cogs/mod/mod.py
@@ -714,10 +714,13 @@ async def get_invite_for_reinvite(ctx: commands.Context, max_age: int = 86400):
         to send the newly unbanned user
         :returns: :class:`Invite`"""
         guild = ctx.guild
-        if "VANITY_URL" in guild.features and guild.me.permissions.manage_guild:
-            # guild has a vanity url so use it as the one to send
-            return await guild.vanity_invite()
-        invites = await guild.invites()
+        if guild.me.permissions.manage_guild:
+            if "VANITY_URL" in guild.features:
+                # guild has a vanity url so use it as the one to send
+                return await guild.vanity_invite()
+            invites = await guild.invites()
+        else:
+            invites = []
         for inv in invites:  # Loop through the invites for the guild
             if not (inv.max_uses or inv.max_age or inv.temporary):
                 # Invite is for the guild's default channel,
@@ -1392,23 +1395,24 @@ async def check_tempban_expirations(self):
         member = namedtuple("Member", "id guild")
         while self == self.bot.get_cog("Mod"):
             for guild in self.bot.guilds:
-                guild_tempbans = await self.settings.guild(guild).current_tempbans()
-                for uid in guild_tempbans:
-                    unban_time = datetime.utcfromtimestamp(
-                        await self.settings.member(member(uid, guild)).banned_until()
-                    )
-                    now = datetime.utcnow()
-                    if now > unban_time:  # Time to unban the user
-                        user = await self.bot.get_user_info(uid)
-                        queue_entry = (guild.id, user.id)
-                        self.unban_queue.append(queue_entry)
-                        try:
-                            await guild.unban(user, reason="Tempban finished")
-                        except discord.Forbidden:
-                            self.unban_queue.remove(queue_entry)
-                            log.info("Failed to unban member due to permissions")
-                        except discord.HTTPException:
-                            self.unban_queue.remove(queue_entry)
+                async with self.settings.guild(guild).current_tempbans() as guild_tempbans:
+                    for uid in guild_tempbans:
+                        unban_time = datetime.utcfromtimestamp(
+                            await self.settings.member(member(uid, guild)).banned_until()
+                        )
+                        now = datetime.utcnow()
+                        if now > unban_time:  # Time to unban the user
+                            user = await self.bot.get_user_info(uid)
+                            queue_entry = (guild.id, user.id)
+                            self.unban_queue.append(queue_entry)
+                            try:
+                                await guild.unban(user, reason="Tempban finished")
+                                guild_tempbans.remove(uid)
+                            except discord.Forbidden:
+                                self.unban_queue.remove(queue_entry)
+                                log.info("Failed to unban member due to permissions")
+                            except discord.HTTPException:
+                                self.unban_queue.remove(queue_entry)
             await asyncio.sleep(60)
 
     async def check_duplicates(self, message):


Test patch: