我的addrole+ Removerole命令?
因此,我最近尝试为我的Discord机器人制作一些附加和外径命令。我在YT上使用了James S的视频(第15集),几乎完全复制了。 我用Python语言在Pycharm工作。
这是代码:
@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def addRole(self, ctx, user : discord.Member, *, role : discord.Role):
if role in user.roles:
await ctx.send(f"{user.mention} already has that role! ({role})")
else:
await user.add_roles(role)
await ctx.send(f"Added {role} to {user.mention}!")
@addRole.error
async def role_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You need to have permissions to do this!")
@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def removeRole(self, ctx, user : discord.Member, *, role : discord.Role):
if role in user.roles:
await user.remove_roles(role)
await ctx.send(f"{user.mention} doesn't have that role! ({role})")
else:
await ctx.send(f"Removed {role} from {user.ention}!")
@removeRole.error
async def removeRole_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You need to have permissions to do this!")
这是我遇到的错误; 忽略命令中的异常无: discord.ext.commands.errors.commandnotfound:找不到命令“ addrole”
So I recently tried to make some AddRole and RemoveRole commands for my Discord Bot. I used a video from James S on YT (episode 15) and almost copied almost exactly.
I work in PyCharm, with the Python language.
This is the code:
@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def addRole(self, ctx, user : discord.Member, *, role : discord.Role):
if role in user.roles:
await ctx.send(f"{user.mention} already has that role! ({role})")
else:
await user.add_roles(role)
await ctx.send(f"Added {role} to {user.mention}!")
@addRole.error
async def role_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You need to have permissions to do this!")
@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def removeRole(self, ctx, user : discord.Member, *, role : discord.Role):
if role in user.roles:
await user.remove_roles(role)
await ctx.send(f"{user.mention} doesn't have that role! ({role})")
else:
await ctx.send(f"Removed {role} from {user.ention}!")
@removeRole.error
async def removeRole_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You need to have permissions to do this!")
This is the error I get;
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "addRole" is not found
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我什至无法运行您的代码,因为它说我缺少参数,因此我对其进行了重构并进行了更正,并且可以很好地授予/撤销角色。我无法真正重现您遇到的问题。
我更改了:
self
,因为您无论如何都没有使用它。....然后我做了一切都很好。查看我的代码,让我知道问题是否仍然存在。
I could not even run your code, because it was stating I'm missing parameters, so I refactored and corrected it a bit and it works fine with granting/revoking roles for me. I could not really recreate the problem you have.
What I've changed:
self
from required parameters since you were not using it anyway.....and after I've done that everything worked fine. Check out my code and let me know if the problem still exists.