-
Notifications
You must be signed in to change notification settings - Fork 1
/
Music basic bot.py
61 lines (50 loc) · 2.04 KB
/
Music basic 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
import discord
from discord.ext import commands
import yt_dlp
import asyncio
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
FFMPEG_OPTIONS = {'options': '-vn'}
YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': True}
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:
url, title = self.queue.pop(0)
source = await discord.FFmpegOpusAudio.from_probe(url, **FFMPEG_OPTIONS)
ctx.voice_client.play(source, after=lambda: self.client.loop.create_task(self.play_next(ctx)))
await ctx.send(f'Now playing **{title}**')
else:
await ctx.voice_client.disconnect()
await ctx.send("Queue is empty, leaving the voice channel.")
@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")
client = commands.Bot(command_prefix="!", intents=intents)
async def main():
await client.add_cog(MusicBot(client))
await client.start('Your_Bot_Token')
asyncio.run(main())