-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
783 lines (656 loc) · 36.3 KB
/
bot.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
"""
copyright (c) 2021 pritam20ps05(Pritam Das)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
"""
import discord
import asyncio
import re
from random import shuffle
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from yt_dlp import YoutubeDL, utils
from lyrics_extractor import SongLyrics, LyricScraperException
from NeodiumUtils import *
getCookieFile()
activity = discord.Activity(type=discord.ActivityType.listening, name="-help")
client = commands.Bot(command_prefix='-', activity=activity, help_command=NeodiumHelpCommand())
lyrics_api = SongLyrics(search_token, search_engine)
spotify_api = SpotifyClient()
yt_dl_instance = YTdownload(client)
in_dl_instance = INSdownload(client)
private_instance = private_login('login.json')
paginator = Paginator(client)
player = {}
masters = {}
queues = {}
queuelocks = {}
def initGuilds():
for guild in client.guilds:
player[guild.id] = {}
queues[guild.id] = []
queuelocks[guild.id] = {}
queuelocks[guild.id]["lock"] = False
class QueueLockCheckFailure(commands.CheckFailure):
def __init__(self, message=None):
super().__init__(message)
def checkQueueLock(hard=False, check_if_bot_connected=False):
async def predicate(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice or not check_if_bot_connected:
if ctx.guild.id in queuelocks.keys() and queuelocks[ctx.guild.id]["lock"] and queuelocks[ctx.guild.id]["author"].voice and queuelocks[ctx.guild.id]["author"].voice.channel == voice.channel and not (not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []):
if queuelocks[ctx.guild.id]["author"] == ctx.message.author and not hard:
return True
else:
raise QueueLockCheckFailure("The queue is currently locked")
else:
queuelocks[ctx.guild.id] = {}
queuelocks[ctx.guild.id]["lock"] = False
return True
else:
raise QueueLockCheckFailure("I am currently not connected to any voice channel")
return commands.check(predicate)
# main queue manager function to play music in queue
async def check_queue(id, voice, ctx, msg=None):
while True:
while voice.is_playing() or voice.is_paused():
await asyncio.sleep(5)
if msg:
await msg.delete()
if queues[id] != [] and not (voice.is_playing() or voice.is_paused()):
current = queues[id].pop(0)
player[ctx.guild.id] = current
embed=discord.Embed(title="Currently Playing", description=f'[{player[id]["title"]}]({player[id]["url"]})', color=0xfe4b81)
embed.set_thumbnail(url=player[id]["thumbnails"][len(player[id]["thumbnails"])-1]["url"])
voice.play(FFmpegPCMAudio(player[id]["link"], **FFMPEG_OPTIONS))
msg = await ctx.send(embed=embed)
await asyncio.sleep(1)
# if anyhow system fails to play the audio it tries to play it again
while not(voice.is_playing() or voice.is_paused()):
info = await ydl_async(player[id]["url"], YDL_OPTIONS, False)
player[id]["link"] = info['url']
voice.play(FFmpegPCMAudio(player[id]["link"], **FFMPEG_OPTIONS))
await asyncio.sleep(1)
else:
player[ctx.guild.id] = {}
break
# a asyncronus function to get video details because normally some timeout issue occurs but this is slower
async def addsongs(entries, ctx):
for i, song in enumerate(entries):
url = song["url"]
try:
if i == 0:
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
else:
info = await ydl_async(url, YDL_OPTIONS, False)
data = {
"link": info['url'],
"url": url,
"title": info['title'],
"thumbnails": info["thumbnails"]
}
queues[ctx.guild.id].append(data)
except Exception as e:
print(e)
embed=discord.Embed(title="Playlist items were added to queue", color=0xfe4b81)
await ctx.send(embed=embed)
@client.event
async def on_ready():
print('Bot online')
initGuilds()
class BasicCommands(commands.Cog, name="Basic", description="This category of commands contains the basic functionalities of the bot such as joining a VC."):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.command(help='Makes the bot join the channel of the user, if the bot has already joined and is in a different channel it will move to the channel the user is in. Only if you are not trying to disturb someone')
async def join(self, ctx):
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
voice = get(self.bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
if not masters[ctx.guild.id].voice or masters[ctx.guild.id].voice.channel != voice.channel or (not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []):
await voice.move_to(channel)
masters[ctx.guild.id] = ctx.message.author
else:
embed=discord.Embed(title="I am currently under use in your server", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
else:
voice = await channel.connect()
queues[ctx.guild.id] = []
masters[ctx.guild.id] = ctx.message.author
player[ctx.guild.id] = {}
else:
embed=discord.Embed(title="You are currently not connected to any voice channel", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
@commands.command(help='Makes the bot leave the voice channel')
async def leave(self, ctx):
voice_client = get(self.bot.voice_clients, guild=ctx.guild)
if voice_client:
if not masters[ctx.guild.id].voice or masters[ctx.guild.id].voice.channel != voice_client.channel or (not (voice_client.is_playing() or voice_client.is_paused()) and queues[ctx.guild.id] == []):
if voice_client.is_playing():
voice_client.stop()
player[ctx.guild.id] = {}
await voice_client.disconnect()
else:
embed=discord.Embed(title="You can't disturb anyone listening to a song", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
else:
embed=discord.Embed(title="I am currently not connected to a voice channel.", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=7)
class PlayerCommands(commands.Cog, name="Player", description="This category of commands contains the playable functionalities of the bot. All of them can make bot join vc, play audio and queue audio."):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.command(aliases=['s'], help='Searches a query on YT and gives 5 results to choose from. Choosen one will be queued or played')
async def search(self, ctx, *, query):
voice = get(self.bot.voice_clients, guild=ctx.guild)
opts = {
"format": "bestaudio",
"quiet": True,
"noplaylist": True,
"skip_download": True,
'forcetitle': True,
'forceurl': True,
'source_address': '0.0.0.0',
"cookiefile": "yt_cookies.txt"
}
songs = await ydl_async(f'ytsearch5:{query}', opts, False)
videos = songs["entries"]
try:
out = ''
for i, song in enumerate(videos):
out = f'{out}{i+1}. [{song["title"]}]({song["webpage_url"]})\n'
embed=discord.Embed(title="Search results", description=out, color=0xfe4b81)
user_choice = await paginator.handleOptions(embed=embed, nops=len(videos), ctx=ctx)
info = videos[user_choice]
if voice:
if not masters[ctx.guild.id].voice or masters[ctx.guild.id].voice.channel != voice.channel or (not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []):
masters[ctx.guild.id] = ctx.message.author
# check if the bot is already playing
if not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []:
data = {
"link": info['url'],
"url": info['webpage_url'],
"title": info['title'],
"thumbnails": info["thumbnails"]
}
queues[ctx.guild.id].append(data)
await check_queue(ctx.guild.id, voice, ctx)
else:
data = {
"link": info['url'],
"url": info['webpage_url'],
"title": info['title'],
"thumbnails": info["thumbnails"]
}
queues[ctx.guild.id].append(data)
embed=discord.Embed(title="Item queued", description=f'[{info["title"]}]({data["url"]})', color=0xfe4b81)
embed.set_thumbnail(url=info["thumbnails"][len(info["thumbnails"])-1]["url"])
await ctx.send(embed=embed)
else:
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
voice = await channel.connect()
masters[ctx.guild.id] = ctx.message.author
queues[ctx.guild.id] = []
player[ctx.guild.id] = {}
data = {
"link": info['url'],
"url": info['webpage_url'],
"title": info['title'],
"thumbnails": info["thumbnails"]
}
queues[ctx.guild.id].append(data)
await check_queue(ctx.guild.id, voice, ctx)
else:
embed=discord.Embed(title="You are currently not connected to any voice channel", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
except asyncio.TimeoutError:
pass
except Exception as e:
embed=discord.Embed(title="can't play the requested audio", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
raise e
@commands.command(aliases=['p'], help='Searches a query on YT and plays or queues the most relevant result')
async def play(self, ctx, *, query):
voice = get(self.bot.voice_clients, guild=ctx.guild)
try:
info = await ydl_async(f'ytsearch:{query}', YDL_OPTIONS, False)
info = info['entries'][0]
url = info['webpage_url']
if voice:
if not masters[ctx.guild.id].voice or masters[ctx.guild.id].voice.channel != voice.channel or (not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []):
masters[ctx.guild.id] = ctx.message.author
# check if the bot is already playing
if not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []:
data = {
"link": info['url'],
"url": url,
"title": info['title'],
"thumbnails": info["thumbnails"]
}
queues[ctx.guild.id].append(data)
await check_queue(ctx.guild.id, voice, ctx)
else:
data = {
"link": info['url'],
"url": url,
"title": info['title'],
"thumbnails": info["thumbnails"]
}
queues[ctx.guild.id].append(data)
embed=discord.Embed(title="Item queued", description=f'[{info["title"]}]({url})', color=0xfe4b81)
embed.set_thumbnail(url=info["thumbnails"][len(info["thumbnails"])-1]["url"])
await ctx.send(embed=embed)
else:
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
voice = await channel.connect()
masters[ctx.guild.id] = ctx.message.author
queues[ctx.guild.id] = []
player[ctx.guild.id] = {}
data = {
"link": info['url'],
"url": url,
"title": info['title'],
"thumbnails": info["thumbnails"]
}
queues[ctx.guild.id].append(data)
await check_queue(ctx.guild.id, voice, ctx)
else:
embed=discord.Embed(title="You are currently not connected to any voice channel", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
except Exception as e:
embed=discord.Embed(title="can't play the requested audio", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
raise e
@commands.command(aliases=['l'], help='Plays a YT live from the URL provided as input')
async def live(self, ctx, url=None):
opts = {
'format': 'bestaudio/best',
'noplaylist': True,
'source_address': '0.0.0.0',
"cookiefile": "yt_cookies.txt"
}
voice = get(self.bot.voice_clients, guild=ctx.guild)
info = await ydl_async(url, opts, False)
if not info.get('is_live'):
embed=discord.Embed(title="The link is not of a live video", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
return
if voice:
if not masters[ctx.guild.id].voice or masters[ctx.guild.id].voice.channel != voice.channel or (not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []):
masters[ctx.guild.id] = ctx.message.author
if not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []:
voice.play(FFmpegPCMAudio(info["url"], **FFMPEG_OPTIONS))
embed=discord.Embed(title="Currently Playing (LIVE)", description=f'[{info["title"]}]({info["webpage_url"]})', color=0xfe4b81)
embed.set_thumbnail(url=info["thumbnails"][len(info["thumbnails"])-1]["url"])
msg = await ctx.send(embed=embed)
await check_queue(ctx.guild.id, voice, ctx, msg)
else:
embed=discord.Embed(title="Lives can't be queued", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
else:
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
voice = await channel.connect()
masters[ctx.guild.id] = ctx.message.author
queues[ctx.guild.id] = []
player[ctx.guild.id] = {}
voice.play(FFmpegPCMAudio(info["url"], **FFMPEG_OPTIONS))
embed=discord.Embed(title="Currently Playing (LIVE)", description=f'[{info["title"]}]({info["webpage_url"]})', color=0xfe4b81)
embed.set_thumbnail(url=info["thumbnails"][len(info["thumbnails"])-1]["url"])
msg = await ctx.send(embed=embed)
await check_queue(ctx.guild.id, voice, ctx, msg)
else:
embed=discord.Embed(title="You are currently not connected to any voice channel", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
@commands.command(name="add-playlist", help='Adds a whole YT or Spotify playlist to queue and starts playing it. Input is taken as the URL to the public or unlisted playlist. You can also mention a starting point or an ending point of the playlist or both')
async def addPlaylist(self, ctx, url: str, sp: int = None, ep: int = None):
voice = get(self.bot.voice_clients, guild=ctx.guild)
source = None
if 'youtube' in url or 'youtu.be' in url:
p_id = re.search(r'^.*?(?:list)=(.*?)(?:&|$)', url)
if p_id:
link = "https://www.youtube.com/playlist?list=" + p_id.groups()[0]
source = 'youtube'
elif 'spotify' in url:
source = 'spotify'
spotify_tracks = await spotify_api.getPlaylist(url, ctx, sp, ep)
if not source:
embed=discord.Embed(title="Invalid link", color=0xfe4b81)
await ctx.send(embed=embed)
return
try:
if source == 'youtube':
opts = {
"extract_flat": True,
"source_address": "0.0.0.0",
"cookiefile": "yt_cookies.txt"
}
info = await ydl_async(link, opts, False)
if info.get('_type') != 'playlist':
embed=discord.Embed(title="The link is not of a playlist", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
return
info["entries"] = info["entries"][sp:ep]
if sp or ep:
if ep:
embed=discord.Embed(title="Adding Playlist", description=f'[{info["title"]}]({link})\n\n**From {sp+1} to {ep}**', color=0xfe4b81)
else:
embed=discord.Embed(title="Adding Playlist", description=f'[{info["title"]}]({link})\n\n**From {sp+1} to {len(info["entries"])+sp}**', color=0xfe4b81)
else:
embed=discord.Embed(title="Adding Playlist", description=f'[{info["title"]}]({link})', color=0xfe4b81)
if voice:
if not masters[ctx.guild.id].voice or masters[ctx.guild.id].voice.channel != voice.channel or (not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []):
masters[ctx.guild.id] = ctx.message.author
# check if the bot is already playing
if not (voice.is_playing() or voice.is_paused()) and queues[ctx.guild.id] == []:
if source == 'youtube':
await ctx.send(embed=embed)
coros = []
coros.append(addsongs(info["entries"], ctx))
coros.append(check_queue(ctx.guild.id, voice, ctx))
asyncio.gather(*coros)
elif source == 'spotify':
coros = []
coros.append(spotify_api.addSongs(spotify_tracks, queues[ctx.guild.id], ctx))
coros.append(check_queue(ctx.guild.id, voice, ctx))
asyncio.gather(*coros)
else:
if source == 'youtube':
await ctx.send(embed=embed)
await addsongs(info["entries"], ctx)
elif source == 'spotify':
await spotify_api.addSongs(spotify_tracks, queues[ctx.guild.id], ctx)
else:
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
voice = await channel.connect()
masters[ctx.guild.id] = ctx.message.author
queues[ctx.guild.id] = []
player[ctx.guild.id] = {}
if source == 'youtube':
await ctx.send(embed=embed)
coros = []
coros.append(addsongs(info["entries"], ctx))
coros.append(check_queue(ctx.guild.id, voice, ctx))
asyncio.gather(*coros)
elif source == 'spotify':
coros = []
coros.append(spotify_api.addSongs(spotify_tracks, queues[ctx.guild.id], ctx))
coros.append(check_queue(ctx.guild.id, voice, ctx))
asyncio.gather(*coros)
else:
embed=discord.Embed(title="You are currently not connected to any voice channel", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
except utils.ExtractorError as e:
if "ERROR: The playlist does not exist." in e:
embed=discord.Embed(title="Such a playlist does not exist", color=0xfe4b81)
else:
embed=discord.Embed(title="can't queue the requested playlist", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
except RuntimeError as e:
print(e)
except Exception as e:
embed=discord.Embed(title="can't queue the requested playlist", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
raise e
class VisualizerCommands(commands.Cog, name="Visualizer", description="This category of commands contains the visualizers which enables you to monitor some states of the bot or get some kind of info about something."):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.command(name="queue", help='Displays the current queue. Limit is the number of entries shown per page, default is 10')
async def listQueue(self, ctx, limit=10):
out = ""
pages = []
npages = 1
voice = get(self.bot.voice_clients, guild=ctx.guild)
if voice and not queues[ctx.guild.id] == []:
if len(queues[ctx.guild.id])%limit == 0 and len(queues[ctx.guild.id]) != 0:
npages = int(len(queues[ctx.guild.id])/limit)
else:
npages = int(len(queues[ctx.guild.id])/limit) + 1
i = 0
p = 1
for j, song in enumerate(queues[ctx.guild.id]):
if i < limit:
out = out + str(j+1) + f'. [{song["title"]}]({song["url"]})\n'
i = i + 1
else:
out = out + f'\n**Page {p}/{npages}**'
embed=discord.Embed(title="Currently in queue", description=out, color=0xfe4b81)
pages.append(embed)
out = str(j+1) + f'. [{song["title"]}]({song["url"]})\n'
i = 1
p = p + 1
out = out + f'\n**Page {p}/{npages}**'
embed=discord.Embed(title="Currently in queue", description=out, color=0xfe4b81)
pages.append(embed)
await paginator.handlePages(pages, ctx)
else:
out = "None"
embed=discord.Embed(title="Currently in queue", description=out, color=0xfe4b81)
await ctx.send(embed=embed)
@commands.command(help='Displays the lyrics of the current song if available')
async def lyrics(self, ctx, index=0):
out = ""
if player[ctx.guild.id]:
try:
lyric = lyrics_api.get_lyrics(player[ctx.guild.id]['title'])['lyrics']
except LyricScraperException as e:
try:
if int(e.args[0]["error"]["code"]) == 429:
lyric = "Daily quota exceeded"
else:
lyric = "Something went wrong"
print(e.args[0]["error"])
except:
lyric = "Something went wrong"
print(e)
out = f'**{player[ctx.guild.id]["title"]}**\n\n{lyric}'
if len(lyric) > 50:
out = f'{out}\n\n**Lyrics provided by [genius.com](https://genius.com/)**'
embed=discord.Embed(title="Lyrics", description=out, color=0xfe4b81)
else:
embed=discord.Embed(title="Nothing currently in the player", color=0xfe4b81)
await ctx.send(embed=embed)
@commands.command(name='current', aliases=['c'], help='Displays information about the current song in the player')
async def currentlyPlaying(self, ctx):
if player[ctx.guild.id]:
embed=discord.Embed(title="Currently in the Player", description=f'[{player[ctx.guild.id]["title"]}]({player[ctx.guild.id]["url"]})', color=0xfe4b81)
embed.set_thumbnail(url=player[ctx.guild.id]["thumbnails"][len(player[ctx.guild.id]["thumbnails"])-1]["url"])
else:
embed=discord.Embed(title="Nothing currently in the player", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
class QueueCommands(commands.Cog, name="Queue", description="This category of commands contains the commands related to queues. Also there is a concept of queue lock which will dissable any user from using these commands except the user initiating the lock with some more exceptions."):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.command(name="remove", help='Removes an mentioned entry from the queue')
@checkQueueLock(check_if_bot_connected=True)
async def removeQueueSong(self, ctx, index: int):
if (index<=len(queues[ctx.guild.id]) and index>0):
rem = queues[ctx.guild.id].pop(index-1)
embed=discord.Embed(title="Removed from queue", description=f'[{rem["title"]}]({rem["url"]})', color=0xfe4b81)
else:
embed=discord.Embed(title="Invalid request", color=0xfe4b81)
await ctx.send(embed=embed)
@commands.command(help='Pauses the current player')
@checkQueueLock(check_if_bot_connected=True)
async def pause(self, ctx):
voice = get(self.bot.voice_clients, guild=ctx.guild)
embed=discord.Embed(title="Pausing...", color=0xfe4b81)
if voice.is_playing():
voice.pause()
await ctx.send(embed=embed, delete_after=7)
@commands.command(help='Resumes the paused player')
@checkQueueLock(check_if_bot_connected=True)
async def resume(self, ctx):
voice = get(self.bot.voice_clients, guild=ctx.guild)
embed=discord.Embed(title="Resuming...", color=0xfe4b81)
if not voice.is_playing():
voice.resume()
await ctx.send(embed=embed, delete_after=7)
@commands.command(help='Skips current audio')
@checkQueueLock(check_if_bot_connected=True)
async def skip(self, ctx):
voice = get(self.bot.voice_clients, guild=ctx.guild)
embed=discord.Embed(title="Skipping...", color=0xfe4b81)
if voice.is_playing():
voice.stop()
await ctx.send(embed=embed, delete_after=7)
@commands.command(help='Just like skip but also clears the queue')
@checkQueueLock(hard=True, check_if_bot_connected=True)
async def stop(self, ctx):
voice = get(self.bot.voice_clients, guild=ctx.guild)
embed=discord.Embed(title="Stopping...", color=0xfe4b81)
queues[ctx.guild.id] = []
if voice.is_playing():
voice.stop()
await ctx.send(embed=embed, delete_after=7)
@commands.command(name="clear-queue", aliases=["clear"], help='Clears the queue')
@checkQueueLock(hard=True, check_if_bot_connected=True)
async def clearQueue(self, ctx):
embed=discord.Embed(title="Do you really want to clear the queue", color=0xfe4b81)
success_embed=discord.Embed(title="The queue has been cleared", color=0xfe4b81)
user_decision = await paginator.handleDecision(embed=embed, resp_embed=success_embed, ctx=ctx)
if user_decision:
queues[ctx.guild.id] = []
@commands.command(name="shuffle", help='Shuffles the whole queue')
@checkQueueLock(check_if_bot_connected=True)
async def shuffleQueue(self, ctx):
embed=discord.Embed(title="Do you really want to shuffle the queue", color=0xfe4b81)
success_embed=discord.Embed(title="The queue has been shuffled", color=0xfe4b81)
user_decision = await paginator.handleDecision(embed=embed, resp_embed=success_embed, ctx=ctx)
if user_decision:
shuffle(queues[ctx.guild.id])
@commands.command(help='Locks the queue and prevents anyone from damaging anyone\'s experience')
async def lock(self, ctx):
voice_client = get(self.bot.voice_clients, guild=ctx.guild)
if ctx.message.author.voice:
if voice_client:
if ctx.guild.id in queuelocks.keys() and queuelocks[ctx.guild.id]["lock"] and queuelocks[ctx.guild.id]["author"].voice :
if queuelocks[ctx.guild.id]["author"] == ctx.message.author or (not (voice_client.is_playing() or voice_client.is_paused()) and queues[ctx.guild.id] == []):
queuelocks[ctx.guild.id]["lock"] = False
embed=discord.Embed(title="Queue lock has been removed", color=0xfe4b81)
await ctx.send(embed=embed)
else:
embed=discord.Embed(title=f'{queuelocks[ctx.guild.id]["author"].display_name} has already locked the queue', color=0xfe4b81)
await ctx.send(embed=embed)
else:
queuelocks[ctx.guild.id] = {}
queuelocks[ctx.guild.id]["lock"] = True
queuelocks[ctx.guild.id]["author"] = ctx.message.author
embed=discord.Embed(title=f'{queuelocks[ctx.guild.id]["author"].display_name} has initiated queuelock', color=0xfe4b81)
await ctx.send(embed=embed)
else:
embed=discord.Embed(title="I am currently not connected to any voice channel", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
else:
embed=discord.Embed(title="You are currently not connected to any voice channel", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
class DownloadCommands(commands.Cog, name="Download", description="This category of commands contains recently added download feature which can download YT and instagram audio video files with private support for instagram only."):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.command(name='download', aliases=['d'], help='Downloads YT or instagram audio video files from the url. If the bot is already playing something then passing no input will result in selecting that video. Copt can be passed for choosing vcodec, default is 0 for h264 but can be set to 1 for codec provided by vendor')
@commands.max_concurrency(number=1, per=commands.BucketType.default, wait=False)
async def dl_yt(self, ctx, url: str = None, copt: int = 0):
def check_url(url: str):
if url:
uw = url.split("://")
if uw[0] == 'https' or uw[0] == 'http':
uweb = uw[1].split('/')[0]
if 'youtube' in uweb or 'youtu.be' in uweb:
return 1
elif 'instagram' in uweb:
return 2
else:
return 0
else:
return 0
else:
return 0
if not url and player[ctx.guild.id] != {}:
url = player[ctx.guild.id]['url']
url_type: int = check_url(url)
if url_type == 1:
await yt_dl_instance.downloadVideo(ctx, url, copt)
elif url_type == 2:
usrcreds = private_instance.get_usercreds(ctx.author.id)
await in_dl_instance.downloadVideo(ctx, url, copt, usrcreds)
else:
embed=discord.Embed(title='The link is invalid, can\'t fetch data', color=0xfe4b81)
await ctx.send(embed=embed, delete_after=15)
@commands.command(help='Supports the instagram private feature. This command Logs in to your inastagram account and uses it to access files through your account. Once logged in use the download command normally. This command can only be used in DMs in order to protect your privacy')
async def login(self, ctx, usrn=None, passw=None):
if isinstance(ctx.channel, discord.DMChannel):
if usrn and passw:
await private_instance.login(ctx, usrn, passw)
else:
embed=discord.Embed(title='Hey use this command here', description='Login command can only be used from the DM. This helps us keep your credentials private.', color=0xfe4b81)
await ctx.author.send(embed=embed, delete_after=30)
@commands.command(help='Logout of your account only if you are already logged in')
async def logout(self, ctx):
if private_instance.is_user_authenticated(ctx.author.id):
embed=discord.Embed(title="Do you really want to logout", color=0xfe4b81)
user_decision = await paginator.handleDecision(embed=embed, resp_embed=embed, ctx=ctx)
if user_decision:
await private_instance.logout(ctx)
class SpecialCommands(commands.Cog, name="Special", description="This category of commands contains the special commands which can only be accessed by the owner of the bot. These commands enables the owner to remotely invoke methods for temporary fixes or other debugging stuff."):
def __init__(self, bot: commands.Bot):
self.bot = bot
async def cog_check(self, ctx):
if not await ctx.bot.is_owner(ctx.author):
raise commands.NotOwner('You do not own this bot.')
return True
@commands.command(help='Refetches the default cookie files')
async def refetch(self, ctx, id_insta=None, id_yt=None):
getCookieFile(id_insta, id_yt)
embed=discord.Embed(title="Default cookies were refetched and refreshed successfully", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=20)
@commands.command(name="add-cog", help='Adds a predefined cog to the bot')
async def addCog(self, ctx, cog_name):
if cog_name != 'Special':
for cog in cog_list:
if cog.qualified_name == cog_name and not self.bot.get_cog(cog_name):
self.bot.add_cog(cog)
embed=discord.Embed(title=f"{cog_name} cog was added successfully", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=20)
@commands.command(name="remove-cog", help='Removes a already existing cog. Generally used to disable a functionality of the bot')
async def removeCog(self, ctx, cog_name):
if cog_name != 'Special':
self.bot.remove_cog(cog_name)
embed=discord.Embed(title=f"{cog_name} cog was removed successfully", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=20)
cog_list = []
cog_list.append(BasicCommands(client))
cog_list.append(PlayerCommands(client))
cog_list.append(VisualizerCommands(client))
cog_list.append(QueueCommands(client))
cog_list.append(DownloadCommands(client))
cog_list.append(SpecialCommands(client))
for cog in cog_list:
client.add_cog(cog)
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MaxConcurrencyReached):
embed=discord.Embed(title="Please wait..", description="Someone is currently using this feature please wait before trying again. This restriction has been implemented to prevent throttling in cases of high processing load.", color=0xfe4b81)
await ctx.send(embed=embed, delete_after=20)
elif isinstance(error, commands.NotOwner):
embed=discord.Embed(title="Access Denied", description=f"It is a special command and is reserved to the owner of the bot only. This types of commands enables the owner to remotely triggure some functions for ease of use. Read more about them from `{ctx.prefix}help Special`.", color=0xfe4b81)
await ctx.reply(embed=embed, delete_after=20)
elif isinstance(error, QueueLockCheckFailure):
embed=discord.Embed(title=error, color=0xfe4b81)
await ctx.send(embed=embed, delete_after=10)
elif isinstance(error, commands.CommandNotFound):
print(error)
elif isinstance(error, commands.errors.MissingRequiredArgument):
print(error)
else:
raise error
client.run(token)