-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_ui.py
More file actions
444 lines (386 loc) · 16.7 KB
/
bot_ui.py
File metadata and controls
444 lines (386 loc) · 16.7 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# pylint: disable=bad-indentation,arguments-differ,arguments-renamed
from contextlib import suppress
import discord
from random import choice
from wavelink import QueueMode
def formatted_time(time):
mins, secs = divmod(round(time/1000), 60)
return f"{int(mins):0>2}:{int(secs):0>2}"
class CancelMirror(discord.ui.View):
def __init__(self, file_obj, timeout=None):
super().__init__(timeout=timeout)
self.file = file_obj
@discord.ui.button(label="Cancel", style=discord.ButtonStyle.danger)
async def cancel_task(self, inter: discord.Interaction, button):
if inter.user not in (self.file.ctx.author, inter.client.owner, inter.guild.owner):
await inter.response.send_message("**:x: You are not the one who started this mirror task!**", ephemeral=True)
else:
button.disabled = True
await inter.response.edit_message(view=self)
await self.file.cancel()
class ConfirmButtons(discord.ui.View):
"""Are you sure? :/"""
def __init__(self, timeout=40):
super().__init__(timeout=timeout)
self.value = None
@discord.ui.button(label="✓", custom_id="yes_button", style=discord.ButtonStyle.success)
async def confirm(self, inter: discord.Interaction, button):
for child in self.children:
child.disabled = True
button.style = discord.ButtonStyle.primary
self.value = True
await inter.response.edit_message(view=self)
self.stop()
@discord.ui.button(label="x", custom_id="!yes_button", style=discord.ButtonStyle.danger)
async def reject(self, inter: discord.Interaction, button):
for item in self.children:
item.disabled = True
button.style = discord.ButtonStyle.primary
self.value = False
await inter.response.edit_message(view=self)
self.stop()
# class ButtonFHD(discord.ui.Button):
# def __init__(self):
# super().__init__(
# style=discord.ButtonStyle.primary,
# custom_id='1080p', label='1080p HD video'
# )
# async def callback(self, interaction):
# self.style = discord.ButtonStyle.success
# for item in self.view.children:
# item.disabled = True
# self.view.itag_value = 248
# await interaction.response.edit_message(view=self.view)
# self.view.stop()
class SelectResolution(discord.ui.View):
def __init__(self, *, author, timeout: float = 30):
super().__init__(timeout=timeout)
self.itag_value = None
self.author = author
async def interaction_check(self, interaction):
return self.author == interaction.user
@discord.ui.button(label="720p Video", custom_id='video', style=discord.ButtonStyle.primary)
async def video_button(self, inter, button):
button.style = discord.ButtonStyle.success
for item in self.children:
item.disabled = True
self.itag_value = '22'
await inter.response.edit_message(view=self)
self.stop()
@discord.ui.button(label="128kbps Audio", custom_id='audio', style=discord.ButtonStyle.primary)
async def audio_button(self, inter, button):
button.style = discord.ButtonStyle.success
for item in self.children:
item.disabled = True
self.itag_value = '140'
await inter.response.edit_message(view=self)
self.stop()
class SearchChoiceSelect(discord.ui.Select):
def __init__(self, track_titles):
self.titles = track_titles
opts = [
discord.SelectOption(
label=self.titles[x]['title'][:95],
description=self.titles[x]['channel'],
value=x
) for x in range(len(track_titles))
]
super().__init__(
placeholder="Click here to view the list and select",
custom_id='search_results',
max_values=1,
min_values=1,
options=opts,
row=0
)
async def callback(self, inter):
user_choice = int(self.values[0])
self.placeholder = self.titles[user_choice]['title']
for item in self.view.children:
item.disabled = True
self.view.choice = user_choice
self.view.stop()
return await inter.response.edit_message(view=self.view)
class SearchChoice(discord.ui.View):
choice = None
def __init__(self, track_titles, user, timeout=30):
super().__init__(timeout=timeout)
self.track_titles = track_titles
self.author = user
self.add_item(SearchChoiceSelect(track_titles=self.track_titles))
async def on_error(self, inter, error, item):
item.disabled = True
try:
raise error
except discord.DiscordException as e:
await inter.channel.send(e)
await inter.response.edit_message(view=self)
async def interaction_check(self, inter):
return self.author == inter.user
@discord.ui.button(label="Cancel", custom_id="cancel", style=discord.ButtonStyle.danger, row=1)
async def cancel_search(self, inter, button: discord.ui.Button):
button.label = "Cancelled"
for item in self.children:
item.disabled = True
self.choice = "cancel"
self.stop()
await inter.response.edit_message(view=self)
class RPSChoice(discord.ui.View):
def __init__(self, embed, timeout):
super().__init__(timeout=timeout)
self.emb = embed
@discord.ui.button(label="Rock", emoji="👊", style=discord.ButtonStyle.primary)
async def rock_choice(self, inter, button):
bots_choice = choice(["r", "p", "s"])
if bots_choice == "r":
self.emb.description = "Phew!! What a close call! Both of us have choosen Rock. It's a tie."
elif bots_choice == "s":
self.emb.description = "Congo buddy! I have choosen Scissor and you have choosen rock. The Rock wins"
button.style = discord.ButtonStyle.success
elif bots_choice == "p":
self.emb.description = "Aaha! You loose buddy! You've choosen Rock and I have choosen Paper. And Paper wins against Rock. :)"
button.style = discord.ButtonStyle.danger
for item in self.children:
if item != button:
item.style = discord.ButtonStyle.grey
item.disabled = True
self.stop()
await inter.response.edit_message(embed=self.emb, view=self)
@discord.ui.button(label="Paper", emoji="✋", style=discord.ButtonStyle.primary)
async def paper_choice(self, inter, button):
bots_choice = choice(["r", "p", "s"])
if bots_choice == "p":
self.emb.description = "Pheww!! What a close call! Both of us have choosen Paper. It's a tie."
elif bots_choice == "r":
self.emb.description = "Congo buddy! I have choosen Rock and you have choosen Paper. The Paper wins"
button.style = discord.ButtonStyle.success
elif bots_choice == "s":
self.emb.description = "Aaha! You loose buddy! You've choosen Paper and I have choosen Scissor. And Scissor wins against Paper. :)"
button.style = discord.ButtonStyle.danger
for item in self.children:
if item != button:
item.style = discord.ButtonStyle.grey
item.disabled = True
self.stop()
await inter.response.edit_message(embed=self.emb, view=self)
@discord.ui.button(label="Scissor", emoji="✌️", style=discord.ButtonStyle.primary)
async def scissor_choice(self, inter, button):
bots_choice = choice(["r", "p", "s"])
if bots_choice == "s":
self.emb.description = "Pheww!! What a close call! Both of us have choosen Scissor. It's a tie."
elif bots_choice == "p":
self.emb.description = "Congo buddy! I have choosen Paper and you have choosen Scissor. The Scissor wins"
button.style = discord.ButtonStyle.success
elif bots_choice == "r":
self.emb.description = "Aaha! You loose buddy! You've choosen Scissor and I have choosen Rock. And Rock wins against Scissor. :)"
button.style = discord.ButtonStyle.danger
for item in self.children:
if item != button:
item.style = discord.ButtonStyle.grey
item.disabled = True
self.stop()
await inter.response.edit_message(embed=self.emb, view=self)
@discord.ui.button(label="Cancel", style=discord.ButtonStyle.danger)
async def cancel_rps(self, interact, item):
item.style = discord.ButtonStyle.grey
self.emb.description = ":white_check_mark: **Cancelled!**"
for button in self.children: button.disabled = True
self.stop()
await interact.response.edit_message(embed=self.emb, view=self)
class PageButtons(discord.ui.View):
def __init__(self, embeds, timeout=240):
super().__init__(timeout=timeout)
self.embeds = embeds
self.current_page = 0
async def on_error(self, interaction, error, item):
item.label = "Error"
item.style = discord.ButtonStyle.danger
item.disabled = True
await interaction.response.edit_message(view=self)
print(error.__class__.__name__, ":", str(error))
@discord.ui.button(emoji='\U00002B05', custom_id="page_previous", style=discord.ButtonStyle.primary)
async def previous_page(self, interaction: discord.Interaction, button: discord.ui.Button):
button.style = discord.ButtonStyle.secondary if button.style == discord.ButtonStyle.primary else discord.ButtonStyle.primary
self.current_page -= 1
if self.current_page < 0:
self.current_page = len(self.embeds)-1
await interaction.response.edit_message(embed=self.embeds[self.current_page])
@discord.ui.button(emoji="\U000023F9", custom_id="stop_embeds", style=discord.ButtonStyle.primary)
async def stop_embs(self, interaction: discord.Interaction, button: discord.ui.Button):
for item in self.children:
if item != button:
item.style = discord.ButtonStyle.grey
item.disabled = True
self.stop()
await interaction.response.edit_message(view=self)
await interaction.message.delete(delay=15)
@discord.ui.button(emoji='\U000027A1', custom_id="page_next", style=discord.ButtonStyle.primary)
async def next_page(self, interaction: discord.Interaction, button: discord.Button):
button.style = discord.ButtonStyle.secondary if button.style == discord.ButtonStyle.primary else discord.ButtonStyle.primary
self.current_page += 1
if self.current_page+1 > len(self.embeds):
self.current_page = 0
await interaction.response.edit_message(embed=self.embeds[self.current_page])
class PlayerButtons(discord.ui.View):
def __init__(self, player, timeout=None):
super(PlayerButtons, self).__init__(timeout=timeout)
self.player = player
async def clean_up(self):
vc = self.player
with suppress(AttributeError):
view = discord.ui.View.from_message(vc.controller)
if view:
for item in view.children:
item.style = discord.ButtonStyle.secondary
item.disabled = True
view.stop()
with suppress(discord.errors.NotFound):
await vc.controller.edit(view=view)
vc.current.extras.skips.clear()
await vc.skip(force=True)
@classmethod
async def renew(cls, player):
return cls(player=player)
async def on_error(self, interaction: discord.Interaction, error, item):
print(item.custom_id)
print(error.__class__.__name__, ":", str(error))
item.label = "Error!"
item.style = discord.ButtonStyle.danger
await interaction.response.edit_message(view=self)
@discord.ui.button(emoji='\U000023F8', custom_id='pause_button', style=discord.ButtonStyle.success)
async def interactive_play_pause(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.player.paused:
await self.player.pause(False)
button.emoji = "\U000023F8"
button.style = discord.ButtonStyle.success
await interaction.response.edit_message(view=self)
await interaction.channel.send(f":arrow_forward: Resumed by {interaction.user}!")
else:
await self.player.pause(True)
button.emoji = "\U000025B6"
button.style = discord.ButtonStyle.primary
await interaction.response.edit_message(view=self)
await interaction.channel.send(f":pause_button: Paused by {interaction.user}!")
@discord.ui.button(emoji='\U000023ED', custom_id='skip_button', style=discord.ButtonStyle.success)
async def interactive_skip(self, interaction: discord.Interaction, button: discord.ui.Button):
track = self.player.current
if str(interaction.user.id) in track.extras.skips:
return await interaction.response.send_message(":warning: You have already voted!", ephemeral=True)
else:
track.extras.skips.append(str(interaction.user.id))
skip_msg = f":track_next: {interaction.user} voted to skip the song!"
required = round((len(self.player.channel.members) - 1) / 3)
current = len(track.extras.skips)
if required:
button.label = f"{current}/{required}"
await interaction.response.edit_message(view=self)
if current < required:
await interaction.channel.send(
f"{skip_msg} Need {required-current} more to skip."
)
else:
await interaction.channel.send(f"{skip_msg} Skipping...")
await self.clean_up()
@discord.ui.button(emoji="\U000023EA", custom_id='repeat_button', style=discord.ButtonStyle.success)
async def interactive_repeat(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.player.seek()
button.style = discord.ButtonStyle.success if button.style == discord.ButtonStyle.primary else discord.ButtonStyle.primary
await interaction.response.edit_message(view=self)
await interaction.channel.send(f":rewind: Restarted current song by {interaction.user}")
# @discord.ui.button(emoji="\U0001F502", custom_id="loop_button", style=discord.ButtonStyle.success)
# async def interactive_loop(self, interaction: discord.Interaction, button: discord.ui.Button):
# self, player.loop = self.player.current if self.player.loop is None else None
# if self.player.loop:
# state = "turned on"
# button.style = discord.ButtonStyle.primary
# else:
# state = "turned off"
# button.style = discord.ButtonStyle.success
# await interaction.response.edit_message(view=self)
# await interaction.channel.send(f":repeat_one: Auto replay {state} by {interaction.user}")
@discord.ui.button(emoji='\U0001F3B6', custom_id='queue_button', style=discord.ButtonStyle.success)
async def interactive_queue(self, interaction: discord.Interaction, button):
button.style = discord.ButtonStyle.primary if button.style==discord.ButtonStyle.success else discord.ButtonStyle.success
await interaction.response.edit_message(view=self)
if not self.player.queue:
duration = formatted_time(self.player.current.length)
emb = discord.Embed(
title="Playlist :notes:",
description=f"**Now Playing**\n\n1| {self.player.current.title} ({duration})",
color=discord.Color.random(),
timestamp=discord.utils.utcnow()
)
emb.set_footer(text=self.player.client.user.name)
await interaction.channel.send(embed=emb)
else:
pager = discord.ext.commands.Paginator(suffix=None, prefix=None, max_size=1980, linesep='\n')
duration =formatted_time(self.player.current.length)
current_song = f"**Now Playing**\n1| {self.player.current.title} ({duration})\n\n**Up Next:**\n"
pager.add_line(current_song)
for x, track in enumerate(self.player.queue): pager.add_line(f"{x+2}| {track.title}")
embeds = [
discord.Embed(
title="Playlist :notes:",
description=page,
color=discord.Color.random(),
timestamp=discord.utils.utcnow()
).set_footer(text=self.player.client.user.name) for page in pager.pages]
await interaction.channel.send(
embed=embeds[0],
view=PageButtons(
embeds=embeds
) if len(embeds) > 1 else discord.utils.MISSING
)
@discord.ui.button(emoji="\U0001F3B5", custom_id="nowplay_button", style=discord.ButtonStyle.success)
async def interactive_np(self, interaction: discord.Interaction, button: discord.ui.Button):
pointer_pos = round((self.player.position/self.player.current.length)*30)
timeline = "".join("▬" if x not in (0, pointer_pos) else "🔘" for x in range(1, 31))
current_pos = formatted_time(int(self.player.position))
totaltime = formatted_time(int(self.player.current.length))
np_embed = discord.Embed(
title="Now Playing :musical_note:",
description=f"Title: **[{self.player.current.title}]({self.player.current.uri})**",
color=discord.Color.random(),
timestamp=discord.utils.utcnow()
)
looping = self.player.queue.mode == QueueMode.loop
np_fields = [
{
"name": "Author",
"value": self.player.current.author,
"inline": True
},
# {
# "name": "Requested by",
# "value": ctx.voice_client.current.requester,
# "inline": True
# },
{
"name": "Volume",
"value": f"{self.player.volume}%",
"inline": True
},
{
"name": "Loop",
"value": "Enabled" if looping else "Disabled",
},
{
"name": "Timeline Position",
"value": f"**{current_pos}** `{timeline}` **{totaltime}**",
"inline": False
},
]
for item in np_fields:
np_embed.add_field(**item)
np_embed.set_thumbnail(url=self.player.current.artwork)
np_embed.set_footer(text=self.player.client.user.name)
for item in self.children:
item.disabled = True
if item!=button:
item.style = discord.ButtonStyle.gray
new_view = await self.renew(player=self.player)
with suppress(discord.NotFound):
await interaction.response.edit_message(view=self)
self.player.controller = await interaction.channel.send(
embed=np_embed, view=new_view
)