-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
386 lines (347 loc) · 13.4 KB
/
main.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
import discord
import os
# load our local env so we dont have the token in public
from dotenv import load_dotenv
from discord.ext import commands
from discord.ext import tasks
from discord.utils import get
from youtube_dl import YoutubeDL
import gtts
from keep_alive import keep_alive
import itertools
import time
import random
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = commands.Bot(command_prefix='?') # prefix our commands
status = itertools.cycle(['Counter Strike : Global Offensive','League of Legend'])
queues={}
playing={}
@client.event # check if bot is ready
async def on_ready():
change_status.start()
print('Bot online!')
@tasks.loop(seconds=1800)
async def change_status():
await client.change_presence(activity=discord.Game(next(status)))
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print(current_time)
@client.command(name='join', help='join user voice')
async def join(ctx):
try:
channel = ctx.message.author.voice.channel
except:
await ctx.send('Vào voice đi đã rồi hẵng gọi tao!')
return
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
def check_queue(ctx,id):
global queues
global playing
try:
if queues[id]!=[]:
voice=ctx.guild.voice_client
source = queues[id].pop(0)
playing.update({ctx.message.guild.id:source[1]})
voice.play(source[0],after=lambda x=None: check_queue(ctx,ctx.message.guild.id))
print("*** Playing : "+source[1])
else:
guild_id = ctx.message.guild.id
if guild_id in playing:
del playing[guild_id]
if guild_id in queues:
del queues[guild_id]
print('END QUEUE')
except:
guild_id = ctx.message.guild.id
if guild_id in playing:
del playing[guild_id]
if guild_id in queues:
del queues[guild_id]
print('END QUEUE')
@client.command(name='queue',help='+link/search thêm vào hàng chờ')
async def queue(ctx,*url):
global queues
YDL_OPTIONS = {
'format': 'bestaudio',
'noplaylist': 'True',
'--default-search': "ytsearch"
}
FFMPEG_OPTIONS = {
'before_options':
'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'
}
with YoutubeDL(YDL_OPTIONS) as ydl:
if 'https://' in str(url[0]):
info = ydl.extract_info(url[0], download=False)
else:
info = ydl.extract_info(f"ytsearch:{url}",download=False)['entries'][0]
source= [discord.FFmpegOpusAudio(info['url'], **FFMPEG_OPTIONS),info['title']]
guild_id = ctx.message.guild.id
if guild_id in queues:
queues[guild_id].append(source)
else:
queues[guild_id]=[source]
embed=discord.Embed(color=0x8dcef7)
embed.add_field(name='Add to Queue !\n',value=':musical_note: ' + info['title'], inline=True)
embed.set_footer(text='\nadd by '+ctx.author.display_name)
await ctx.send(embed=embed)
print('***QUEUE*** '+info['title'])
@client.command(name='pnext', help='+link/search để phát ngay sau bài đang phát')
async def play_next(ctx, *url):
global queues
YDL_OPTIONS = {
'format': 'bestaudio',
'noplaylist': 'True',
'--default-search': "ytsearch"
}
FFMPEG_OPTIONS = {
'before_options':
'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'
}
with YoutubeDL(YDL_OPTIONS) as ydl:
if 'https://' in str(url[0]):
info = ydl.extract_info(url[0], download=False)
else:
info = ydl.extract_info(f"ytsearch:{url}",download=False)['entries'][0]
source= [discord.FFmpegOpusAudio(info['url'], **FFMPEG_OPTIONS),info['title']]
guild_id = ctx.message.guild.id
if guild_id in queues:
queues[guild_id].insert(0,source)
else:
queues[guild_id]=[source]
embed=discord.Embed(color=0x8dcef7)
embed.add_field(name='Add to Play Next !\n',value=':musical_note: ' + info['title'], inline=True)
embed.set_footer(text='\nadd by '+ctx.author.display_name)
await ctx.send(embed=embed)
print('***PLAY NEXT*** '+info['title'])
@client.command(name='p', help='+link/search để phát hoặc cho vào hàng chờ')
async def play(ctx, *url):
global queues
global playing
#join
try:
channel = ctx.message.author.voice.channel
except:
await ctx.send('Vào voice đi đã rồi hẵng gọi tao!')
return
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
YDL_OPTIONS = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'noplaylist': True,
'--default-search': "ytsearch"
}
FFMPEG_OPTIONS = {
'before_options':
'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'
}
with YoutubeDL(YDL_OPTIONS) as ydl:
if 'https://' in str(url[0]):
info = ydl.extract_info(url[0], download=False)
else:
info = ydl.extract_info(f"ytsearch:{url}",download=False)['entries'][0]
guild_id = ctx.message.guild.id
if voice.is_playing():
source= [discord.FFmpegOpusAudio(info['url'], **FFMPEG_OPTIONS),info['title']]
if guild_id in queues:
queues[guild_id].append(source)
else:
queues[guild_id]=[source]
embed=discord.Embed(color=0x8dcef7)
embed.add_field(name='Add to Queue !\n',value=':musical_note: ' + info['title'], inline=True)
embed.set_footer(text='\nadd by '+ctx.author.display_name)
await ctx.send(embed=embed)
print('***QUEUE*** '+info['title'])
else:
source = [discord.FFmpegOpusAudio(info['url'], **FFMPEG_OPTIONS),info['title']]
embed=discord.Embed(color=0x8dcef7)
embed.add_field(name='Playing music !\n',value=':musical_note: ' + str(info['title']), inline=True)
embed.set_footer(text='\nadd by '+ctx.author.display_name)
await ctx.send(embed=embed)
print('*** PLAYING : '+info['title'])
playing.update({guild_id:info['title']})
voice.play(source[0],after=lambda x=None: check_queue(ctx,ctx.message.guild.id))
#play list from URL
@client.command(name='pl', help='+URL playlist để phát hoặc cho vào hàng chờ')
async def play_list(ctx, *url):
global queues
global playing
#join
try:
channel = ctx.message.author.voice.channel
except:
await ctx.send('Vào voice đi đã rồi hẵng gọi tao!')
return
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
YDL_OPTIONS = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'noplaylist': False,
'quiet': True,
'playlistend': 10
}
FFMPEG_OPTIONS = {
'before_options':
'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'
}
with YoutubeDL(YDL_OPTIONS) as ydl:
if 'https://' in str(url[0]):
await ctx.send('Extracting list, Please wait...')
infos = ydl.extract_info(url[0], download=False)['entries']
guild_id = ctx.message.guild.id
if voice.is_playing():
list_queue=['']
for info in infos:
source= [discord.FFmpegOpusAudio(info['url'], **FFMPEG_OPTIONS),info['title']]
list_queue.append(info['title'])
print('***QUEUE*** '+info['title'])
if guild_id in queues:
queues[guild_id].append(source)
else:
queues[guild_id]=[source]
embed=discord.Embed(color=0x8dcef7)
embed.add_field(name='Add to Queue !',value='\n:musical_note: '.join(list_queue), inline=False)
embed.set_footer(text='add by '+ctx.author.display_name)
await ctx.send(embed=embed)
else:
first_info=infos.pop(0)
source = [discord.FFmpegOpusAudio(first_info['url'], **FFMPEG_OPTIONS),first_info['title']]
embed=discord.Embed(color=0x8dcef7)
embed.add_field(name='Playing music !\n',value=':musical_note: ' + str(first_info['title']), inline=True)
embed.set_footer(text='\nadd by '+ctx.author.display_name)
await ctx.send(embed=embed)
print('***PLAYING : '+first_info['title'])
playing.update({guild_id:first_info['title']})
voice.play(source[0],after=lambda x=None: check_queue(ctx,ctx.message.guild.id))
list_queue=['']
for info in infos:
source= [discord.FFmpegOpusAudio(info['url'], **FFMPEG_OPTIONS),info['title']]
list_queue.append(info['title'])
print('***QUEUE*** '+info['title'])
if guild_id in queues:
queues[guild_id].append(source)
else:
queues[guild_id]=[source]
embed=discord.Embed(color=0x8dcef7)
embed.add_field(name='Add to Queue !',value='\n:musical_note: '.join(list_queue), inline=False)
embed.set_footer(text='add by '+ctx.author.display_name)
await ctx.send(embed=embed)
else:
await ctx.send('Wrong link play list! Try Again...')
@client.command(name='playing', help='bài đang hát')
async def show_playing(ctx):
global playing
guild_id = ctx.message.guild.id
if guild_id in playing:
embed=discord.Embed(color=0x8dcef7)
embed.add_field(name='Playing !\n',value=':musical_note: ' + playing[guild_id], inline=True)
embed.set_footer(text='\nrequest show by '+ctx.author.display_name)
await ctx.send(embed=embed)
else:
await ctx.send('Nothing is playing now !')
# command to speech from text
@client.command(name='t', help='+ message để bot nói')
async def talk(ctx, *txt):
#join
try:
channel = ctx.message.author.voice.channel
print("JOIN " + str(channel.name))
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
#convert text to speech
FFMPEG_OPTIONS = {'options': '-vn'}
tts = gtts.gTTS(str(txt), lang="vi",slow=False)
path = './temp/temp_sound.mp3'
tts.save(path)
voice.play(discord.FFmpegOpusAudio(path, **FFMPEG_OPTIONS))
voice.is_playing()
except:
await ctx.send('Vào voice đi đã rồi hẵng gọi tao!')
# command to resume voice if it is paused
@client.command(name='resume', help='tiếp tục')
async def resume(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if not voice.is_playing():
voice.resume()
await ctx.send('Bot is resuming!')
print('RESUME')
# command to pause voice if it is playing
@client.command(name='pause', help='dừng')
async def pause(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
await ctx.send('Bot has been paused!')
print('PAUSE')
# command to stop voice
@client.command(name='leave', help='cút')
async def leave(ctx):
global playing
global queues
voice = get(client.voice_clients, guild=ctx.guild)
guild_id = ctx.message.guild.id
if voice.is_playing():
await ctx.send('Stopping...',delete_after=2)
voice.stop()
if guild_id in playing:
del playing[guild_id]
if guild_id in queues:
del queues[guild_id]
await voice.disconnect()
print('DISCONECT')
# command to clear channel messages
@client.command(name='clear', help='+ n ,xóa n tin nhắn gần nhất(default 5)')
async def clear(ctx, amount=5):
await ctx.channel.purge(limit=amount+1)
await ctx.send("Messages have been cleared",delete_after=5.0)
print('CLEAR')
@client.command(name='chui')
async def chui(ctx,*ten):
choice=random.randint(1,5)
if choice ==1:
await ctx.send('ĐỊT MẸ {}!'.format(' '.join(ten).upper()))
elif choice ==2:
await ctx.send('THẰNG {} BỐC CỨT!'.format(' '.join(ten).upper()))
elif choice ==3:
await ctx.send('THẰNG {} ĐẦU BUỒI QUẤN GIẺ!'.format(' '.join(ten).upper()))
elif choice ==4:
await ctx.send('THẰNG {} ÓC CẶC!'.format(' '.join(ten).upper()))
elif choice ==5:
await ctx.send('THẰNG LỒN {} BÚ BUỒI!'.format(' '.join(ten).upper()))
@client.command(name='skip', help='next')
async def skip(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.stop()
await ctx.send('SKIP!')
print('SKIP')
keep_alive()
client.run(TOKEN)