Discord components python buttons

Components¶

This is located in discord.enums but i place it here.

The possible styles for a Button

link , grey_url , Link_Button

Represents an ActionRow -Part for the components of a discord.Message .

For general information about ActionRow’s visit the Discord-APIMethodes Documentation.

You could use a list instead of this but you don’t have the functions and parameters of this class then.

components – Union[ Button , SelectMenu ] The components the ActionRow should have. It could contain at least 5 Button or 1 SelectMenu .

classmethod from_dict ( data ) ¶

Converts a dict (e.g. created with to_dict() ) into an ActionRow, provided it is in the format Discord expects.

You can read about this format in the official Discord documentation.

dict – dict The dictionary to convert into an ActionRow.

Convert this ActionRow in to a dict.

Disable all object’s of type Button in this ActionRow .

disable_all_buttons_if ( check , * args ) ¶

Disable all Button (s) in this ActionRow if the passed check returns True .

  • check – Union[Bool, Callable] Could be a bool or usually any Callable that returns a bool
  • *args – Any Arguments that should passed in to the check if it is a Callable .

disable_all_select_menus_if ( check , * args ) ¶

Disables all SelectMenu (s) in this ActionRow if the passed check returns True .

  • check – Union[ bool , Callable] Could be a bool or usually any Callable that returns a bool .
  • *args – Any Arguments that should passed in to the check if it is a Callable .

This function returns the class instance to allow for fluent-style chaining.

component – Union[ Button , SelectMenu , dict ] The component to add to the ActionRow .

Add multiple components to the ActionRow .

This function returns the class instance to allow for fluent-style chaining.

*components – *Union[ Button , SelectMenu , dict ] The components to add to the ActionRow .

insert_component_at ( index , component ) ¶

Inserts a component before a specified index to the ActionRow .

  • index – int The index of where to insert the component.
  • component – Union[ Button , SelectMenu , dict ] The component to insert at the specified index of the ActionRow .

set_component_at ( index , component ) ¶

Modifies a component to the ActionRow object.

The index must point to a valid pre-existing component.

This function returns the class instance to allow for fluent-style chaining.

  • index – int The index of the component to modify.
  • component – Union[ Button , SelectMenu , dict ] The component to replace the old one with.
Читайте также:  Таблица

IndexError – An invalid index was provided.

Represents a Discord-Button

For general information about Discord-Button’s visit the Documentation of the discord-api.

  • label – str The Text displayed in discord on the Button. Maximal length is 80 Chars.
  • custom_id – Union[ str , int ] The custom_id discord send us when a User press the Button. The max. length of this is 100.
  • style – ButtonStyle The Style the Button should have.

Warning You can’t pass a custom_id and a url because discord don’t send anything when clicking on an URL-Button so it don’t accept both; url / ButtonStyle.url and custom_id !

Disable the Button if the passed check returns True .

  • check – typing.Union[ bool , Callable] Could be a bool or usually any Callable that returns a bool .
  • *args – Any Arguments that should passed in to the check if it is a Callable

set_style_if ( check , style , * args ) ¶

Sets the style of the Button to the specified one if the specified check returns True.

  • check – Union[ bool , typing.Callable ] The check could be an bool or usually any Callable that returns an bool
  • style – discord.ButtonStyle The style the Button should have when the check returns True
  • *args – Any Arguments that should passed in to the check if it is an Callable .

class SelectOption ( label , value , description , emoji , default ) ¶

Represents a option for a SelectMenu .

  • label – str The user-facing name of the option, max 25 characters.
  • value – str The dev-define value of the option, max 100 characters.
  • description – Optional[ str ] An additional description of the option, max 50 characters.
  • emoji – Optional[Union[ PartialEmoji , Emoji , str ]] An Emoji that will be displayed on the left side of the option.
  • default – Optional[ bool ] Whether this option is selected by default.

Represents a Discord-Select-Menu

For general information about Select-Menus visit the Discord-APIMethodes-Documentation.

  • custom_id – Union[ str , int ] A developer-defined identifier for the SelectMenu , max. 100 characters.
  • options – List[ SelectOption ] A list of choices( SelectOption ) the SelectMenu should have, max. 25.
  • placeholder – Optional[ str ] Custom placeholder text if nothing is selected, max. 100 characters.
  • min_values – Optional[: int ] The minimum number of items that must be chosen; default 1, min. 0, max. 25.
  • max_values – Optional[ int ] The maximum number of items that can be chosen; default 1, max. 25.
  • disabled – Optional[ bool ] Whether the SelectMenu is disabled or not. False by default.
Читайте также:  Html css javascript jquery book

Returns a generator with all values of the options of the SelectMenu .

If the value is a number it is returned as an integer, otherwise a string

for option in select.options: if option.value.isdigit(): yield int(option.value) else: yield option.value 

