This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
1038 lines (886 loc) · 36.2 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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json
import logging
import random
import re
import textwrap
import threading
from sys import argv
import discord
from config import Config
from opus_loader import load_opus_libs
lock = asyncio.Lock()
class Music(discord.Client):
def __init__(self, config, player_queue, res_queue, ctrl_queue, loop):
super().__init__()
load_opus_libs()
self.config = config
self.player_queue = player_queue
self.res_queue = res_queue
self.ctrl_queue = ctrl_queue
self.loop = loop
self.voice = None
self.vc_members = 0
self.player_status = {
'state': False,
'source': None,
'title': None,
'album': None,
'artist': None,
'uri': None,
'is_liked': False,
'duration': None,
'position': None
}
self.play_queue = {}
self.nums = [
':one:',
':two:',
':three:',
':four:',
':five:',
':six:',
':seven:',
':eight:',
':nine:'
]
self.unicode_nums = [
'\U00000031\U000020E3',
'\U00000032\U000020E3',
'\U00000033\U000020E3',
'\U00000034\U000020E3',
'\U00000035\U000020E3',
'\U00000036\U000020E3',
'\U00000037\U000020E3',
'\U00000038\U000020E3',
'\U00000039\U000020E3'
]
self.inv_unicode_nums = {
'\U00000031\U000020E3': 1,
'\U00000032\U000020E3': 2,
'\U00000033\U000020E3': 3,
'\U00000034\U000020E3': 4,
'\U00000035\U000020E3': 5,
'\U00000036\U000020E3': 6,
'\U00000037\U000020E3': 7,
'\U00000038\U000020E3': 8,
'\U00000039\U000020E3': 9
}
self.control = {
'fast_forward': '\U000023E9',
'rewind': '\U000023EA',
'ok': '\U0001F197',
'no_entry_sign': '\U0001F6AB',
'white_check_mark': '\U00002705',
'play_all': '\U0001F35C'
}
async def on_ready(self):
logging.info('connected to discord')
#await asyncio.wait_for(self.join_vc(), timeout=5.0)
await self.join_vc()
await self.post('discord ready')
logging.info('discord vc connect')
response_handler = threading.Thread(target=self.handle_res, daemon=True)
self.vc_members = len(self.voice.channel.members)
self.loop.create_task(self._play())
response_handler.start()
logging.info('connected to vc')
async def safe_send(self, dest, payload, users=None):
'''
Parameters
----------
dest: discord.channel.TextChannel
payload: str
user: message.author
Returns
-------
msg
'''
try:
if users:
if not isinstance(users, (list, tuple)):
users = [users]
msg = await dest.send(f'{" ".join([user.mention for user in users])}\n{payload}')
else:
msg = await dest.send(payload)
except discord.Forbidden:
logging.error('You do not have the proper permissions to send the message.')
return None
except discord.NotFound:
logging.error('dest channel is not found')
return None
except discord.HTTPException:
logging.error('Sending the message failed.')
return None
return msg
async def safe_delete(self, message):
try:
return await message.delete()
except discord.Forbidden:
logging.error('cannot delete message: no permission')
except discord.NotFound:
logging.error('cannot delete message: message not found')
async def join_vc(self):
self.voice_channel = self.get_channel(self.config.voice_channel_id)
self.voice = await self.voice_channel.connect()
async def exit_vc(self):
await self.voice.disconnect()
self.voice = None
async def cmd_reconnect(self, message=None, dest=None, cmd_args=[]):
try:
await asyncio.wait_for(self.exit_vc(), timeout=0.2)
except TimeoutError:
logging.error('TimeoutError')
return
await self.join_vc()
async def on_voice_state_update(self, member, before, after):
if not self.voice:
return
if self.vc_members != len(self.voice.channel.members):
if self.vc_members < len(self.voice.channel.members):
await self.cmd_reconnect()
self.vc_members = len(self.voice.channel.members)
async def post(self, op, data=None, postback=None):
'''
post op and message
'''
async with lock:
self.ctrl_queue.put_nowait((op, data, postback))
async def set_game_activity(self):
'''
update game activity
'''
song = ''
if self.player_status.get('state') != 'playing':
song = u'\u275A\u275A'
if self.player_status.get('is_liked'):
song += u'\u2665 '
if self.player_status.get('title'):
song += f'{self.player_status.get("title")}'
if self.player_status.get('artist'):
song += f' / {self.player_status.get("artist")}'
playing_status = discord.Game(name=song)
await self.change_presence(activity=playing_status)
async def _play(self):
await lock.acquire()
if self.player_queue.empty():
lock.release()
await asyncio.sleep(0.001)
else:
if self.voice:
self.voice.send_audio_packet(self.player_queue.get_nowait(), encode=False)
lock.release()
self.loop.create_task(self._play())
def handle_res(self):
self.loop.create_task(self._handle_res())
async def _handle_res(self):
res = await self.res_queue.get()
response_type = res.get('type')
if response_type == 'search':
self.loop.create_task(self.search(res))
elif response_type == 'list_queue':
self.loop.create_task(self.set_play_queue(res))
elif response_type == 'state':
self.loop.create_task(self.show_np(res))
elif response_type == 'playlists':
self.loop.create_task(self.show_playlists(res))
elif response_type == 'playlist':
self.loop.create_task(self.show_likelen(res))
elif response_type == 'token':
self.loop.create_task(self.show_token(res))
elif response_type == 'invite':
self.loop.create_task(self.show_invite(res))
elif response_type == 'event_player_state_change':
self.loop.create_task(self.set_player_status(res))
elif response_type == 'event_queue_change':
self.loop.create_task(self.set_play_queue(res))
elif response_type == 'event_playlists_change':
pass
elif response_type == 'event_playlist_entry_change':
pass
else:
logging.warning('error: unexpected response type')
self.loop.create_task(self._handle_res())
async def set_player_status(self, res):
'''
update self.player_status
'''
data = res.get('data')
self.player_status['state'] = data.get('state')
entry = data.get('entry')
if entry:
self.player_status['source'] = entry.get('source')
self.player_status['title'] = entry.get('title')
self.player_status['uri'] = entry.get('uri')
self.player_status['is_liked'] = entry.get('is_liked')
self.player_status['duration'] = entry.get('duration')
self.player_status['position'] = entry.get('position')
if self.player_status['source'] == 'gpm':
self.player_status['title'] = entry.get('entry').get('title')
self.player_status['album'] = entry.get('entry').get('album')
self.player_status['artist'] = entry.get('entry').get('artist')
self.player_status['user'] = entry.get('entry').get('user')
else:
self.player_status['album'] = None
self.player_status['artist'] = None
else:
self.player_status['source'] = None
self.player_status['title'] = None
self.player_status['title'] = None
self.player_status['album'] = None
self.player_status['artist'] = None
self.player_status['user'] = None
self.player_status['uri'] = None
self.player_status['is_liked'] = False
self.player_status['duration'] = None
self.player_status['position'] = None
await self.set_game_activity()
if self.player_status['state'] == 'stopped':
await lock.acquire()
while not self.player_queue.empty():
self.player_queue.get_nowait()
lock.release()
async def set_play_queue(self, res):
'''
update self.player_queue
'''
self.play_queue = res
async def search(self, raw_result):
'''
search
Parameters
----------
raw_result: dict
'''
dest = self.get_channel(raw_result.get('postback') or self.config.text_channel_id)
results = await self.parse_result(raw_result)
if not results:
res_text = f'Sorry\ncould not find any track'
await self.safe_send(dest, res_text)
return
res_text = (f'**search result**\n'
f'**{len(results)}** hits')
await self.safe_send(dest, res_text)
res_count = self.config.serch_result_count
total_page = -(-len(results) // res_count)
result = results
to_play = []
async def _send():
if to_play:
if len(to_play) == 1:
await self.post('queue', {'uri': to_play[0]})
else:
await self.post('queue', {'uri': to_play})
for page in range(total_page):
res_text = f'page: {page+1} / {total_page}\n'
show = result[0:len(result) if len(result) < res_count else res_count]
result = result[len(show):]
res_text += await self.format_list(show, 'search')
#r = re.compile(key, re.IGNORECASE)
#res_text = re.sub(r, f'**{key}**', res_text)
msg = await self.safe_send(dest, res_text)
for i in range(len(show)):
self.loop.create_task(msg.add_reaction(self.unicode_nums[i]))
if page+1 != total_page:
self.loop.create_task(msg.add_reaction(self.control['fast_forward']))
for i in ['no_entry_sign', 'white_check_mark', 'play_all']:
self.loop.create_task(msg.add_reaction(self.control[i]))
def check(reaction, user):
return user != msg.author and reaction.message.id == msg.id
next_page = False
while True:
try:
reaction, _ = await self.wait_for('reaction_add', timeout=60.0, check=check)
if str(reaction.emoji) == self.control['fast_forward']: #go next page
await self.safe_delete(msg)
next_page = True
break
elif str(reaction.emoji) == self.control['no_entry_sign']: #cancel
await self.safe_delete(msg)
return
elif str(reaction.emoji) == self.control['white_check_mark']: #submit
await self.safe_delete(msg)
break
elif str(reaction.emoji) == self.control['play_all']: #play all
await self.safe_delete(msg)
to_play.clear()
to_play = [i.get('uri') for i in results]
break
select = self.inv_unicode_nums.get(str(reaction.emoji))
if select:
to_play.append(show[select-1].get('uri'))
except asyncio.TimeoutError:
await self.safe_delete(msg)
return
if next_page and page != total_page:
continue
else:
break
await _send()
async def show_np(self, res):
dest = self.get_channel(res.get('postback') or self.config.text_channel_id)
try:
await asyncio.wait_for(self.set_player_status(res), timeout=1.0)
res_text = ''
if self.player_status.get('state') == 'playing':
res_text = f':arrow_forward: **{self.player_status.get("title")}\n\n**'
else:
res_text = f':pause_button: **{self.player_status.get("title")}\n\n**'
if self.player_status.get('source') == 'gpm':
res_text += (f' album: **{self.player_status.get("album")}**\n'
f' artist: **{self.player_status.get("artist")}**\n'
' from: **gpm**\n'
f' owner: **{self.player_status.get("user")}**\n'
f' uri: {self.player_status.get("uri")}\n')
else:
res_text += (f' from: **{self.player_status.get("source")}**\n'
f' uri: <{self.player_status.get("uri")}>\n')
def _format_time (song_len):
return f'{int(song_len/60)}:{int(song_len%60)}'
res_text += f' position: {_format_time(self.player_status.get("position"))} / {_format_time(self.player_status.get("duration"))}'
await self.safe_send(dest, res_text)
except TimeoutError:
logging.error('TimeoutError')
return
async def parse_result(self, res):
'''
parse json
Parameters
----------
res: dict
Returns
-------
songs: list
'''
if not res.get('data'):
#0hit
return None
songs = []
# for entry in res.get('data').get('result') or res.get('data').get('queue'):
for entry in res.get('data'):
song = {}
song['source'] = entry.get('source')
song['title'] = entry.get('title')
if entry.get('entry'):
song['title'] = entry.get('entry').get('title')
song['artist'] = entry.get('entry').get('artist')
song['album'] = entry.get('entry').get('album')
song['user'] = entry.get('entry').get('user')
song['uri'] = entry.get('uri')
song['thumbnail'] = entry.get('thumbnail_small')
songs.append(song)
return songs
async def parse_queue(self, res):
'''
fetch play que
Paramters
---------
res: dict
Returns
-------
songs: list
'''
if not res.get('data'):
#0hit
return None
songs = []
for entry in res.get('data').get("queue"):
song = {}
song['source'] = entry.get('source')
song['title'] = entry.get('title')
if entry.get('entry'):
song['title'] = entry.get('entry').get('title')
song['artist'] = entry.get('entry').get('artist')
song['album'] = entry.get('entry').get('album')
song['user'] = entry.get('entry').get('user')
song['uri'] = entry.get('uri')
song['thumbnail'] = entry.get('thumbnail_small')
songs.append(song)
return songs
async def show_playlists(self, res):
_playlists = {}
dest = self.get_channel(res.get('postback') or self.config.text_channel_id)
for entry in res.get('data').get('playlists'):
_playlists[entry.get('name')] = entry.get('length')
res_text = ''
res_text = 'playlist\n\n'
for i in _playlists:
res_text += f':file_folder: **{i}** has **{_playlists[i]}** tracks :musical_note:\n'
res_text += '\nmore info use web client'
await self.safe_send(dest, res_text)
async def show_likelen(self, res):
dest = self.get_channel(res.get('postback') or self.config.text_channel_id)
lenlist = len(res.get('data').get('entries'))
await self.safe_send(dest, f'Likes has **{lenlist}** tracks :musical_note: \nmore info use web client')
async def show_token(self, res):
dest = self.get_channel(res.get('postback') or self.config.text_channel_id)
token = res.get('data').get('token')
msg_id = await self.safe_send(dest, f'Your TOKEN : **{token}**')
await asyncio.sleep(60)
await self.safe_delete(msg_id)
async def show_invite(self, res):
dest = self.get_channel(res.get('postback') or self.config.text_channel_id)
invite = res.get('data').get('invite')
msg_id = await self.safe_send(dest, f'Your invite link : https://aria.gaiji.pro/auth/github/register?invite={invite}')
await asyncio.sleep(60)
await self.safe_delete(msg_id)
async def gen_command_list(self):
self.commandlist = []
for attr in dir(self):
if attr.startswith('cmd_'):
self.commandlist.append(self.config.cmd_prefix+attr[4:])
async def format_list(self, orig_list, opr):
numberd_list = ''
if opr == 'queue':
if self.player_status.get('state') == 'playing':
numberd_list = f':arrow_forward: **{self.player_status.get("title")}**\n'
else:
numberd_list = f':pause_button: **{self.player_status.get("title")}**\n'
if self.player_status.get('source') == 'gpm':
numberd_list += f' {self.player_status.get("album")} / {self.player_status.get("artist")}\n\n'
else:
numberd_list += '\n'
numberd_list += f'**{len(orig_list)}** tracks in queue\n\n'
for num, song in zip(self.nums, orig_list):
if song['source'] == 'gpm':
numberd_list += (f'{num} **{song["title"]}**\n'
f' {song["album"]} / {song["artist"]}\n'
f' from: gpm - {song["user"]}\n')
else:
numberd_list += (f'{num} **{song["title"]}**\n'
f' - from: {song["source"]}\n')
return numberd_list
def op_only(func):
async def wrapper(self, *args, **kwargs):
#print(args[0].author.id)
if args[0].author.id in self.config.op:
return await func(self, *args, **kwargs)
else:
return await self.safe_send(args[0].channel ,':regional_indicator_f: :regional_indicator_u: :regional_indicator_c: :regional_indicator_k: :regional_indicator_y: :regional_indicator_o: :regional_indicator_u:', args[0].author.mention)
return wrapper
##########################
async def cmd_play(self, message, dest, *cmd_args):
'''
play song from URL
usage {prefix}play URL(s)
'''
if not cmd_args:
await self.safe_send(dest, 'error:anger:\nusage: `{prefix}play URL`'.format(prefix=self.config.cmd_prefix))
return
if len(cmd_args) == 1:
if cmd_args[0][0] == '<':
await self.post('queue', {'uri':cmd_args[0][1:-1]}, dest.id)
else:
await self.post('queue', {'uri':cmd_args[0]}, dest.id)
else:
await self.post('queue', {'uri':[i if i[0] != '<' else i[1:-1] for i in cmd_args]}, dest.id)
async def cmd_quelist(self, message, dest, *cmd_args):
'''
queue playlist
usage {prefix}quelist PlaylistName
'''
if not cmd_args:
await self.safe_send(dest, 'uhnnn fuckyou\nusage quelist PlaylistName')
return
playlist = ' '.join(cmd_args)
await self.post('queue', {'playlist': playlist}, dest.id)
async def cmd_repeat(self, message, dest, *cmd_args):
'''
repeat now-playing song
usage {prefix}repeat <num>
'''
if not cmd_args:
await self.post('repeat', {'uri': self.player_status.get('uri')}, dest.id)
return
try:
count = int(cmd_args[0])
await self.post('repeat', {'uri': self.player_status.get('uri'), 'count': count}, dest.id)
except ValueError:
await self.post('repeat', {'uri': self.player_status.get('uri')}, dest.id)
async def cmd_pause(self, message, dest, *cmd_args):
'''
pause
usage {prefix}pause
'''
if not self.player_status.get('state') == 'playing':
#pausing
await dest.send('error:anger:\nplayer is already paused')
return
else:
await self.post('pause', postback=dest.id)
async def cmd_resume(self, message, dest, *cmd_args):
'''
resume
usage {prefix}resume
'''
if self.player_status.get('state') == 'playing':
#playing
await dest.send('error:anger:\nplayer is already playing')
return
else:
await self.post('resume', postback=dest.id)
async def cmd_skip(self, message, dest, *cmd_args):
'''
skip
usage {prefix}skip
'''
if not cmd_args:
await self.post('skip', postback=dest.id)
return
await self.cmd_skip_to(message, dest, *cmd_args)
async def cmd_skip_to(self, message, dest, *cmd_args):
'''
skip to selected entry
usage {prefix}skip_to num
'''
if not cmd_args:
await self.safe_send(dest, ('error:anger:\n'
'If you want to skip this track, USE `{prefix}skip`\n'
'usage `{prefix}skip_to num`'.format(prefix=self.config.cmd_prefix)))
return
try:
index = int(cmd_args[0]) - 1
if index <= len(self.play_queue.get('data').get('queue')):
await self.post('skip_to', {'index': index, 'uri': self.play_queue.get('data').get('queue')[index].get('uri')}, dest.id)
else:
await self.safe_send(dest, f'{index+1} is out of queue range')
except ValueError:
await self.safe_send(dest, 'error:anger:\n usage `{prefix}skip_to num`'.format(prefix=self.config.cmd_prefix))
async def cmd_queue(self, message, dest, *cmd_args):
'''
show song list in queue
usage {prefix}queue
'''
result = await self.parse_queue(self.play_queue)
if not result:
res_text = f'No tracks in queue'
await self.safe_send(dest, res_text)
return
res_text = await self.format_list(result, 'queue')
await self.safe_send(dest, res_text)
async def cmd_playnext(self, message, dest, *cmd_args):
'''
play next
usage {prefix}playnext URL
'''
if len(cmd_args) != 1:
await dest.send('error:anger:\n usage `{prefix}playnext URL`'.format(prefix=self.config.cmd_prefix))
return
await self.post('queue', {'uri': cmd_args[0], 'head': True}, dest.id)
async def cmd_add(self, message, dest, *cmd_args):
'''
store auto playlist
usage {prefix}add URL(s)
'''
if not cmd_args:
await dest.send('error:anger:\n usage `{prefix}add URL(s)`'.format(prefix=self.config.cmd_prefix))
return
if len(cmd_args) == 1:
if cmd_args[0][0] == '<':
await self.post('add_to_playlist', {'name': 'Likes', 'uri':cmd_args[0][1:-1]}, dest.id)
else:
await self.post('add_to_playlist', {'name': 'Likes', 'uri':cmd_args[0]}, dest.id)
else:
for i in cmd_args:
if i[0] == '<':
await self.post('add_to_playlist', {'name': 'Likes', 'uri':i[1:-1]}, dest.id)
else:
await self.post('add_to_playlist', {'name': 'Likes', 'uri':i}, dest.id)
async def cmd_remove(self, message, dest, *cmd_args):
'''
remove from queue
usage {prefix}remove num
'''
if not cmd_args:
await dest.send('error:anger:\nusage `{prefix}remove URL(s)`'.format(prefix=self.config.cmd_prefix))
return
try:
index = int(cmd_args[0]) - 1
if index <= len(self.play_queue):
await self.post('remove', {'index': index, 'uri': self.play_queue.get('data').get('queue')[index].get('uri')}, dest.id)
else:
await self.safe_send(dest, f'{index} is out of queue range')
except ValueError:
await self.safe_send(dest, 'error:anger:\n usage `{prefix}remove num`'.format(prefix=self.config.cmd_prefix))
async def cmd_np(self, message, dest, *cmd_args):
'''
show song info
usage {prefix}np
'''
await self.post('state', postback=dest.id)
async def cmd_like(self, message, dest, *cmd_args):
'''
save current track to playlist [Likes]
usage {prefix}like
'''
if not self.player_status.get('is_liked'):
await self.post('like', {'uri': self.player_status.get('uri')}, dest.id)
async def cmd_likes(self, message, dest, *cmd_args):
'''
show how many songs in playlist [Likes]
usage {prefix}likes
'''
await self.post('playlist', {'name': 'Likes'}, dest.id)
async def cmd_playlists(self, message, dest, *cmd_args):
'''
show playlists name
usage {prefix}playlists
'''
await self.post('playlists', postback=dest.id)
async def cmd_mklist(self, message, dest, *cmd_args):
'''
create playlist
usage {prefix}mklist
'''
if not cmd_args:
await self.safe_send(dest, 'error:anger:\nusage: `{prefix}play URL`'.format(prefix=self.config.cmd_prefix))
return
await self.post('create_playlist', {'name': cmd_args[0]}, dest.id)
async def cmd_clear(self, message, dest, *cmd_args):
'''
clear queue
usage {prefix}clear
'''
await self.post('clear_queue', postback=dest.id)
async def cmd_shuffle(self, message, dest, *cmd_args):
'''
shuffle queue
usage {prefix}shuffle
'''
await self.post('shuffle', postback=dest.id)
async def cmd_purge(self, message, dest, *cmd_args):
'''
remove from auto playlist
usage {prefix}purge URLs
'''
if not cmd_args:
if self.player_status.get('is_liked'):
await self.post('remove_from_playlist', {'name':'Likes', 'uri': self.player_status.get('uri')}, dest.id)
else:
await self.safe_send(dest, 'error:anger:\nThis track is not in Likes')
return
for url in cmd_args:
await self.post('remove_from_playlist', {'name':'Likes', 'uri': url}, dest.id)
async def cmd_save(self, message, dest, *cmd_args):
'''
save song into playlist
usage {prefix}save
'''
if not cmd_args:
await self.post('add_to_playlist', {'name': 'Likes', 'uri': self.player_status.get('uri')}, dest.id)
else:
playlist_name = ' '.join(cmd_args)
await self.post('add_to_playlist', {'name': playlist_name, 'uri': self.player_status.get('uri')}, dest.id)
async def cmd_search(self, message, dest, *cmd_args):
'''
search song in youtube and gpm
usage {prefix}search keyword
'''
await self._cmd_search(dest, None, cmd_args)
async def cmd_gsearch(self, message, dest, *cmd_args):
'''
search song in gpm
usage {prefix}gsearch keyword
'''
await self._cmd_search(dest, 'gpm', cmd_args)
async def cmd_ysearch(self, message, dest, *cmd_args):
'''
search song in youtube
usage {prefix}ysearch keyword
'''
await self._cmd_search(dest, 'youtube', cmd_args)
async def _cmd_search(self, dest, provider, cmd_args):
'''
search
'''
if not cmd_args:
await dest.send('error:anger:\nusage `{prefix}search keyword`'.format(prefix=self.config.cmd_prefix))
return
query = ' '.join(cmd_args)
if provider:
await self.post('search', {'query': query, 'provider': provider}, dest.id)
else:
await self.post('search', {'query': query}, dest.id)
async def cmd_s(self, message, dest, *cmd_args):
if not cmd_args:
await self.post('skip', postback=dest.id)
return
try:
int(cmd_args[0])
await self.cmd_skip_to(message, dest, *cmd_args)
except ValueError:
await self.cmd_search(message, dest, *cmd_args)
@op_only
async def cmd_join(self, message, dest, *cmd_args):
'''
[operator only]join VC
usage {prefix}join
'''
if self.voice:
await dest.send(f'{self.user} is already in VC')
return
await self.join_vc()
@op_only
async def cmd_kick(self, message, dest, *cmd_args):
'''
[operator only]kick bot from VC
usage {prefix}kick
'''
if not self.voice:
await dest.send(f'{self.user} is not in VC')
return
await self.exit_vc()
@op_only
async def cmd_logout(self, message, dest, *cmd_args):
'''
[operator only]
usage {prefix}logout
'''
await self.safe_delete(message)
await self.logout()
exit(1)
async def cmd_show_alias(self, message, dest, *cmd_args):
'''
show command alias
usage {prefix}show_alias
'''
res_text = ''
if self.config.alias:
res_text = 'alias\n\n'
for i in self.config.alias:
res_text += f'`{i}` is`{self.config.alias[i]}`\n'
else:
res_text = 'command alias does not exist\nedit `config/alias.json`'
await self.safe_send(dest, res_text)
@op_only
async def cmd_reload_alias(self, message, dest, *cmd_args):
'''
reload config/alias.json
usage {prefix}reload_alias
'''
try:
with open('config/alias.json', 'r') as f:
self.config.alias = json.load(f)
await self.safe_send(dest, 'alias is updated')
except FileNotFoundError:
logging.error('alias file does not exist')
await self.safe_send(dest, 'alias file does not exist\nNo changed')
async def cmd_fuck(self, message, dest, *cmd_args):
'''
F U C K Y O U
'''
await self.safe_delete(message)
target = message.mentions or [message.author]
fuck_str = ':regional_indicator_f: :regional_indicator_u: :regional_indicator_c: :regional_indicator_k: :regional_indicator_y: :regional_indicator_o: :regional_indicator_u: '
fuck_emote = ['\U0001F1EB', '\U0001F1FA', '\U0001F1E8', '\U0001F1F0', '\U0001F595']
msg = await self.safe_send(dest, fuck_str, target)
for emote in fuck_emote:
self.loop.create_task(msg.add_reaction(emote))
async def cmd_potg(self, message, dest, *cmd_args):
await self.safe_delete(message)
target = message.mentions or [message.author]
potg_strs = [':right_facing_fist: :left_facing_fist: 推薦されました:チームプレイヤー', ':handshake: 推薦されました:スポーツマンシップ', ':point_right: 推薦を獲得:ショットコーラー']
await self.safe_send(dest, random.choice(potg_strs), target)
async def cmd_uc(self, message, dest, *cmd_args):
'''
U C
'''
await self.safe_delete(message)
await self.post('play', {'uri': 'https://youtu.be/ZHbzi_stmiI'}, dest.id)
@op_only
async def cmd_updatedb(self, message, dest, *cmd_args):
'''
gpmのライブラリをアップデートするコマンド
usage {prefix}updatedb user_name
'''
if not cmd_args:
await self.safe_send(dest, 'error:anger:\nusage: `{prefix}updatedb user`'.format(prefix=self.config.cmd_prefix))
return
await self.post('update_db', {'user': cmd_args[0]}, dest.id)
async def cmd_web(self, message, dest, *cmd_args):
'''
open web ui
'''
await self.safe_send(dest, 'Open web player\n:point_right: https://aria.gaiji.pro/auth/github/login')
@op_only
async def cmd_token(self, message, dest, *cmd_args):
'''
get token
usage {prefix}token
'''
await self.post('token', postback=dest.id)
async def cmd_invite(self, message, dest, *cmd_args):
'''
generate invite code for account registration
usage {prefix}invite
'''
await self.post('invite', postback=dest.id)
async def cmd_help(self, message, dest, *cmd_args):
'''
show commandlist
usage {prefix}help
'''
if cmd_args:
handler = getattr(self, f'cmd_{cmd_args[0]}', None)
if handler:
res_text = (f'Command: **{cmd_args[0]}**'
f'```{textwrap.dedent(handler.__doc__.format(prefix=self.config.cmd_prefix))}```')
await self.safe_send(dest, res_text)
return
elif self.config.alias.get(cmd_args[0]):
alias = self.config.alias.get(cmd_args[0])
handler = getattr(self, f'cmd_{alias}', None)
res_text = (f'Command: **{cmd_args[0]}**'
f'```{textwrap.dedent(handler.__doc__.format(prefix=self.config.cmd_prefix))}```')
await self.safe_send(dest, res_text)
return
await self.gen_command_list()
print_text = 'available commands\nmore info `.help [command]` ```'
for cmd in self.commandlist:
print_text += f'{cmd}, '
print_text += '```'
await self.safe_send(dest, print_text)
async def cmd_save_que(self, mesasge, dest, *cmd_args):
'''
save que as playlist
usage {prefix}save_que playlist name
'''
playlist = ' '.join(cmd_args) or 'Likes'
# TODO: check playlist valid
_que = await self.parse_queue(self.play_queue)
for song in _que:
_uri = song['uri']
await self.post('add_to_playlist', {'name': playlist, 'uri': _uri}, dest.id)
@op_only
async def cmd_test(self, message, dest, *cmd_args):
'''
何でも屋