我有什么办法可以使机器人可以扮演角色

时间:2020-06-15 03:51:56

标签: python discord.py python-3.8

像标题一样,我需要制作一个可以以某种格式赋予角色的机器人。我已经设置了机器人以及所有东西,它可以执行基本命令。我想要它做的一个例子如下:

User: !!roles
Bot:  Who do you want to give roles to?
User: User1 User2 User3 User4     (etc...)
Bot: What role do you want to give them?
User: Admin               (Or rank1 rank2 etc...)

这是我创建的代码(是MESS):

@client.command(name="roles")
async def _roles(ctx):
  if ctx.message.author.server_permissions.administrator:
    global times_used
    times_used = times_used + 1
    await ctx.send(f"type who you want to give roles to, type cancel to cancel:")

    # This will make sure that the response will only be registered if the following
    # conditions are met:
    def check(msg):
      return msg.author == ctx.author and msg.channel == ctx.channel

    try:
      msg = await client.wait_for("message", check=check, timeout=120) # 120 seconds to reply

    except asyncio.TimeoutError:
      await ctx.send("Sorry, you didn't reply in time!")

    if(msg.content == "cancel"):
      return
    else:
      role = get(message.server.roles, name=role_to_give)
      content_split = msg.content.split()
      for user in content_split:
        await client.add_roles(user, role)

  else:
      msg = "You're an average joe {0.author.mention}".format(ctx.message)  
      await client.send_message(ctx.message.channel, msg)
      return

我正在使用python 3.8.3,将不胜感激!

1 个答案:

答案 0 :(得分:1)

您需要两个wait_for实例,一个实例读取用户列表,另一个实例读取角色列表。 add_role也在成员/用户对象上完成。 对于将一个类的多个实例作为参数,您会发现Greedy很有用。