-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboomthebox_bot.py
82 lines (62 loc) · 2.46 KB
/
boomthebox_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
import discord
from discord.ext import commands
import yt_dlp
import asyncio
# Got to the FFMpeg website and download the files, place them on the root folder of this bot
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
FFMPEG_OPTIONS = {'options' : '-vn'}
YDL_OPTIONS = {'format' : 'bestaudio', 'noplaylist' : True}
with open('auth_key.txt', 'r') as file:
KEY = file.read()
class MusicBot(commands.Cog):
def __init__(self, client):
self.client = client
self.queue = []
@commands.command()
async def play(self, ctx, *, search):
voice_channel = ctx.author.voice.channel if ctx.author.voice else None
if not voice_channel:
return await ctx.send("You're not in a voice channel!")
if not ctx.voice_client:
await voice_channel.connect()
async with ctx.typing():
with yt_dlp.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(f"ytsearch:{search}", download=False)
if 'entries' in info:
info = info["entries"][0]
url = info["url"]
title = info["title"]
self.queue.append((url, title))
await ctx.send(f"Added to queue: --> {title}")
if not ctx.voice_client.is_playing():
await self.play_next(ctx)
async def play_next(self, ctx):
if self.queue:
print("Inside self.queue")
url, title = self.queue.pop(0)
print("Inside pop")
try:
source = await discord.FFmpegOpusAudio.from_probe(url, **FFMPEG_OPTIONS)
except Exception as e:
print(e)
print("Inside source =")
ctx.voice_client.play(source, after=lambda _:self.client.loop.create_task(self.play_next(ctx)))
print("Inside voice_client.play")
await ctx.send(f"Now playing -->{title}")
elif not ctx.voice_client.is_playing():
await ctx.send("Queue is empty!")
@commands.command()
async def skip(self, ctx):
if ctx.voice_client and ctx.voice_client.is_playing():
ctx.voice_client.stop()
await ctx.send("Skipped")
@commands.command()
async def disconnect(self, ctx):
await ctx.disconnect()
client = commands.Bot(command_prefix="+", intents=intents)
async def main():
await client.add_cog(MusicBot(client))
await client.start(KEY)
asyncio.run(main())