[英]How can I make a discord bot create a role based on what the person types in the command
I am new to coding and making discord bots, I have gotten it to make a role using a command but I can't figure out how to make it make a role based on what the person puts in the command. 我是编码和制作不一致机器人的新手,我已经知道它是使用命令来扮演角色的,但是我无法根据人在命令中的输入来弄清楚如何使其扮演角色。 For example, !rolecreate test, if I typed that I want it to make a role called test and give it to me. 例如,!rolecreate test,如果我键入它,我希望它扮演一个称为test的角色并将其交给我。 If it helps here is the code I have for just making a blue role called test. 如果有帮助,那么这里是我仅担任蓝色角色的代码,称为test。
https://pastebin.com/HMkLTkSe 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))
This is untested, but something like this should work: 这未经测试,但是类似这样的方法应该起作用:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.