|
| 1 | +# discord.py-with-message-components |
| 2 | +The Original [discord.py](https://github.com/Rapptz/discord.py) Libary made by [Rapptz](https://github.com/Rapptz) with implementation of the [Discord-Message-Components](https://discord.com/developers/docs/interactions/message-components) by [mccoderpy](https://github.com/mccoderpy/) |
| 3 | + |
| 4 | +([discord](https://discord.com/channels/@me)-User `mccuber04#2960`) |
| 5 | + |
| 6 | +## Installation: |
| 7 | + |
| 8 | +Replace the [discord](file:///\C:/%APPDATA%/Local/Programs/Python/Python39/Lib/site-packages/discord) folder in the site-packages with the discord folder contained in this repository or yust clone it with git. |
| 9 | + |
| 10 | + |
| 11 | +## Questions or ideas? |
| 12 | + |
| 13 | +Send me a direct-message on [Discord](https://discord.com/channels/@me): `mccuber04#2960` |
| 14 | + |
| 15 | +-------------------------------------------------------- |
| 16 | + |
| 17 | +# Examples: |
| 18 | + |
| 19 | +```py |
| 20 | +import discord |
| 21 | +from discord.ext import commands |
| 22 | +from discord import ActionRow, Button, ButtonColor |
| 23 | + |
| 24 | +client = commands.Bot(command_prefix=commands.when_mentioned_or('.!'), intents=discord.Intents.all(), case_insensitive=True) |
| 25 | + |
| 26 | + |
| 27 | +# an Command that sends you an Message and edit it if you click an Button |
| 28 | + |
| 29 | + |
| 30 | +@client.command(name='buttons', description='sends you some nice Buttons') |
| 31 | +async def buttons(ctx: commands.Context): |
| 32 | + components = [ActionRow(Button(label='Option Nr.1', |
| 33 | + custom_id='option1', |
| 34 | + emoji='1️1️⃣2️', |
| 35 | + style=ButtonColor.green |
| 36 | + ), |
| 37 | + Button(label='Option Nr.2', |
| 38 | + custom_id='option2', |
| 39 | + emoji='2️⃣', |
| 40 | + style=ButtonColor.blurple)), |
| 41 | + ActionRow(Button(label='A Other Row', |
| 42 | + custom_id='sec_row_1st option', |
| 43 | + emoji='😀'), |
| 44 | + Button(url='https://www.youtube.com/watch?v=dQw4w9WgXcQ', |
| 45 | + label="This is an Link", |
| 46 | + emoji='🎬')) |
| 47 | + ] |
| 48 | + an_embed = discord.Embed(title='Here are some Button\'s', description='Choose an option', color=discord.Color.random()) |
| 49 | + msg = await ctx.send(embed=an_embed, components=components) |
| 50 | + |
| 51 | + def _check(i: discord.RawInteractionCreateEvent): |
| 52 | + return i.message == msg and i.member == ctx.author |
| 53 | + |
| 54 | + interaction: discord.RawInteractionCreateEvent = await client.wait_for('interaction_create', check=_check) |
| 55 | + button_id = interaction.button.custom_id |
| 56 | + |
| 57 | + # This sends the Discord-API that the interaction has been received and is being "processed" (if this is not used, Discord will indicate that the interaction failed). |
| 58 | + await interaction.defer() |
| 59 | + |
| 60 | + if button_id == "option1": |
| 61 | + await interaction.message.edit(embed=an_embed.add_field(name='Choose', value=f'Your Choose was `{button_id}`'), components=[components[0].edit_obj(0, disabled=True), components[1]]) |
| 62 | + |
| 63 | + elif button_id == "option2": |
| 64 | + await interaction.message.edit(embed=an_embed.add_field(name='Choose', value=f'Your Choose was `{button_id}`'), components=[components[0].edit_obj(1, disabled=True), components[1]]) |
| 65 | + |
| 66 | + elif button_id == 'sec_row_1st option': |
| 67 | + await interaction.message.edit(embed=an_embed.add_field(name='Choose', value=f'Your Choose was `{button_id}`'), |
| 68 | + components=[components[0], components[1].edit_obj(0, disabled=True)]) |
| 69 | + |
| 70 | + # The Discord API doesn't send an event when you press a link button so we can't "receive" that. |
| 71 | + |
| 72 | +client.run('Your Bot-Token here') |
| 73 | +``` |
| 74 | +---------------------------------------------------------------------------------------------------- |
| 75 | +```py |
| 76 | + |
| 77 | +# Another command where a small embed is sent where you can move the small white ⬜ with the buttons. |
| 78 | + |
| 79 | +pointers = [] |
| 80 | + |
| 81 | + |
| 82 | +class Pointer: |
| 83 | + def __init__(self, guild: discord.Guild): |
| 84 | + self.guild = guild |
| 85 | + self._possition_x = 0 |
| 86 | + self._possition_y = 0 |
| 87 | + |
| 88 | + @property |
| 89 | + def possition_x(self): |
| 90 | + return self._possition_x |
| 91 | + |
| 92 | + def set_x(self, x: int): |
| 93 | + self._possition_x += x |
| 94 | + return self._possition_x |
| 95 | + |
| 96 | + @property |
| 97 | + def possition_y(self): |
| 98 | + return self._possition_y |
| 99 | + |
| 100 | + def set_y(self, y: int): |
| 101 | + self._possition_y += y |
| 102 | + return self._possition_y |
| 103 | + |
| 104 | + |
| 105 | +def get_pointer(obj: typing.Union[discord.Guild, int]): |
| 106 | + if isinstance(obj, discord.Guild): |
| 107 | + for p in pointers: |
| 108 | + if p.guild.id == obj.id: |
| 109 | + return p |
| 110 | + return get_pointer(obj) |
| 111 | + |
| 112 | + elif isinstance(obj, int): |
| 113 | + for p in pointers: |
| 114 | + if p.guild.id == obj: |
| 115 | + return p |
| 116 | + guild = client.get_guild(obj) |
| 117 | + if guild: |
| 118 | + pointers.append(Pointer(guild)) |
| 119 | + return get_pointer(guild) |
| 120 | + return None |
| 121 | + |
| 122 | + |
| 123 | +def display(x: int, y: int): |
| 124 | + base = [ |
| 125 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 126 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 127 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 128 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 129 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 130 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 131 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 132 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 133 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 134 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 135 | + ] |
| 136 | + base[y].__setitem__(x, 1) |
| 137 | + base.reverse() |
| 138 | + 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))) |
| 139 | + |
| 140 | + |
| 141 | +empty_button = discord.Button(style=discord.ButtonStyle.Secondary, label=" ", custom_id="empty", disabled=True) |
| 142 | + |
| 143 | + |
| 144 | +@property |
| 145 | +def arrow_button(): |
| 146 | + return discord.Button(style=discord.ButtonStyle.Primary) |
| 147 | + |
| 148 | + |
| 149 | +async def on_raw_interaction_create(interaction: discord.RawInteractionCreateEvent): |
| 150 | + await interaction.defer() |
| 151 | + pointer: Pointer = get_pointer(interaction.guild) |
| 152 | + if not (message := interaction.message): |
| 153 | + message: discord.Message = await interaction.channel.fetch_message(interaction.message_id) |
| 154 | + if interaction.button.custom_id == "links": |
| 155 | + await message.edit(embed=discord.Embed(title="Du Hast Links gewählt"), components=[discord.ActionRow(discord.Button(label='Links', custom_id='links', style=discord.ButtonStyle.Secondary, disabled=True), discord.Button(label='Rechts', custom_id='rechts', style=discord.ButtonStyle.Danger))]) |
| 156 | + elif interaction.button.custom_id == "rechts": |
| 157 | + await message.edit(embed=discord.Embed(title="Du Hast Rechts gewählt"), components=[discord.ActionRow(discord.Button(label='Links', custom_id='links', style=discord.ButtonStyle.Danger), discord.Button(label='Rechts', custom_id='rechts', style=discord.ButtonStyle.Secondary, disabled=True))]) |
| 158 | + elif interaction.button.custom_id == "up": |
| 159 | + pointer.set_y(1) |
| 160 | + await message.edit(embed=discord.Embed(title="Little Game", |
| 161 | + description=display(x=pointer.possition_x, y=pointer.possition_y)), |
| 162 | + components=[discord.ActionRow(empty_button, arrow_button.set_label('↑').set_custom_id('up').disable_if(pointer.possition_y >= 9), empty_button), |
| 163 | + 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))] |
| 164 | + ) |
| 165 | + elif interaction.button.custom_id == "down": |
| 166 | + pointer.set_y(-1) |
| 167 | + await message.edit(embed=discord.Embed(title="Little Game", |
| 168 | + description=display(x=pointer.possition_x, y=pointer.possition_y)), |
| 169 | + components=[discord.ActionRow(empty_button, arrow_button.set_label('↑').set_custom_id('up'), empty_button), |
| 170 | + 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))] |
| 171 | + ) |
| 172 | + elif interaction.button.custom_id == "right": |
| 173 | + pointer.set_x(1) |
| 174 | + await message.edit(embed=discord.Embed(title="Little Game", |
| 175 | + description=display(x=pointer.possition_x, y=pointer.possition_y)), |
| 176 | + components=[discord.ActionRow(empty_button, arrow_button.set_label('↑').set_custom_id('up'), empty_button), |
| 177 | + 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))] |
| 178 | + ) |
| 179 | + elif interaction.button.custom_id == "left": |
| 180 | + pointer.set_x(-1) |
| 181 | + await message.edit(embed=discord.Embed(title="Little Game", |
| 182 | + description=display(x=pointer.possition_x, y=pointer.possition_y)), |
| 183 | + components=[discord.ActionRow(empty_button, arrow_button.set_label('↑').set_custom_id('up'), empty_button), |
| 184 | + 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'))] |
| 185 | + ) |
| 186 | + |
| 187 | +``` |
0 commit comments