[英]How would I make it so that an individual person is the only one who can run a command with a discord bot?
[英]How can I make a discord bot create a role based on what the person types in the command
我是編碼和制作不一致機器人的新手,我已經知道它是使用命令來扮演角色的,但是我無法根據人在命令中的輸入來弄清楚如何使其扮演角色。 例如,!rolecreate test,如果我鍵入它,我希望它扮演一個稱為test的角色並將其交給我。 如果有幫助,那么這里是我僅擔任藍色角色的代碼,稱為test。
@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))
這未經測試,但是類似這樣的方法應該起作用:
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.