-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.py
100 lines (77 loc) · 3.54 KB
/
player.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
import urllib
import asyncio
import os
from os import environ
from os.path import join, dirname
from dotenv import load_dotenv
from discord.ext import commands
import discord
from youtube_dl import YoutubeDL
from asyncio import sleep
YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'False', 'postprocessors': [{'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
intents = discord.Intents.all();
bot = commands.Bot(intents=intents, command_prefix='!')
@bot.event
async def on_ready():
print('Бот запущен и ожидает команду.\nКоманды:\n!play <ссылка> - воспроизводит плейлист\n!kill - убивает бота')
#
# ЭТА КОМАНДА ВЫЗЫВАЕТ ПОМОЩЬ ПО БОТУ
#
@bot.command(name='помоги')
async def helpme(ctx):
await ctx.send(f'Команды:\n!play <ссылка> - воспроизводит плейлист\n!kill - убивает бота\n\nМожно и по русски:\n!включи, !помоги, !умри')
#
# ЭТА КОМАНДА ЗАСТАВЛЯЕТ БОТА ПОДКЛЮЧИТЬСЯ К ГОЛОСОВОМУ КАНАЛУ И ВОСПРОИЗВЕСТИ ЗВУК ИЗ ССЫЛКИ
#
@bot.command(name='play')
async def play(ctx, arg):
ctx.message.author.voice.channel
voice_channel = ctx.message.author.voice.channel
vc = await voice_channel.connect()
if vc.is_playing():
# ЭТОТ КУСОК КОДА СТОПИТ ТЕКУЩУЮ МУЗЫКУ, ЕСЛИ ВОСПРОИЗВОДИТСЯ, И ВКЛЮЧАЕТ НОВУЮ
await ctx.send(f'{ctx.message.author.mention}, музыка уже проигрывается. Останавливаю, и включаю нов��ю.\nДля воспроизведения другого плейлиста, отправь команду !play <ссылка>')
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],}
vc.stop()
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(arg, download=False)
URL = info['formats'][0]['url']
vc.play(discord.FFmpegPCMAudio(source=URL, **FFMPEG_OPTIONS))
while vc.is_playing():
await sleep(1)
if not vc.is_paused():
await vc.disconnect()
else:
# ЭТОТ КУСОК КОДА ВОСПРОИЗВОДИТ МУЗЫКУ ПО ССЫЛКЕ
await ctx.send(f'{ctx.message.author.mention}, включаю музыку.\nДля воспроизведения другого плейлиста, отправь команду !play <ссылка>')
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(arg, download=False)
URL = info['formats'][0]['url']
vc.play(discord.FFmpegPCMAudio(source=URL, **FFMPEG_OPTIONS))
while vc.is_playing():
await sleep(1)
if not vc.is_paused():
await vc.disconnect()
#
# ЭТА КОМАНДА ЗАСТАВЛЯЕТ БОТА УМЕРЕТЬ
#
@bot.command(name='умри')
async def kill(ctx):
await ctx.send(f'Умер.')
print('Умер.')
await ctx.bot.close()
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
bot.run(environ.get('token'))
if __name__ == '__main__':
main()