Package: discord.py-message-components
The Originaldiscord.pyLibrary made byRapptzwith implementation of theDiscord-Message-ComponentsbymccoderpyRead the DocumentationhereYou need help? Or have ideas/feedback?Open a Issue/Pull request onGitHub, join thesupport-Serveror send me a direct-message onDiscord:mccuber04#2960InstallingPython 3.5.3 or higher is requiredThis Library overwrite the original discord.py Library so to be sure all will work fine
first uninstall the originaldiscord.pyLibrary if it is installed:# Linux/macOSpython3-mpipuninstalldiscord.py# Windowspy-3-mpipuninstalldiscord.pyThen installthis Libraryusing:# Linux/macOSpython3-mpipinstall-Udiscord.py-message-components# Windowspy-3-mpipinstall-Udiscord.py-message-componentsExamplesA Command that sends you a Message and edit it when you click a Button:importtypingimportdiscordfromdiscord.extimportcommandsfromdiscordimportActionRow,Button,ButtonStyleclient=commands.Bot(command_prefix=commands.when_mentioned_or('.!'),intents=discord.Intents.all(),case_insensitive=True)@client.command(name='buttons',description='sends you some nice Buttons')asyncdefbuttons(ctx:commands.Context):components=[ActionRow(Button(label='Option Nr.1',custom_id='option1',emoji="🆒",style=ButtonStyle.green),Button(label='Option Nr.2',custom_id='option2',emoji="🆗",style=ButtonStyle.blurple)),ActionRow(Button(label='A Other Row',custom_id='sec_row_1st option',style=ButtonStyle.red,emoji='😀'),Button(url='https://www.youtube.com/watch?v=dQw4w9WgXcQ',label="This is an Link",style=ButtonStyle.url,emoji='🎬'))]an_embed=discord.Embed(title='Here are some Button\'s',description='Choose an option',color=discord.Color.random())msg=awaitctx.send(embed=an_embed,components=components)def_check(i:discord.Interaction,b):returni.message==msgandi.member==ctx.authorinteraction,button=awaitclient.wait_for('button_click',check=_check)button_id=button.custom_id# This sends the Discord-API that the interaction has been received and is being "processed"awaitinteraction.defer()# if this is not used and you also do not edit the message within 3 seconds as described below,# Discord will indicate that the interaction has failed.# If you use interaction.edit instead of interaction.message.edit, you do not have to defer the interaction,# if your response does not last longer than 3 seconds.awaitinteraction.edit(embed=an_embed.add_field(name='Choose',value=f'Your Choose was `{button_id}`'),components=[components[0].disable_all_buttons(),components[1].disable_all_buttons()])# The Discord API doesn't send an event when you press a link button so we can't "receive" that.client.run('You Bot-Token here')Another (complex) Example where a small Embed will be send; you can move a small white ⬜ with the Buttons:pointers=[]classPointer:def__init__(self,guild:discord.Guild):self.guild=guildself._possition_x=0self._possition_y=0@propertydefpossition_x(self):returnself._possition_xdefset_x(self,x:int):self._possition_x+=xreturnself._possition_x@propertydefpossition_y(self):returnself._possition_ydefset_y(self,y:int):self._possition_y+=yreturnself._possition_ydefget_pointer(obj:typing.Union[discord.Guild,int]):ifisinstance(obj,discord.Guild):forpinpointers:ifp.guild.id==obj.id:returnppointers.append(Pointer(obj))returnget_pointer(obj)elifisinstance(obj,int):forpinpointers:ifp.guild.id==obj:returnpguild=client.get_guild(obj)ifguild:pointers.append(Pointer(guild))returnget_pointer(guild)returnNonedefdisplay(x:int,y:int):base=[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]]base[y][x]=1base.reverse()return''.join(f"\n{''.join([str(base[i][w])forwinrange(len(base[i]))]).replace('0','⬛').replace('1','⬜')}"foriinrange(len(base)))empty_button=discord.Button(style=discord.ButtonStyle.Secondary,label=" ",custom_id="empty",disabled=True)defarrow_button():returndiscord.Button(style=discord.ButtonStyle.Primary)@client.command(name="start_game")asyncdefstart_game(ctx:commands.Context):pointer:Pointer=get_pointer(ctx.guild)awaitctx.send(embed=discord.Embed(title="Little Game",description=display(x=0,y=0)),components=[discord.ActionRow(empty_button,arrow_button().set_label('↑').set_custom_id('up'),empty_button),discord.ActionRow(arrow_button().update(disabled=True).set_label('←').set_custom_id('left').disable_if(pointer.possition_x<=0),arrow_button().set_label('↓').set_custom_id('down').disable_if(pointer.possition_y<=0),arrow_button().set_label('→').set_custom_id('right'))])@client.on_click()asyncdefup(i:discord.Interaction,button):pointer:Pointer=get_pointer(interaction.guild)pointer.set_y(1)awaiti.edit(embed=discord.Embed(title="Little Game",description=display(x=pointer.possition_x,y=pointer.possition_y)),components=[discord.ActionRow(empty_button,arrow_button().set_label('↑').set_custom_id('up').disable_if(pointer.possition_y>=9),empty_button),discord.ActionRow(arrow_button().set_label('←').set_custom_id('left').disable_if(pointer.possition_x<=0),arrow_button().set_label('↓').set_custom_id('down'),arrow_button().set_label('→').set_custom_id('right').disable_if(pointer.possition_x>=9))])@client.on_click()asyncdefdown(i:discord.Interaction,button):pointer:Pointer=get_pointer(interaction.guild)pointer.set_y(-1)awaitmessage.edit(embed=discord.Embed(title="Little Game",description=display(x=pointer.possition_x,y=pointer.possition_y)),components=[discord.ActionRow(empty_button,arrow_button().set_label('↑').set_custom_id('up'),empty_button),discord.ActionRow(arrow_button().set_label('←').set_custom_id('left').disable_if(pointer.possition_x<=0),arrow_button().set_label('↓').set_custom_id('down').disable_if(pointer.possition_y<=0),arrow_button().set_label('→').set_custom_id('right').disable_if(pointer.possition_x>=9))])@client.on_click()asyncdefright(i:discord.Interaction,button):pointer:Pointer=get_pointer(interaction.guild)pointer.set_x(1)awaitmessage.edit(embed=discord.Embed(title="Little Game",description=display(x=pointer.possition_x,y=pointer.possition_y)),components=[discord.ActionRow(empty_button,arrow_button().set_label('↑').set_custom_id('up'),empty_button),discord.ActionRow(arrow_button().set_label('←').set_custom_id('left'),arrow_button().set_label('↓').set_custom_id('down'),arrow_button().set_label('→').set_custom_id('right').disable_if(pointer.possition_x>=9))])@client.on_click()asyncdefleft(i:discord.Interaction,button):pointer:Pointer=get_pointer(interaction.guild)pointer.set_x(-1)awaitmessage.edit(embed=discord.Embed(title="Little Game",description=display(x=pointer.possition_x,y=pointer.possition_y)),components=[discord.ActionRow(empty_button,arrow_button().set_label('↑').set_custom_id('up'),empty_button),discord.ActionRow(arrow_button().set_label('←').set_custom_id('left').disable_if(pointer.possition_x<=0),arrow_button().set_label('↓').set_custom_id('down'),arrow_button().set_label('→').set_custom_id('right'))])Take a look atthe documentationto see more examples.