discord.py-message-components
The Original discord.py LibaryLibrary made by Rapptz with implementation of the Discord-Message-Components by mccoderpy
(discord-User mccuber04#2960
)
Installing
Python 3.5.3 or higher is required
first uninstall the original discord.py LibaryLibrary:
# 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
Another (complex) Example where a small Embed will be send; you can move a small white ⬜ with the Buttons:
pointers = [] class Pointer: def __init__(self, guild: discord.Guild): self.guild = guild self._possition_x = 0 self._possition_y = 0 @property def possition_x(self): return self._possition_x def set_x(self, x: int): self._possition_x += x return self._possition_x @property def possition_y(self): return self._possition_y def set_y(self, y: int): self._possition_y += y return self._possition_y def get_pointer(obj: typing.Union[discord.Guild, int]): if isinstance(obj, discord.Guild): for p in pointers: if p.guild.id == obj.id: return p pointers.append(Pointer(obj)) return get_pointer(obj) elif isinstance(obj, int): for p in pointers: if p.guild.id == obj: return p guild = client.get_guild(obj) if guild: pointers.append(Pointer(guild)) return get_pointer(guild) return None def display(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].__setitem__(x, 1) base.reverse() return ''.join(f"\n{''.join([str(base[i][w]) for w in range(len(base[i]))]).replace('0', '⬛').replace('1', '⬜')}" for i in range(len(base))) empty_button = discord.Button(style=discord.ButtonStyle.Secondary, label=" ", custom_id="empty", disabled=True) def arrow_button(): return discord.Button(style=discord.ButtonStyle.Primary) @client.command(name="start_game") async def start_game(ctx: commands.Context): pointer: Pointer = get_pointer(ctx.guild) await ctx.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.event async def on_raw_interaction_create(interaction: discord.RawInteractionCreateEvent): await interaction.defer() pointer: Pointer = get_pointer(interaction.guild) if not (message := interaction.message): message: discord.Message = await interaction.channel.fetch_message(interaction.message_id) if interaction.button.custom_id == "up": pointer.set_y(1) await message.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))] ) elif interaction.button.custom_id == "down": pointer.set_y(-1) await message.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))] ) elif interaction.button.custom_id == "right": pointer.set_x(1) await message.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))] ) elif interaction.button.custom_id == "left": pointer.set_x(-1) await message.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'))] )
0 commit comments