-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.py
162 lines (136 loc) · 7.95 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import yaml
import asyncio
import discord
import pafy
from discord.ext import commands
import youtube_dl
f = open("config.yml", "r")
cfg = yaml.load(f, Loader=yaml.FullLoader)
configMessages = cfg["messages"]
configMain = cfg["main"]
class newmusic(commands.Cog):
def __init__(self, client):
self.client = client
self.queue = {}
self.setup()
def setup(self):
for g in self.client.guilds:
self.queue[g.id] = []
@commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["notInVoice"],color=discord.Color.green())
await ctx.send(embed=embed)
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
elif ctx.voice_client.channel != ctx.author.voice.channel:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["alreadyInUse"],color=discord.Color.green())
await ctx.send(embed=embed)
async def checkqueue(self,ctx):
if len(self.queue[ctx.guild.id]) > 0:
ctx.voice_client.stop()
await self.playsong(ctx,self.queue[ctx.guild.id][0])
self.queue[ctx.guild.id].pop(0)
async def searchurl(self,amount,song,get_url=False):
info = await self.client.loop.run_in_executor(None, lambda:youtube_dl.YoutubeDL({'format':"bestaudio","quiet": True}).extract_info(f"ytsearch{amount}:{song}", download=False, ie_key="YoutubeSearch"))
if len(info["entries"]) == 0: return None
return[entry["webpage_url"] for entry in info["entries" if get_url else info]]
async def playsong(self,ctx,song):
url = pafy.new(song).getbestaudio().url
ctx.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url)), after=lambda error: self.client.loop.create_task(self.checkqueue(ctx)))
ctx.voice_client.source.volume = 0.5
@commands.command()
async def play(self, ctx, *,song=None):
if song is None:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["noLink"],color=discord.Color.green())
return await ctx.send(embed=embed)
if ctx.author.voice is None:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["notInVoice"],color=discord.Color.green())
return await ctx.send(embed=embed)
if ctx.voice_client is None:
await ctx.author.voice.channel.connect()
elif ctx.voice_client.channel != ctx.author.voice.channel:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["alreadyInUse"],color=discord.Color.green())
return await ctx.send(embed=embed)
if not("youtube.com/watch?" in song or "https://youtu.be" in song):
await ctx.send("Link not known. Starting search")
result = await self.searchurl(1, song,get_url=True)
if result is None:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["searchNoResults"],color=discord.Color.green())
return await ctx.send(embed=embed)
song = result[0]
if ctx.voice_client.source is not None:
queue_len = len(self.queue[ctx.guild.id])
if queue_len < 20:
self.queue[ctx.guild.id].append(song)
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["addedToQueue"].format(queue_len=queue_len+1),color=discord.Color.green())
return await ctx.send(embed=embed)
else:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["queueIsFull"],color=discord.Color.green())
return await ctx.send(embed=embed)
await self.playsong(ctx,song)
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["nowPlaying"].format(song=song),color=discord.Color.green())
await ctx.send(embed=embed)
@commands.command()
async def skip(self, ctx):
if ctx.author.voice is None:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["notInVoice"],color=discord.Color.green())
await ctx.send(embed=embed)
if ctx.voice_client is None:
await ctx.author.voice.channel.connect()
elif ctx.voice_client.channel != ctx.author.voice.channel:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["alreadyInUse"],color=discord.Color.green())
await ctx.send(embed=embed)
ctx.voice_client.stop()
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["skip"],color=discord.Color.green())
await ctx.send(embed=embed)
await self.checkqueue(ctx)
@commands.command()
async def pause(self,ctx):
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["paused"],color=discord.Color.green())
await ctx.send(embed=embed)
await ctx.voice_client.pause()
@commands.command()
async def resume(self,ctx):
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["resumed"],color=discord.Color.green())
await ctx.send(embed=embed)
await ctx.voice_client.resume()
@commands.command()
async def info(self,ctx):
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["infoMsg"],color=discord.Color.green())
await ctx.send(embed=embed)
@commands.command()
async def stop(self,ctx):
ctx.voice_client.stop()
self.queue[ctx.guild.id].clear()
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["clear"],color=discord.Color.green())
await ctx.send(embed=embed)
async def clear(self,ctx):
ctx.voice_client.stop()
self.queue[ctx.guild.id].clear()
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["stop"],color=discord.Color.green())
await ctx.send(embed=embed)
@commands.command()
async def next(self, ctx):
if ctx.author.voice is None:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["notInVoice"],color=discord.Color.green())
await ctx.send(embed=embed)
if ctx.voice_client is None:
await ctx.author.voice.channel.connect()
elif ctx.voice_client.channel != ctx.author.voice.channel:
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["alreadyInUse"],color=discord.Color.green())
await ctx.send(embed=embed)
ctx.voice_client.stop()
embed = discord.Embed(title="Music.BOT",url="https://github.com/felixslama/music.bot", description=configMessages["skip"],color=discord.Color.green())
await ctx.send(embed=embed)
await self.checkqueue(ctx)
@commands.command()
async def check(self, ctx):
await self.checkqueue(ctx)
@commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
@commands.command()
async def leave(self,ctx):
await ctx.voice_client.disconnect()