dolfies / discord.py-self

A fork of the popular discord.py for user accounts.
https://discordpy-self.rtfd.io/en/latest/
MIT License
717 stars 163 forks source link

Custom owner IDs: Bool can't be used in await expression #239

Closed TheOnlyWayUp closed 2 years ago

TheOnlyWayUp commented 2 years ago

Summary

If you set owners, and have commands that use the is_owner decorator, an error regarding a non-async function being awaited will be raised.

Reproduction Steps

Create a command that's owner only

@commands.command(help="Returns bot latency.")
@commands.is_owner()
async def ping(self, ctx):
    await ctx.reply(f"Pong, {round(self.bot.latency, 1)}ms.", mention_author=False)

For example.

Then set owner IDs -

bot.owner_ids = [your ids here] # Mine were [876055467678375998, 741904523991449674]

Not setting owner IDs would raise another error, that's

Ignoring exception in command help:
Traceback (most recent call last):
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 167, in wrapped
    ret = await coro(*args, **kwargs)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/help.py", line 835, in command_callback
    return await self.send_bot_help(mapping)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/help.py", line 1029, in send_bot_help
    filtered = await self.filter_commands(bot.commands, sort=True, key=get_category)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/help.py", line 580, in filter_commands
    valid = await predicate(cmd)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/help.py", line 574, in predicate
    return await cmd.can_run(self.context)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 1124, in can_run
    return await discord.utils.async_all(predicate(ctx) for predicate in predicates)  # type: ignore
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/utils.py", line 537, in async_all
    elem = await elem
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 2103, in predicate
    if not await ctx.bot.is_owner(ctx.author):
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 356, in is_owner
    raise AttributeError('Owners aren\'t set.')
AttributeError: Owners aren't set.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 996, in invoke
    await ctx.command.invoke(ctx)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 894, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 176, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: Owners aren't set.

Anyway, once you set owner_ids, you can run either the help or ping (or the command you created) to reproduce the error.

Ignoring exception in command help:
Traceback (most recent call last):
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 167, in wrapped
    ret = await coro(*args, **kwargs)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/help.py", line 835, in command_callback
    return await self.send_bot_help(mapping)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/help.py", line 1029, in send_bot_help
    filtered = await self.filter_commands(bot.commands, sort=True, key=get_category)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/help.py", line 580, in filter_commands
    valid = await predicate(cmd)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/help.py", line 574, in predicate
    return await cmd.can_run(self.context)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 1124, in can_run
    return await discord.utils.async_all(predicate(ctx) for predicate in predicates)  # type: ignore
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/utils.py", line 537, in async_all
    elem = await elem
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 2103, in predicate
    if not await ctx.bot.is_owner(ctx.author):
TypeError: object bool can't be used in 'await' expression

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 996, in invoke
    await ctx.command.invoke(ctx)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 894, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/root/code/Templates/Selfbot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 176, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object bool can't be used in 'await' expression

Results

The help command, and any commands with is_owner shouldn't error when called, but they do.

System Information

That's broken too, addressed in #240.

Using commit 0f2dda9dd4392039576243a27fc34f7ab9542e2e on branch rebase. Installed using pip install git+https://github.com/dolfies/discord.py-self@rebase.

Issue present on Line 2103 of discord.ext.commands/core.py. Going to use grep to view all occurrences of await ctx.bot.is_owner.

bot.is_owner is defined on Line 330 of discord.ext.commands/bot.py. This function could be made async to reduce the amount of changes made to the codebase but 😬, I'm going to make a PR that replaces all calls to is_owner that use await.

TheOnlyWayUp commented 2 years ago

Looks like there's only one occurrence of await bot.is_owner

discord/ext/commands/core.py:        if not await ctx.bot.is_owner(ctx.author):

On line 2103.

TheOnlyWayUp commented 2 years ago

Fixed in #243.

dolfies commented 2 years ago

Bug is fixed by upstream dpy.