python - 如何修复discord.py中的“名称'服务器'未定义”错误
问题描述
我正在为我的不和谐服务器设置一个 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:名称'服务器'未定义”
解决方案
您正在传递调用 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)
推荐阅读
- javascript - 在 React 中按 CTRL+ENTER 键后退出 ContentEditable 中的自定义元素样式
- python - 如何将我在文本冒险(函数)中的位置保存到文件中?
- sql - 如何使用列表中的变量创建 DB2 UNION 查询
- javascript - 如何使用 ReactJs 中的值访问键?
- json - 嵌套子键的 JSON 过滤
- javascript - 无法在 Javascript 中的数组中添加键值
- python - Python 中棘手的基线偏移
- swift - 如何在 swift UI 中的计算器 App 中创建字符串(格式:“%.f”)中的数字之间的间距
- python - 使用 BERT 示例输入错误
- python - 使用 Python 在我的家庭网络上获取设备的全名