File size: 1,454 Bytes
8ace1ff
 
 
 
7bf4d89
 
 
 
 
 
8ace1ff
 
 
2744111
8ace1ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix="!", intents=intents)
DiscordComponents(bot)

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')
    await user.send('hello!')

@bot.command()
async def button(ctx):
    """Sends a message with a button, and assigns a role when the button is clicked."""

    # Create a button
    button = Button(style=ButtonStyle.red, label="Click me!")

    # Send a message with the button
    await ctx.send("Click the button to get a role!", components=[button])

@bot.event
async def on_button_click(interaction):
    """Handle a button click."""

    # Check that the button's custom_id matches what you expect
    if interaction.component.label == "Click me!":

        # Get the role
        role = discord.utils.get(interaction.guild.roles, name="YOUR_ROLE_NAME")
        if role is None:
            await interaction.send(content="Role not found.", hidden=True)
            return

        # Assign the role
        await interaction.user.add_roles(role)

        # Respond with a message that only the person who clicked the button can see
        await interaction.send(content="You have been given a role!", hidden=True)

bot.run('e709ec16e02c87a6daa281f654203454f6c4b34469fa0e208dee162bd1cdd01f')