Repository: Cog-Creators/Red-DiscordBot

Issue:
warnaction add throws OSErrors
# Command bugs

<!-- 
Did you find a bug with a command? Fill out the following:
-->

#### Command name

warnaction

#### What cog is this command from?

Warnings

#### What were you expecting to happen?

an action was created when I try to add automated actions to warnings

#### What actually happened?
```
Exception in command 'warnaction add'
Traceback (most recent call last):
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 83, in wrapped
  File "/root/redenv/lib/python3.8/site-packages/redbot/cogs/warnings/warnings.py", line 103, in action_add
  File "/root/redenv/lib/python3.8/site-packages/redbot/cogs/warnings/helpers.py", line 111, in get_command_for_exceeded_points
  File "/root/redenv/lib/python3.8/site-packages/redbot/cogs/warnings/helpers.py", line 72, in get_command_from_input
  File "/usr/lib/python3.8/inspect.py", line 985, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/lib/python3.8/inspect.py", line 967, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python3.8/inspect.py", line 798, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 892, in invoke
  File "/root/redenv/lib/python3.8/site-packages/redbot/core/commands/commands.py", line 794, in invoke
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1234, in invoke
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 797, in invoke
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 92, in wrapped
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OSError: could not get source code
```

#### How can we reproduce this issue?

Install Red-Bot
try to add an action to warnings
profit?


Discussion:
What command are you trying to add? Can you provide some more detailed steps to reproduce please?
I tried to add several commands, first I tried `kick {user}` then I tried to do `ban {user}` both of them throw the same error.

the exact command I ran was `[p]warnaction add kick 5` I waited until it allowed me to respond with the command and entered the one I wanted.

as for steps to reproduce, I can't really help, this is all I did.
And you never reach the point where it asks 
> Enter the command to be run when the user returns to a value **below** the points for this action to occur.

it just errors on the first input request?

Also, please post the output of `[p]debuginfo`.
sorry, I should've been a bit more clear, after that point, that is when I entered the command.


```
Exception in command 'debuginfo'
Traceback (most recent call last):
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 83, in wrapped
  File "/root/redenv/lib/python3.8/site-packages/redbot/core/core_commands.py", line 1643, in debuginfo
ModuleNotFoundError: No module named 'distro'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 892, in invoke
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 797, in invoke
  File "/root/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 92, in wrapped
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ModuleNotFoundError: No module named 'distro'
```
that is the output of debuginfo.
Fixed it, unrelated issues to the bot.
Reopening, cause we shouldn't use `inspect` to check for `is_owner` check here:
https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/cogs/warnings/helpers.py#L72-L78

We can just check for `command.requires.privilege_level` here.

Resolving patch:
diff --git a/redbot/cogs/warnings/helpers.py b/redbot/cogs/warnings/helpers.py
--- a/redbot/cogs/warnings/helpers.py
+++ b/redbot/cogs/warnings/helpers.py
@@ -1,9 +1,9 @@
 from copy import copy
 import asyncio
-import inspect
 import discord
 
 from redbot.core import Config, checks, commands
+from redbot.core.commands.requires import PrivilegeLevel
 from redbot.core.i18n import Translator
 from redbot.core.utils.predicates import MessagePredicate
 
@@ -54,7 +54,9 @@ async def create_and_invoke_context(
     try:
         await realctx.bot.invoke(fctx)
     except (commands.CheckFailure, commands.CommandOnCooldown):
-        await fctx.reinvoke()
+        # reinvoke bypasses checks and we don't want to run bot owner only commands here
+        if fctx.command.requires.privilege_level < PrivilegeLevel.BOT_OWNER:
+            await fctx.reinvoke()
 
 
 def get_command_from_input(bot, userinput: str):
@@ -69,9 +71,7 @@ def get_command_from_input(bot, userinput: str):
     if com is None:
         return None, _("I could not find a command from that input!")
 
-    check_str = inspect.getsource(checks.is_owner)
-    if any(inspect.getsource(x) in check_str for x in com.checks):
-        # command the user specified has the is_owner check
+    if com.requires.privilege_level >= PrivilegeLevel.BOT_OWNER:
         return (
             None,
             _("That command requires bot owner. I can't allow you to use that for an action"),


Test patch: