-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexamples.py
181 lines (148 loc) · 9.3 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import typing
import discord
from discord.ext import commands
from discord import ActionRow, Button, ButtonColor
client = commands.Bot(command_prefix=commands.when_mentioned_or('.!'), intents=discord.Intents.all(), case_insensitive=True)
# A Command that sends you a Message and edit it when you click a Button
@client.command(name='buttons', description='sends you some nice Buttons')
async def buttons(ctx: commands.Context):
components = [ActionRow(Button(label='Option Nr.1',
custom_id='option1',
emoji="🆒",
style=ButtonColor.green
),
Button(label='Option Nr.2',
custom_id='option2',
emoji="🆗",
style=ButtonColor.blurple)),
ActionRow(Button(label='A Other Row',
custom_id='sec_row_1st option',
style=ButtonColor.red,
emoji='😀'),
Button(url='https://www.youtube.com/watch?v=dQw4w9WgXcQ',
label="This is an Link",
emoji='🎬'))
]
an_embed = discord.Embed(title='Here are some Button\'s', description='Choose an option', color=discord.Color.random())
msg = await ctx.send(embed=an_embed, components=components)
def _check(i: discord.Interaction, b: discord.ButtonClick):
return i.message == msg and i.author == ctx.author
interaction, button = await client.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"
await interaction.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 deffer the interaction if your response does not last longer than 3 seconds.
await interaction.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('your Bot-Token here')
########################################################################################################################
# Another command where a small embed is sent where you can move the small white ⬜ with the buttons.
pointers = []
class Pointer:
"""Just a small class that facilitates holding and changing the position of the square."""
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):
"""This function ``renders`` and returns the image"""
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_button_click(interaction: discord.Interaction, button: discord.ButtonClick):
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 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 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 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 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'))]
)