繁体   English   中英

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

[英]How to add/create roles using commands in discord.py

因此,我试图让用户查看潜在的“角色”列表,并根据用户选择的内容(该角色当前是否存在),机器人将向用户添加该角色,或创建要添加到用户的新角色。 目前,我一直收到错误消息,说未定义名称“ message”,但我不确定该怎么做。

这是我的代码

@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!")

任何帮助将非常感激

错误消息name "message" is not defined ,表明您正在尝试访问不存在的名为“ message”的变量。

查看您的代码,似乎在这里:

async def classes():
    class_list = ["English", "Math", "Science", "History", "Geography", "French","Technology", "Comp_Sci", "Business", "Music", "Art"]
    entered_class = HERE ---> message <--- .content

您需要的是:

@client.command(pass_context = True)
async def classes(ctx): # note that we add a `ctx` argument here
    class_list = ["English", "Math", "Science", "History", "Geography", "French","Technology", "Comp_Sci", "Business", "Music", "Art"]
    entered_class = ctx.message.content # we get the message from the context here

但是实际上,这可能不是您想要的,因为它将包含命令名称和前缀以及参数。 您正在使用discord.py的命令扩展名,因此您可能更喜欢这样做:

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

然后只需使用arg变量的值即可。

另外, 永远不要与任何人共享您的机器人令牌。 回到不和谐的页面, 现在重新生成。 这是因为有了该令牌, 任何看到它的人都可以访问您的机器人并使它执行任何操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM