【问题标题】:How to fix "name 'server' is not defined" error in discord.py如何修复discord.py中的“名称'服务器'未定义”错误
【发布时间】:2018-12-28 22:34:46
【问题描述】:

我正在为我的 discord 服务器设置一个 python 机器人。我正在尝试添加一个功能,当人们加入时他们会键入 - 机器人会将他们分配给所述角色

我已尝试使用 message.roles 和 server.roles 分配我想要的角色,但两者仍然出现相同的错误。

BOT_PREFIX = ('-')

...

#ComputerScience
@client.command(pass_context = True)
async def Cs(member, *roles):
    role = discord.utils.get(server.roles, name="ComputerScience")
    await client.add_role(member, role)

当在聊天中输入“-Cs”时,它显示给他们计算机科学的角色,但我却得到: “忽略命令 Cs 中的异常...... NameError: name 'server' is not defined"

【问题讨论】:

  • 我从来不知道我必须定义服务器我将它更改为 member.server.roles 并尝试了 member.roles,但我收到错误“AttributeError: 'Context' object has no attribute 'roles'”

标签: python discord.py


【解决方案1】:

您正在传递invocation context,但在命令的函数签名中不接受它。您可以从该上下文中获取server

@client.command(pass_context = True)
async def Cs(ctx):
    role = discord.utils.get(ctx.message.server.roles, name="ComputerScience")
    await client.add_role(ctx.message.author, role)

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    相关资源
    最近更新 更多