Spaces:
Runtime error
Runtime error
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) | |
async def on_ready(): | |
print(f'We have logged in as {bot.user}') | |
await user.send('hello!') | |
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]) | |
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') |