T
T
Telmor2021-09-09 21:36:24
Python
Telmor, 2021-09-09 21:36:24

How to assign role by button in discord.py?

I have a command that I call and 5 buttons appear. By clicking on which USERS who want to click on the button and get the role, and if they do it again, they remove it. The question is: how to make an endless button (so that it can be clicked not only by the caller of the command, but also by all other users of the server, but also as many times as you like), how to track exactly who pressed the button (to issue a role).
My code:

@bot.command(aliases='ВыдачаИгровыхРолей')
async def __giveplayroles__(ctx):
    await ctx.channel.purge(limit = 1)
    msg = await ctx.send(
        emb = discord.Embed(title='Игровые Роли',color=0xE7EEF5, description=f'Нажмите на кнопку что бы получить роль, что бы убрать роль нажмите второй раз.'),
        components = [
            Button(style = ByttonStyle.blue, label = 'Rust'),
            Button(style = ByttonStyle.blue, label = 'Dota 2'),
            Button(style = ByttonStyle.blue, label = 'CS:GO'),
            Button(style = ByttonStyle.blue, label = 'Genshin Impact'),
            #Button(style = ByttonStyle.blue, label = 'Osu!'),
            #Button(style = ByttonStyle.blue, label = 'Minecraft'),
            #Button(style = ByttonStyle.blue, label = 'GTA')
        ])
     responce = await bot.wait_for('button_click')
     if responce.component.label == 'Rust':
        role = discord.utils.get(ctx.author.server.roles, id=864796640929251349)
        if not role in ctx.author.roles:
          role = discord.utils.get(ctx.author.guild.roles, id=864796640929251349)
          await member.add_roles(role)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
x4zx, 2021-09-09
@x4zx

import discord
from discord import member
from discord.ext import commands
from dislash import InteractionClient, ActionRow, Button, ButtonStyle

intents = discord.Intents.all()
bot = commands.Bot(command_prefix = "!", intents = intents)
bot.remove_command("help")

inter_client = InteractionClient(bot)

@bot.event
async def on_ready():
    print(f'Вы вошли как {bot.user}')

@bot.command()
async def verif(ctx):

    emb = discord.Embed(
        description = 
        f"""
        Здраствуйте вы попали на сервер {ctx.guild.name}, пройдите верификацию чтобы получить доступ к другим каналам.
        """,
        colour = 0xFF8C00
    )
    emb.set_thumbnail(url = 'https://cdn.discordapp.com/attachments/772850448892690462/880752123418136596/947d1f802c858b540b84bc3000fc2439_1_-removebg-preview.png')
    emb.set_author(name = 'Верификация')

    row = ActionRow(
        Button(
            style = ButtonStyle.gray,
            label = 'Верифицироваться',
            custom_id = 'verif_button'
        )
    )
    await ctx.send(embed = emb, components = [row])

@bot.event
async def on_button_click(inter):

    res = 'Вы успешно верифицировались!' # ваш вывод сообщение что человек получил роль
    guild = bot.get_guild(inter.guild.id)

    if inter.component.id == "verif_button":
        verif = guild.get_role(id вашей роли)
        member = inter.author
        await member.add_roles(verif)
        await inter.reply(res, ephemeral = True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question