-
Notifications
You must be signed in to change notification settings - Fork 96
/
music.py
332 lines (259 loc) · 13.8 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
This example cog demonstrates basic usage of Lavalink.py, using the DefaultPlayer.
As this example primarily showcases usage in conjunction with discord.py, you will need to make
modifications as necessary for use with another Discord library.
Usage of this cog requires Python 3.6 or higher due to the use of f-strings.
Compatibility with Python 3.5 should be possible if f-strings are removed.
"""
import re
import discord
import lavalink
from discord.ext import commands
from lavalink.events import TrackStartEvent, QueueEndEvent
from lavalink.errors import ClientError
from lavalink.filters import LowPass
from lavalink.server import LoadType
url_rx = re.compile(r'https?://(?:www\.)?.+')
class LavalinkVoiceClient(discord.VoiceProtocol):
"""
This is the preferred way to handle external voice sending
This client will be created via a cls in the connect method of the channel
see the following documentation:
https://discordpy.readthedocs.io/en/latest/api.html#voiceprotocol
"""
def __init__(self, client: discord.Client, channel: discord.abc.Connectable):
self.client = client
self.channel = channel
self.guild_id = channel.guild.id
self._destroyed = False
if not hasattr(self.client, 'lavalink'):
# Instantiate a client if one doesn't exist.
# We store it in `self.client` so that it may persist across cog reloads,
# however this is not mandatory.
self.client.lavalink = lavalink.Client(client.user.id)
self.client.lavalink.add_node(host='localhost', port=2333, password='youshallnotpass',
region='us', name='default-node')
# Create a shortcut to the Lavalink client here.
self.lavalink = self.client.lavalink
async def on_voice_server_update(self, data):
# the data needs to be transformed before being handed down to
# voice_update_handler
lavalink_data = {
't': 'VOICE_SERVER_UPDATE',
'd': data
}
await self.lavalink.voice_update_handler(lavalink_data)
async def on_voice_state_update(self, data):
channel_id = data['channel_id']
if not channel_id:
await self._destroy()
return
self.channel = self.client.get_channel(int(channel_id))
# the data needs to be transformed before being handed down to
# voice_update_handler
lavalink_data = {
't': 'VOICE_STATE_UPDATE',
'd': data
}
await self.lavalink.voice_update_handler(lavalink_data)
async def connect(self, *, timeout: float, reconnect: bool, self_deaf: bool = False, self_mute: bool = False) -> None:
"""
Connect the bot to the voice channel and create a player_manager
if it doesn't exist yet.
"""
# ensure there is a player_manager when creating a new voice_client
self.lavalink.player_manager.create(guild_id=self.channel.guild.id)
await self.channel.guild.change_voice_state(channel=self.channel, self_mute=self_mute, self_deaf=self_deaf)
async def disconnect(self, *, force: bool = False) -> None:
"""
Handles the disconnect.
Cleans up running player and leaves the voice client.
"""
player = self.lavalink.player_manager.get(self.channel.guild.id)
# no need to disconnect if we are not connected
if not force and not player.is_connected:
return
# None means disconnect
await self.channel.guild.change_voice_state(channel=None)
# update the channel_id of the player to None
# this must be done because the on_voice_state_update that would set channel_id
# to None doesn't get dispatched after the disconnect
player.channel_id = None
await self._destroy()
async def _destroy(self):
self.cleanup()
if self._destroyed:
# Idempotency handling, if `disconnect()` is called, the changed voice state
# could cause this to run a second time.
return
self._destroyed = True
try:
await self.lavalink.player_manager.destroy(self.guild_id)
except ClientError:
pass
class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot
if not hasattr(bot, 'lavalink'):
bot.lavalink = lavalink.Client(bot.user.id)
bot.lavalink.add_node(host='localhost', port=2333, password='youshallnotpass',
region='us', name='default-node')
self.lavalink: lavalink.Client = bot.lavalink
self.lavalink.add_event_hooks(self)
def cog_unload(self):
"""
This will remove any registered event hooks when the cog is unloaded.
They will subsequently be registered again once the cog is loaded.
This effectively allows for event handlers to be updated when the cog is reloaded.
"""
self.lavalink._event_hooks.clear()
async def cog_command_error(self, ctx, error):
if isinstance(error, commands.CommandInvokeError):
await ctx.send(error.original)
# The above handles errors thrown in this cog and shows them to the user.
# This shouldn't be a problem as the only errors thrown in this cog are from `ensure_voice`
# which contain a reason string, such as "Join a voicechannel" etc. You can modify the above
# if you want to do things differently.
async def create_player(ctx: commands.Context):
"""
A check that is invoked before any commands marked with `@commands.check(create_player)` can run.
This function will try to create a player for the guild associated with this Context, or raise
an error which will be relayed to the user if one cannot be created.
"""
if ctx.guild is None:
raise commands.NoPrivateMessage()
player = ctx.bot.lavalink.player_manager.create(ctx.guild.id)
# Create returns a player if one exists, otherwise creates.
# This line is important because it ensures that a player always exists for a guild.
# Most people might consider this a waste of resources for guilds that aren't playing, but this is
# the easiest and simplest way of ensuring players are created.
# These are commands that require the bot to join a voicechannel (i.e. initiating playback).
# Commands such as volume/skip etc don't require the bot to be in a voicechannel so don't need listing here.
should_connect = ctx.command.name in ('play',)
voice_client = ctx.voice_client
if not ctx.author.voice or not ctx.author.voice.channel:
# Check if we're in a voice channel. If we are, tell the user to join our voice channel.
if voice_client is not None:
raise commands.CommandInvokeError('You need to join my voice channel first.')
# Otherwise, tell them to join any voice channel to begin playing music.
raise commands.CommandInvokeError('Join a voicechannel first.')
voice_channel = ctx.author.voice.channel
if voice_client is None:
if not should_connect:
raise commands.CommandInvokeError("I'm not playing music.")
permissions = voice_channel.permissions_for(ctx.me)
if not permissions.connect or not permissions.speak:
raise commands.CommandInvokeError('I need the `CONNECT` and `SPEAK` permissions.')
if voice_channel.user_limit > 0:
# A limit of 0 means no limit. Anything higher means that there is a member limit which we need to check.
# If it's full, and we don't have "move members" permissions, then we cannot join it.
if len(voice_channel.members) >= voice_channel.user_limit and not ctx.me.guild_permissions.move_members:
raise commands.CommandInvokeError('Your voice channel is full!')
player.store('channel', ctx.channel.id)
await ctx.author.voice.channel.connect(cls=LavalinkVoiceClient)
elif voice_client.channel.id != voice_channel.id:
raise commands.CommandInvokeError('You need to be in my voicechannel.')
return True
@lavalink.listener(TrackStartEvent)
async def on_track_start(self, event: TrackStartEvent):
guild_id = event.player.guild_id
channel_id = event.player.fetch('channel')
guild = self.bot.get_guild(guild_id)
if not guild:
return await self.lavalink.player_manager.destroy(guild_id)
channel = guild.get_channel(channel_id)
if channel:
await channel.send('Now playing: {} by {}'.format(event.track.title, event.track.author))
@lavalink.listener(QueueEndEvent)
async def on_queue_end(self, event: QueueEndEvent):
guild_id = event.player.guild_id
guild = self.bot.get_guild(guild_id)
if guild is not None:
await guild.voice_client.disconnect(force=True)
@commands.command(aliases=['p'])
@commands.check(create_player)
async def play(self, ctx, *, query: str):
""" Searches and plays a song from a given query. """
# Get the player for this guild from cache.
player = self.bot.lavalink.player_manager.get(ctx.guild.id)
# Remove leading and trailing <>. <> may be used to suppress embedding links in Discord.
query = query.strip('<>')
# Check if the user input might be a URL. If it isn't, we can Lavalink do a YouTube search for it instead.
# SoundCloud searching is possible by prefixing "scsearch:" instead.
if not url_rx.match(query):
query = f'ytsearch:{query}'
# Get the results for the query from Lavalink.
results = await player.node.get_tracks(query)
embed = discord.Embed(color=discord.Color.blurple())
# Valid load_types are:
# TRACK - direct URL to a track
# PLAYLIST - direct URL to playlist
# SEARCH - query prefixed with either "ytsearch:" or "scsearch:". This could possibly be expanded with plugins.
# EMPTY - no results for the query (result.tracks will be empty)
# ERROR - the track encountered an exception during loading
if results.load_type == LoadType.EMPTY:
return await ctx.send("I couldn'\t find any tracks for that query.")
elif results.load_type == LoadType.PLAYLIST:
tracks = results.tracks
# Add all of the tracks from the playlist to the queue.
for track in tracks:
# requester isn't necessary but it helps keep track of who queued what.
# You can store additional metadata by passing it as a kwarg (i.e. key=value)
player.add(track=track, requester=ctx.author.id)
embed.title = 'Playlist Enqueued!'
embed.description = f'{results.playlist_info.name} - {len(tracks)} tracks'
else:
track = results.tracks[0]
embed.title = 'Track Enqueued'
embed.description = f'[{track.title}]({track.uri})'
# requester isn't necessary but it helps keep track of who queued what.
# You can store additional metadata by passing it as a kwarg (i.e. key=value)
player.add(track=track, requester=ctx.author.id)
await ctx.send(embed=embed)
# We don't want to call .play() if the player is playing as that will effectively skip
# the current track.
if not player.is_playing:
await player.play()
@commands.command(aliases=['lp'])
@commands.check(create_player)
async def lowpass(self, ctx, strength: float):
""" Sets the strength of the low pass filter. """
# Get the player for this guild from cache.
player = self.bot.lavalink.player_manager.get(ctx.guild.id)
# This enforces that strength should be a minimum of 0.
# There's no upper limit on this filter.
strength = max(0.0, strength)
# Even though there's no upper limit, we will enforce one anyway to prevent
# extreme values from being entered. This will enforce a maximum of 100.
strength = min(100, strength)
embed = discord.Embed(color=discord.Color.blurple(), title='Low Pass Filter')
# A strength of 0 effectively means this filter won't function, so we can disable it.
if strength == 0.0:
await player.remove_filter('lowpass')
embed.description = 'Disabled **Low Pass Filter**'
return await ctx.send(embed=embed)
# Lets create our filter.
low_pass = LowPass()
low_pass.update(smoothing=strength) # Set the filter strength to the user's desired level.
# This applies our filter. If the filter is already enabled on the player, then this will
# just overwrite the filter with the new values.
await player.set_filter(low_pass)
embed.description = f'Set **Low Pass Filter** strength to {strength}.'
await ctx.send(embed=embed)
@commands.command(aliases=['dc'])
@commands.check(create_player)
async def disconnect(self, ctx):
""" Disconnects the player from the voice channel and clears its queue. """
player = self.bot.lavalink.player_manager.get(ctx.guild.id)
# The necessary voice channel checks are handled in "create_player."
# We don't need to duplicate code checking them again.
# Clear the queue to ensure old tracks don't start playing
# when someone else queues something.
player.queue.clear()
# Stop the current track so Lavalink consumes less resources.
await player.stop()
# Disconnect from the voice channel.
await ctx.voice_client.disconnect(force=True)
await ctx.send('✳ | Disconnected.')
def setup(bot):
bot.add_cog(Music(bot))