@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 = disnake.Embed(color=disnake.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)
print
else:
print()
# 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)
view = musicbutton(self.bot)
await ctx.send(embed=embed, view=view)
# We don't want to call .play() if the player is playing as that will effectively skip
# the current track.
# Check if the player is already playing music
view = musicbutton(self.bot)
if not player.is_playing:
await player.play()
track = results['tracks'][0]
now_playing_embed = disnake.Embed(
title="Now Playing",
description=f'[{track.title}]({track.uri})',
color=disnake.Color.green()
)
await ctx.send(embed=now_playing_embed, view=view)
else:
queue_embed = disnake.Embed(
title="Track Added to Queue",
description=f'[{track.title}]({track.uri}) has been added to the queue.',
color=disnake.Color.orange()
)
await ctx.send(embed=queue_embed)```