Discord.py 我的编码中出现了一个名字错误,有人知道为什么吗?
因此,我在discord.py中编写了一个静音命令,这就是它的代码,但是当我运行bot并尝试将我的测试帐户静音时,我得到了以下错误:Discord.py 我的编码中出现了一个名字错误,有人知道为什么吗?,discord.py,Discord.py,因此,我在discord.py中编写了一个静音命令,这就是它的代码,但是当我运行bot并尝试将我的测试帐户静音时,我得到了以下错误: @bot.command() @commands.has_guild_permissions(mute_members=True) async def mute(ctx, member: discord.Member, *, Reason=None): await Member.add_roles(get_role) await get_role(
@bot.command()
@commands.has_guild_permissions(mute_members=True)
async def mute(ctx, member: discord.Member, *, Reason=None):
await Member.add_roles(get_role)
await get_role('751096892867477594')
await ctx.send(f'User {member} Has been muted')
您有多个错误:
- 在
中,Member.add_roles(…)
必须小写Member
- 在将角色添加到用户之前,需要获取该角色
- 你使用的方式是错误的→ 角色id必须是整数,而不是字符串/写入
无效,因为它是类方法get\u role()
@bot.command()
@commands.has_guild_权限(mute_members=True)
异步def静音(ctx,成员:discord.member,*,原因=无):
role=ctx.guild.get_角色(751096892867477594)
等待成员。添加_角色(角色)
wait ctx.send(f'User{member.antify}已静音')
第4行中的
成员
应该是小写的,就像您在参数中传递它一样。另外,为了获得角色使用,角色ID应该是int
,您将其作为str
传递。以下代码适用于您,只需将id
更改为静音角色的id即可
@bot.command()
@commands.has_guild_权限(mute_members=True)
异步def静音(ctx,成员:discord.member,*,原因=无):
muted_role=discord.utils.get(ctx.guild.roles,id=750678725544247329)
等待成员。添加角色(静音的角色,原因=原因)
wait ctx.send(f'User{member}已被静音')
Ignoring exception in command mute:
Traceback (most recent call last):
File "C:\Users\Robin\AppData\Roaming\Python\Python37\sitepackages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\Robin\Desktop\discord bot in python\bot.py", line 85, in mute
await Member.add_roles(get_role)
NameError: name ‘Member’ is not defined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Robin\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command. invoke (ctx)
File "C:\Users\Robin\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(“ctx.args, “*ctx.kwargs)
File "C:\Users\Robin\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name ‘Member’ is not defined