Disables the SelectMenu if the passed check returns True .

  • check – Union[ bool , Callable] The check could be an bool or usually any Callable that returns an bool .
  • *args – Any Arguments that should passed in to the check if it is an Callable .

custom_id – Union[ str , int ] The custom_id to replace the old one with

Indices and tables¶

© Copyright 2021, Mathieu Corsham aka. mccoder.py. Revision c3751e2c .

Источник

Welcome to discord.py-message-components’ documentation!¶

Discord Server Invite

PyPI Logo

Visit on PyPI here

You need help? Or have ideas/feedback?¶

Open a Issue/Pull request on GitHub, join the support-Server or send me a direct-message on Discord: mccuber04#2960

Installing:¶

Python 3.5.3 or higher is required

This Library overwrite the original discord.py Library so to be sure all will work fine first uninstall the original discord.py Library if it is installed:

# Linux/macOS python3 -m pip uninstall discord.py # Windows py -3 -m pip uninstall discord.py
# Linux/macOS python3 -m pip install -U discord.py-message-components # Windows py -3 -m pip install -U discord.py-message-components

quickstart¶

Sending-buttons¶

import discord from discord.ext import commands from discord import Button, ButtonStyle client = commands.Bot(command_prefix=commands.when_mentioned_or('!')) @client.command() async def buttons(ctx): await ctx.send('Hey here are some Buttons', components=[[ Button(label="Hey i\'m a red Button", custom_id="this is an custom_id", style=ButtonStyle.red), Button(label="Hey i\'m a green Button", custom_id="this is an custom_id", style=ButtonStyle.green), Button(label="Hey i\'m a blue Button", custom_id="this is an custom_id", style=ButtonStyle.blurple), Button(label="Hey i\'m a grey Button", custom_id="this is an custom_id", style=ButtonStyle.grey), Button(label="Hey i\'m a URL Button", url="https://pypi.org/project/discord.py-message-components", style=ButtonStyle.url) ]]) client.run('Your Bot-Token') 

Interact when a button was pressed¶

import discord from discord.ext import commands from discord import Button, ButtonStyle client = commands.Bot(command_prefix=commands.when_mentioned_or('!')) @client.command() async def buttons(ctx): msg_with_buttons = await ctx.send('Hey here are some Buttons', components=[[ Button(label="Hey i\'m a red Button", custom_id="red", style=ButtonStyle.red), Button(label="Hey i\'m a green Button", custom_id="green", style=ButtonStyle.green), Button(label="Hey i\'m a blue Button", custom_id="blue", style=ButtonStyle.blurple), Button(label="Hey i\'m a grey Button", custom_id="grey", style=ButtonStyle.grey) ]]) def check_button(i: discord.Interaction, button): return i.author == ctx.author and i.message == msg_with_buttons interaction, button = await client.wait_for('button_click', check=check_button) embed = discord.Embed(title='You pressed an Button', description=f'You pressed a button.custom_id> button.', color=discord.Color.random()) await interaction.respond(embed=embed) client.run('Your Bot-Token') 

You could set the parameter hidden in the response to True to make the message ephemeral. See discord.Interaction.respond for more information about respond() .

Sending-SelectMenu’s and respond to them¶

import discord from discord.ext import commands from discord import Button, SelectMenu, SelectOption client = commands.Bot(command_prefix=commands.when_mentioned_or('!')) @client.command() async def select(ctx): msg_with_selects = await ctx.send('Hey here is an nice Select-Menu', components=[ [ SelectMenu(custom_id='_select_it', options=[ SelectOption(emoji='1️⃣', label='Option Nr° 1', value='1', description='The first option'), SelectOption(emoji='2️⃣', label='Option Nr° 2', value='2', description='The second option'), SelectOption(emoji='3️⃣', label='Option Nr° 3', value='3', description='The third option'), SelectOption(emoji='4️⃣', label='Option Nr° 4', value='4', description='The fourth option')], placeholder='Select some Options', max_values=3) ]]) def check_selection(i: discord.Interaction, select_menu): return i.author == ctx.author and i.message == msg_with_selects interaction, select_menu = await client.wait_for('selection_select', check=check_selection) embed = discord.Embed(title='You have chosen:', description=f"You have chosen "+'\n'.join([f'\nOption Nr° o>' for o in select_menu.values]), color=discord.Color.random()) await interaction.respond(embed=embed) client.run('Your Bot-Token') 

coro¶

A coroutine is a function that must be invoked with await or yield from . When Python encounters an await it stops the function’s execution at that point and works on other things until it comes back to that point and finishes off its work. This allows for your program to be doing multiple things at the same time without using threads or complicated multiprocessing.

If you forget to await a coroutine then the coroutine will not run. Never forget to await a coroutine.

Источник

Оцените статью