Aiden Zhang
Aiden Zhang

Reputation: 54

Discord.py: How Should I Use Has_Role In a If Statement

I was just setting up role requirements for certain commands. But I'm using message.content to make the commands. Because of this All My Commands are in one client.event, I Don't Want all my Commands to Need Mod/Admin to Use.

What Should I do?

Code for One of The Commands:

from discord.utils import get

@client.event
async def on_message(message):
  
  if message.content.startswith('-queue duos'):
    print('you have queued')
    
      
    role = discord.utils.get(message.guild.roles, id=830219965549510656) 
    await message.author.add_roles(role) 

    role4 = discord.utils.get(message.guild.roles, id=830557149863608367)
    await message.author.remove_roles(role4)

    await message.channel.send('you have joined the duos queue sit tight!')

Upvotes: -1

Views: 643

Answers (1)

Anzam Siddek
Anzam Siddek

Reputation: 19

You can get the role object and then check if the user has the role:

from discord.utils import get

@client.event
async def on_message(message):
  
  if message.content.startswith('-queue duos'):
    role = discord.utils.get(message.guild.roles, id=id_here)
    if role in message.author.roles:
      print('you have queued')
    
      
      role = discord.utils.get(message.guild.roles, id=830219965549510656) 
      await message.author.add_roles(role) 

      role4 = discord.utils.get(message.guild.roles, id=830557149863608367)
      await message.author.remove_roles(role4)

      await message.channel.send('you have joined the duos queue sit tight!')
    else:
      await message.channel.send('You do not have the required permissions')

Upvotes: 1

Related Questions