-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.py
129 lines (109 loc) · 5.11 KB
/
music.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
import discord
import os
import asyncio
import yt_dlp
from dotenv import load_dotenv
def run_bot():
load_dotenv()
TOKEN = os.getenv("discord_token")
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
# Dictionary to store voice_client instances and queues for each server
voice_clients = {}
queues = {}
yt_dl_options = {"format": "bestaudio/best"}
ytdl = yt_dlp.YoutubeDL(yt_dl_options)
ffmpeg_options = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn -filter:a "volume=0.25"'
}
@client.event
async def on_ready():
print(f"{client.user} is now online and ready to play music!")
async def play_next(guild_id, text_channel):
if queues[guild_id]:
url = queues[guild_id].pop(0)
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
song = data["url"]
title = data["title"]
player = discord.FFmpegOpusAudio(song, **ffmpeg_options)
voice_clients[guild_id].play(player, after=lambda e: client.loop.create_task(play_next(guild_id, text_channel)))
await text_channel.send(f"Now playing: {title}") # Adds text of what is now playing
@client.event
async def on_message(message):
# Use guild ID to manage voice clients per server
guild_id = message.guild.id
if message.author == client.user:
return
if message.content.startswith("dreamyplay"):
try:
if message.author.voice:
voice_channel = message.author.voice.channel
if guild_id in voice_clients:
voice_client = voice_clients[guild_id]
if voice_client.is_connected():
await voice_client.move_to(voice_channel)
else:
voice_client = await voice_channel.connect()
else:
voice_client = await voice_channel.connect()
voice_clients[guild_id] = voice_client
if guild_id not in queues:
queues[guild_id] = []
else:
await message.channel.send("You need to be in a voice channel to use this command.")
return
except Exception as e:
print(f"Error connecting to voice channel: {e}")
return
try:
url = message.content.split()[1]
queues[guild_id].append(url)
if not voice_clients[guild_id].is_playing():
await play_next(guild_id, message.channel)
else:
data = await asyncio.get_event_loop().run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
title = data["title"]
queue_position = len(queues[guild_id])
await message.channel.send(f"'{title}' added to queue. Position: {queue_position}")
except Exception as e:
print(f"Error adding song to queue: {e}")
if message.content.startswith("dreamypause"):
try:
if guild_id in voice_clients and voice_clients[guild_id].is_playing():
voice_clients[guild_id].pause()
else:
await message.channel.send("Nothing is playing to pause.")
except Exception as e:
print(f"Error pausing audio: {e}")
if message.content.startswith("dreamyresume"):
try:
if guild_id in voice_clients and voice_clients[guild_id].is_paused():
voice_clients[guild_id].resume()
else:
await message.channel.send("Nothing is paused to resume.")
except Exception as e:
print(f"Error resuming audio: {e}")
if message.content.startswith("dreamystop"):
try:
if guild_id in voice_clients:
voice_clients[guild_id].stop()
await voice_clients[guild_id].disconnect()
del voice_clients[guild_id] # Remove the voice_client from the dictionary
queues[guild_id].clear() # Clear the queue for the guild
else:
await message.channel.send("Nothing is playing to stop.")
except Exception as e:
print(f"Error stopping audio: {e}")
if message.content.startswith("dreamyskip"):
try:
if guild_id in voice_clients and voice_clients[guild_id].is_playing():
voice_clients[guild_id].stop()
await play_next(guild_id, message.channel)
else:
await message.channel.send("Nothing is playing to skip.")
except Exception as e:
print(f"Error skipping audio: {e}")
client.run(TOKEN)