Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - My ban command for some reason doesn't work, and I don't know hot to fix it (discord.py)

I made a ban command about a month ago for my bot, and a few days ago, it just stopped working. Does anyone know why? Here is my code:

@client.command()
@commands.has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, *, reason=None):
    await member.ban(reason=reason)

Here is the error I get:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'ban'

I need all the help I can get. Thanks in advance!

question from:https://stackoverflow.com/questions/66050649/my-ban-command-for-some-reason-doesnt-work-and-i-dont-know-hot-to-fix-it-dis

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

1 Reply

0 votes
by (71.8m points)

you're doing the command in the a Cog. all methods (functions) in a class (in this case, Cog) have their first set as self which is the class itself.

Because you didn't passed "self" "ctx" is being used as it, basically your command is missing one parameter. Try this:

@commands.command()
@commands.has_permissions(ban_members=True)
async def ban(self, ctx, member : discord.Member, *, reason=None):
    await member.ban(reason=reason)

Also, take into consideration that commands.has_permissions works only on the current channel, you probably want to use commands.has_guild_permissions instead


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...