classMyView(discord.ui.View):@discord.ui.button(label='A button', style=discord.ButtonStyle.primary, emoji='😎')asyncdefbutton_callback(self,button,interaction):await interaction.response.send_message('Button was pressed', ephemeral=True)view =MyView()@bot.command()asyncdefbutton(ctx):await ctx.send('Press the button!', view=view)
Sub-Classing discord.ui.Button
classMyButton(discord.ui.Button):def__init__(self):super().__init__(label='A button', style=discord.ButtonStyle.primary)asyncdefcallback(self,interaction):await interaction.response.send_message('Button was pressed', ephemeral=True)view = discord.ui.View()view.add_item(MyButton())@bot.command()async def ctx.send('Press the button!', view=view)
Using buttons with subclassing
view = discord.ui.View()view.add_item(discord.ui.Button(label='Go to website', url='https://gist.github.com/MhmCats/500eafdad0aaf278b94c612764688976', style=discord.ButtonStyle.url))
Button Styles
Styles
Name
Usage
Color
Primary
discord.ButtonStyle.primary
Blurple
Secondary
discord.ButtonStyle.secondary
Grey
Success
discord.ButtonStyle.success
Green
Danger
discord.ButtonStyle.danger
Red
Link
discord.ButtonStyle.link
Grey
URL Buttons
view = discord.ui.View()view.add_item(discord.ui.Button(label='Go to website', url='https://gist.github.com/MhmCats/500eafdad0aaf278b94c612764688976', style=discord.ButtonStyle.url))
@bot.command()asyncdefurl(ctx):await ctx.send(content="Press the button!", view=view)
Disabling Buttons
Disabling Buttons on Click
Here's how you can disable a single button or multiple buttons on press:
classMyView(discord.ui.View):@discord.ui.button(label='A button', style=discord.ButtonStyle.primary)asyncdefbutton_callback(self,button,interaction): button.disabled =True button.label ='No more pressing!'await interaction.response.edit_message(view=self)view =MyView()@bot.command()asyncdefbutton(ctx):await ctx.send('Press the button!', view=view)
classMyView(discord.ui.View):@discord.ui.button(emoji='😀', label="Button 1", style=discord.ButtonStyle.primary)asyncdefbutton_callback(self,button,interaction):for child in self.children: child.disabled =True button.label ='No more pressing either button!'await interaction.response.edit_message(view=self)@discord.ui.button(label="Button 2", style=discord.ButtonStyle.primary)asyncdefsecond_button_callback(self,button,interaction):for child in self.children: child.disabled =True button.label ='No more pressing either button!'await interaction.response.edit_message(view=self)@bot.command()asyncdefbutton(ctx):await ctx.send(content="Press the button!", view=MyView())
Making Pre-Disabled Buttons
classMyView(discord.ui.View):@discord.ui.button(label='A button', style=discord.ButtonStyle.primary, disabled=True)asyncdefbutton_callback(self,button,interaction): ...view =MyView()@bot.command()asyncdefbutton(ctx):await ctx.send('Press the button!', view=view)
Timeouts
You probably wanna set a timeout too, and its quite simple, really!
classMyView(discord.ui.View):def__init__(self):super().__init__(timeout=10)asyncdefon_timeout(self):for child in self.children: child.disabled =Trueawait self.message.edit(view=self)@discord.ui.button(style=discord.ButtonStyle.primary, emoji=':sunglasses:')asyncdefcallback(self,button,interaction):await interaction.response.send_message('Button was pressed', ephemeral=True)@bot.command()asyncdefhi(ctx): view =MyView() view.message =await ctx.send("hi", view=view)
Rows
A message can have up to five "action rows" and each of these "action" rows have five slots where you can put message components. A button takes up one of these slots but a select menus takes up all five slots of a "action row". Keep this in mind when creating your views since you don't want to run out of space!