Repository: Cog-Creators/Red-DiscordBot

Issue:
[V3] Ban peers with hackban
# Command bugs

<!-- 
Did you find a bug with a command? Fill out the following:
-->

#### Command name

[p]hackban

#### What cog is this command from?

Mod

#### What were you expecting to happen?

When called by a mod on another mod, display error message: "I cannot let you do that. You are not higher than the user in the role hierarchy." and do nothing.

#### What actually happened?

Mod got banned, message displayed: "Done. The user will not be able to join this server."

#### How can we reproduce this issue?

1) Set a role with ban permissions and access to the Mod cog and hackban command.
2) Give the role to two members, it should be the highest role for both of them.
3) Have one of them execute [p]hackban <other's id>



Discussion:
Can confirm that this is a bug. Also as an additional bug, ban does not check if you can actually ban the user and it will error out if the bot doesn't have the permissions to ban.

<details><summary>Traceback</summary><p>
Exception in command 'hackban'
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 61, in wrapped
    ret = await coro(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/redbot/cogs/mod/mod.py", line 441, in hackban
    ban_list = await guild.bans()
  File "/usr/local/lib/python3.6/dist-packages/discord/guild.py", line 857, in bans
    data = await self._state.http.get_bans(self.id)
  File "/usr/local/lib/python3.6/dist-packages/discord/http.py", line 209, in request
    raise Forbidden(r, data)
discord.errors.Forbidden: FORBIDDEN (status code: 403): Missing Permissions

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/bot.py", line 898, in invoke
    await ctx.command.invoke(ctx)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 550, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 70, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: FORBIDDEN (status code: 403): Missing Permissions
</p></details>

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
@@ -436,7 +436,8 @@ async def hackban(self, ctx: commands.Context, user_id: int, *, reason: str = No
         using this command"""
         author = ctx.author
         guild = ctx.guild
-
+        if not guild.me.guild_permissions.ban_members:
+            return await ctx.send(_("I lack the permissions to do this."))
         is_banned = False
         ban_list = await guild.bans()
         for entry in ban_list:
@@ -449,8 +450,10 @@ async def hackban(self, ctx: commands.Context, user_id: int, *, reason: str = No
             return
 
         user = guild.get_member(user_id)
-        if user is None:
-            user = discord.Object(id=user_id)  # User not in the guild, but
+        if user is not None:
+            # Instead of replicating all that handling... gets attr from decorator
+            return await ctx.invoke(self.ban, user, None, reason=reason)
+        user = discord.Object(id=user_id)  # User not in the guild, but
 
         audit_reason = get_audit_reason(author, reason)
         queue_entry = (guild.id, user_id)


Test patch: