-
Notifications
You must be signed in to change notification settings - Fork 0
/
guanatino.py
67 lines (56 loc) · 1.76 KB
/
guanatino.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
import discord, os
from discord.ext import commands, tasks
import yt_dlp as youtube_dl
TOKEN = os.getenv('')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='radio!', intents=intents)
ffmpeg_options = {
'options': '-vn',
}
playlist = [
"",
]
@bot.event
async def on_ready():
print(f'{bot.user.name} est connecté.')
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="radio!join"))
play_next_song.start()
@bot.command()
async def join(ctx):
if ctx.author.voice:
channel = ctx.author.voice.channel
await channel.connect()
else:
await ctx.send("Tu n'es pas dans un salon !")
def play_song(vc, url):
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
url2 = info['formats'][0]['url']
vc.play(discord.FFmpegPCMAudio(url2, **ffmpeg_options), after=lambda e: print('done', e))
except Exception as e:
print(f"An error occurred while playing {url}: {e}")
@tasks.loop(seconds=5.0)
async def play_next_song():
for guild in bot.guilds:
vc = discord.utils.get(bot.voice_clients, guild=guild)
if vc and not vc.is_playing():
if len(playlist) > 0:
url = playlist.pop(0)
play_song(vc, url)
playlist.append(url)
@bot.command()
async def leave(ctx):
if ctx.voice_client:
await ctx.voice_client.disconnect()
bot.run(TOKEN)