Python 如何使用discord.py中的命令添加/创建角色

Python 如何使用discord.py中的命令添加/创建角色,python,discord,discord.py,Python,Discord,Discord.py,因此,我试图让用户查看一个潜在“角色”列表,并根据用户选择的内容(无论该角色目前是否存在),bot将向用户添加该角色,或创建一个新角色添加到用户。目前,我一直收到一个错误,说“消息”的名称没有定义,但我真的不知道该怎么办 这是我的密码 @client.command(pass_context = True) async def classes(): class_list = ["English", "Math", "Science", "History", "Geography", "Frenc

因此,我试图让用户查看一个潜在“角色”列表,并根据用户选择的内容(无论该角色目前是否存在),bot将向用户添加该角色,或创建一个新角色添加到用户。目前,我一直收到一个错误,说“消息”的名称没有定义,但我真的不知道该怎么办

这是我的密码

@client.command(pass_context = True)
async def classes():
class_list = ["English", "Math", "Science", "History", "Geography", "French","Technology", "Comp_Sci", "Business", "Music", "Art"]
entered_class = message.content
role = discord.utils.get(message.server.roles, name=entered_class)
roles = ["470082568163950612", "470082563696754708"]
if role is None:
    await client.create_role(name=entered_class, mentionable=True)
    await client.add_roles(message.author, role)
    msg = 'Successfully created and added role{0.author.mention}'.format(message)
    await client.send_message(message.channel, msg)
if role is True:
    await client.add_roles(message.author, role)
    await client.send_message(message.channel, msg)
这是我的进口货

import discord
from discord.utils import get
from discord.utils import find
from discord.ext import commands
from discord.ext.commands import Bot

TOKEN = 'NDcwMDYwMDc1MjgxODA5NDEw.DjQyZQ.j-YlOu5mZnDjpeUj32Nbc7wfbbs'


client = Bot(command_prefix = "Wood!")

任何帮助都将不胜感激

错误消息
名称“message”未定义
告诉您正在尝试访问一个不存在的名为“message”的变量

看看你的代码,它似乎在这里:

async def classes():
    class_list = ["English", "Math", "Science", "History", "Geography", "French","Technology", "Comp_Sci", "Business", "Music", "Art"]
    entered_class = HERE ---> message <--- .content
但实际上,这可能不是您想要的,因为它将包含命令名和前缀以及参数。您正在使用discord.py的命令扩展,因此您可能更喜欢这样做:

@client.command(pass_context = True)
async def classes(ctx, arg): # commands extension gets the argument for you!
然后只使用
arg
变量的值

另请注意,永远不要与任何人共享您的机器人令牌。现在返回到discord页面并重新生成它。这是因为有了这个令牌,任何看到它的人都可以访问你的机器人并让它做任何事情

@client.command(pass_context = True)
async def classes(ctx, arg): # commands extension gets the argument for you!