如何根据人员在命令中键入的内容创建一个discord bot

我是编码和制作不和谐机器人的新手,我已经让它使用命令发挥作用,但我无法弄清楚如何根据人们在命令中放置的内容来制作角色 . 例如,!rolecreate测试,如果我键入,我希望它创建一个名为test的角色并将其交给我 . 如果它有帮助,那么我只需要创建一个名为test的蓝色角色代码 .

https://pastebin.com/HMkLTkSe

@client.command(pass_context=True)
async def rolecreate(ctx):
    author = ctx.message.author
    await client.create_role(author.server, name='TEST', colour=discord.Colour(0x0000FF))

回答(1)

2 years ago

这是未经测试的,但这样的事情应该有效:

from discord.utils import get

@client.command(pass_context=True)
async def rolecreate(ctx):
    author = ctx.message.author
    # split the string to get the rolename to create
    role_name = ctx.message.content.lower().split("!rolecreate ", maxsplit=1)[1]
    # check if that role already exists
    check_for_duplicate = get(ctx.message.server.roles, name=role_name)
    if check_for_duplicate is not None: # if the role doesn't exist
        # create the role
        role = await client.create_role(author.server, name=role_name, colour=discord.Colour(0x0000FF))
        await client.add_roles(author, role)
    else:
        client.send_message(ctx.message.channel, "That role already exists")