使用discord.py获取角色

时间:2017-10-06 14:58:15

标签: python python-3.x discord discord.py

我试图获得用户执行命令的角色:

async def clear (ctx, n):
    if "Mod" in [y.name.lower() for y in ctx.message.author.roles]:
       //delete messages
    else:
       client.send_message(ctx.message.channel, "You are not allowed to use this command!")

普通用户使用!clear时,他可以清除消息,但也会收到权限错误。

代码:

@bot.command(pass_context=True)
async def clear(ctx, n):
   if "mod" in [y.name.lower() for y in ctx.message.author.roles]:
       n = int(n)
       tn = n + 1
       async for x in bot.logs_from(ctx.message.channel, limit=tn):
          await bot.delete_messages(x)

          await bot.send_message(ctx.message.channel, "Deleted" + str(n) + "messages")
   elif not "mod" in [y.name.lower() for y in ctx.message.author.roles]:
       await bot.send_message(ctx.message.channel, "You need the **Mod** role to use this command!")

普通用户使用!clear时,他可以清除消息,但也会收到权限错误。

SOLUTION:

@bot.command(pass_context=True)
async def clear(ctx, n):
   if "mod" in [y.name.lower() for y in ctx.message.author.roles]:
       n = int(n)
       msg = []
       tn = n + 1
       async for x in bot.logs_from(ctx.message.channel, limit=tn):
          msg.append(x)
          await bot.delete_messages(x)

       await bot.send_message(ctx.message.channel, "Deleted" + str(n) + "messages")
   elif not "mod" in [y.name.lower() for y in ctx.message.author.roles]:
       await bot.send_message(ctx.message.channel, "You need the **Mod** role to use this command!")

1 个答案:

答案 0 :(得分:1)

具有Mod角色的用户也至少具有@everyone角色。因此,您需要将else更改为elif not "Mod" in [y.name.lower() for y in ctx.message.author.roles]: