Skip to content

Commit fa8d5d3

Browse files
authored
Add files via upload
1 parent 80ff494 commit fa8d5d3

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

examples.py

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

0 commit comments

Comments
 (0)