Francesco
Francesco

Reputation: 175

Get a role with discord.py

I'm trying to get a user's role to execute a command:

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!")

When a normal user uses !clear, he can clear the messages, but also get the permission error.

Code:

@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!")

When a normal user uses !clear, he can clear the messages, but also get the permission error.

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!")

Upvotes: 2

Views: 10504

Answers (1)

Ynnc
Ynnc

Reputation: 26

Users that have the Mod role also have at least the @everyone role. So you need to change the else to elif not "Mod" in [y.name.lower() for y in ctx.message.author.roles]:.

Upvotes: 1

Related Questions