Search code examples
pythondiscordbots

How to Check discord member roles


I'm have a problem with a part of my python code , I created a discord bot for showing the balances of a game to users they are 3 type of people I mean 3 different discord role when i use !payment i want check their role and show their balances (right now there is alot of records i want to make it clean)

elif message.content.startswith('!payment'):
        await message.add_reaction("✅")
        if not any(role.name in['Guild Master','Calculator','Programmer'] for role in message.author.roles):
            await message.channel.send("You don't have permission to use this command")
            return
        c.execute('SELECT Tag,Balance,Date_Time FROM dbr1')
        data=c.fetchall()
        async def send_embeds(message,data):
            embed = discord.Embed(title="Information", description="**Details for all users.**", colour=discord.Colour(int(custom_color, 16)))
            field_count = 0
            for keys in data:
                tag, balance, date_time = keys
                f_p_c = '{:,.0f}'.format(balance).replace(',', '.')
                embed.add_field(name=f':pencil:___________________________', value=f":id:{tag}\n ``` Balance : {f_p_c}\n ```", inline=False)
                field_count += 1
                if field_count >= 25:
                    await message.channel.send(embed=embed)
                    embed = discord.Embed(title="Information", description="details for all users.", colour=discord.Colour(int(custom_color, 16)))
                    field_count=0
            if field_count>0:
                    await message.channel.send(embed=embed)
        await send_embeds(message, data)
        process_payment=False

I cant put their roles in my database i want program extract their roles


Solution

  • After fixing formatting and correcting some syntax (based on the answer by @hossein-dahaei), you can see the corrected code. It uses the multiple embeds functionality present within Discord to bundle multiple embeds together in a single message.

    if message.content.startswith('!payment'):
        await message.add_reaction("✅")
        allowed_roles = ['Guild Master', 'Calculator', 'Programmer']
        user_roles = [role.name for role in message.author.roles]
    
        if not any(role in allowed_roles for role in user_roles):
            await message.channel.send("You don't have permission to use this command")
            return
    
        c.execute('SELECT Tag, Balance, Date_Time FROM dbr1')
        data = c.fetchall()
    
        embeds = []
    
        embed = discord.Embed(
            title="Information", 
            description="Details for all users.", 
            colour=discord.Colour(int(custom_color, 16))
        )
    
        embeds.append(embed)
    
        field_count = 0
    
        for keys in data:
            tag, balance, date_time = keys
            formatted_balance = '{:,.0f}'.format(balance).replace(',', '.')
    
            embed.add_field(
                name=f':pencil:___________________________',
                value=f":id:{tag}\n Balance : {formatted_balance}\n",
                inline=False
            )
    
            field_count += 1
    
            if field_count >= 25:
                embeds.append(embed)
                embed = discord.Embed(
                    title="Information", 
                    description="details for all users.", 
                    colour=discord.Colour(int(custom_color, 16))
                )
                field_count = 0
    
            if len(embeds) >= 10:
                await message.channel.send(embeds=embeds)
                embeds = []
                field_count = 0
    
        # If we didn't reach 10 embeds, and the 
        # other send branch is not run, send the rest of the embeds
        if len(embeds) > 0:
            await message.channel.send(embeds=embeds)
    
    process_payment = False