Discord.py 未定义变量';公会';
我试图发出addrole命令,但它不起作用。我犯了一个错误:`undefined Variable'guild'我为列出服务器中的角色编写了代码abc=guild.roles(),但它不起作用,请提供帮助Discord.py 未定义变量';公会';,discord.py,Discord.py,我试图发出addrole命令,但它不起作用。我犯了一个错误:`undefined Variable'guild'我为列出服务器中的角色编写了代码abc=guild.roles(),但它不起作用,请提供帮助 @client.command(pass_context=True) async def addrole(ctx, user: discord.Member, role: discord.Role): if ctx.author.guild_permissions.administ
@client.command(pass_context=True)
async def addrole(ctx, user: discord.Member, role: discord.Role):
if ctx.author.guild_permissions.administrator:
xx=user.roles
abc= guild.roles()
if role in xx:
if role in abc:
await user.add_roles(role)
await ctx.send(f'{user.mention}, {role} is given')
else:
await ctx.send(f'The role you are looking for is not in the server')
else:
await ctx.send(f'The User already have the role')
else:
await ctx.send(f'You Have not enough permissions to run this command')
guild
当前不在您的范围内。你需要一个discord.Guild
对象才能获得它的角色
通过使用上下文
,您可以获得发送命令的帮会:
@client.command()#在重写时自动传递上下文
异步def addrole(ctx,用户:discord.Member,角色:discord.role):
如果ctx.author.guild\u permissions.administrator:
xx=用户角色
abc=ctx.guild.roles
如果在xx中扮演角色:
if在abc中的角色:#见脚注
等待用户。添加_角色(角色)
等待ctx.send(f'{user.antify},{role}已给定')
其他:
等待ctx.send('您要查找的角色不在服务器中')
其他:
等待ctx.send('用户已具有角色')
其他:
等待ctx.send('您没有足够的权限运行此命令')
#不相关的检查,如果用户有它,那么它保证在公会中。
#此外,如果它不存在,该命令将出错
#因为角色需要存在于公会中(arg类型中的discord.role)
#为了执行命令
我还删除了不必要的f字符串。它们用于格式化,因此使用了f
,在某些地方,您没有格式化字符串中的变量,所以我将它们取出
除此之外,到目前为止,您的代码看起来不错,请继续
参考文献:
- -Python 3.6.0+
- -所有这些属性都可在命令上下文中使用。它是第一个参数,通常命名为
ctx
- -请注意,它不是一个协程/函数,因此不需要用括号调用它
。在文档中,如果它是一个函数,您将看到末尾的括号,如果它是一个协同程序,则会看到前面的()
wait