Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

discord - 'if not "giveaway rig" in str(ctx.command): AttributeError: 'CheckFailure' object has no attribute 'command'` Can you tell how to fix this?

This is my code:

@bot.event
async def on_command_error(error, ctx):
    if not "giveaway rig" in str(ctx.command):
        if isinstance(error, commands.CommandNotFound):
            pass
        elif isinstance(error, commands.MissingRequiredArgument):
            await send_cmd_help(ctx)
        elif isinstance(error, commands.BadArgument):
            await send_cmd_help(ctx)
    else:
        await ctx.bot.send_message(ctx.message.channel, defaultGiveawayErrorMessage)

And when it runs, this error shows:

if not "giveaway rig" in str(ctx.command):
AttributeError: 'CheckFailure' object has no attribute 'command'

How can I fix this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue at hand:

You've got your event's parameters the wrong way round.
Swap them around like so:

async def on_command_error(ctx, error):

Discord.py version:

And from the looks of things, you seem to be using some async code:

await ctx.bot.send_message(ctx.message.channel, defaultGiveawayErrorMessage)

which, in rewrite (v1.x), can be written as:

await ctx.send(defaultGiveawayErrorMessage)

The rewrite and async versions of d.py are not compatible, and you will need to update your version of d.py.
You can do this by running:

pip install -U discord.py

and you'll know whether you have the rewrite version judging by if you see this in your output:

Requirement already up-to-date: discord.py in c:PATHTOpythonpython35libsite-packages (1.X.X)

(Note the version number at the end)


Some issues that may arise:

  1. You've got it installed successfully, but running your script errors when using rewrite's documentation.

Cause: This is because you may be trying to run the script on a version of python that discord.py wasn't installed on.


Fixes:

  • If this was done in a virtual environment, it should be an easy enough fix - just open up the venv's terminal and make sure you run the command there (not in your cmd prompt!).
  • If you're not using a venv, it'd be your best bet to just make sure you have one version of Python installed.
  1. Whenever you run the command pip install -U discord.py, you're only getting v0.16.x installed.

Cause: You're installing the package on an outdated version of Python.


Fixes:

  • discord.py, as of this answer, only supports Python versions 3.5.3 and above.
    This means that you need to upgrade your version of Python.
  • If you're still encountering this issue and you're sure you've got a version of 3.5.3 or above, check the current versions of Python that discord.py supports. A nice and simple fix for this is to make sure you only have one version of Python installed again.

And with that being said, I highly recommend using the rewrite docs, and if you want to follow any tutorials, make sure they're up-to-date (include rewrite as a keyword in your searches).

The rewrite version is much, much nicer than async, and at first it may be a bit difficult to get to grips with what's changed, which is what this page is for. It'll help build the foundations for your migration to the newer d.py version.


References:

  • Major model changes - Moving from async to rewrite, I highly recommend reading through this, as it could fix any issues that you run into when you get started.
  • OS-specific installations - For if you're still running into issues during installation.
  • on_command_error()
  • commands.Context - You will, depending on what your bot does, be using context instances a LOT of the time. Get familiar with what attributes and methods you can access - it'll save you a lot of headaches in the future.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...