Skip to content

Instantly share code, notes, and snippets.

@adistomar
Forked from EvieePy/music.py
Last active November 27, 2022 21:57
Show Gist options
  • Save adistomar/9740faace6cdfe0b214f3f0e2fb3e096 to your computer and use it in GitHub Desktop.
Save adistomar/9740faace6cdfe0b214f3f0e2fb3e096 to your computer and use it in GitHub Desktop.
Basic music with playlist support on Rewrite
import discord
from discord.ext import commands
import random
import json
import asyncio
import itertools
import sys
import traceback
from async_timeout import timeout
from functools import partial
import youtube_dl
from youtube_dl import YoutubeDL
from paginator import Pag
import re
import lyricsgenius
from youtube_title_parse import get_artist_title
import os
invoke = False
passed = False
token = "" # genius lyrics token
genius = lyricsgenius.Genius(token)
with open('config.json') as f:
config = json.load(f)
# Suppress noise about console usage from errors
youtube_dl.utils.bug_reports_message = lambda: ''
ytdlopts = {
'format': 'bestaudio/best',
'outtmpl': 'downloads/%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # ipv6 addresses cause issues sometimes
}
ffmpegopts = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'
}
ytdl = YoutubeDL(ytdlopts)
class VoiceConnectionError(commands.CommandError):
"""Custom Exception class for connection errors."""
class InvalidVoiceChannel(VoiceConnectionError):
"""Exception for cases of invalid Voice Channels."""
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, requester):
super().__init__(source)
self.requester = requester
self.title = data.get('title')
self.web_url = data.get('webpage_url')
self.duration = data.get('duration')
try:
self.track = data.get('track')
self.artist = data.get('artist')
passed = True
except:
pass
# YTDL info dicts (data) have other useful information you might want
# https://github.com/rg3/youtube-dl/blob/master/README.md
def __getitem__(self, item: str):
"""Allows us to access attributes similar to a dict.
This is only useful when you are NOT downloading.
"""
return self.__getattribute__(item)
@classmethod
async def create_source(cls, ctx, search: str, *, loop, download=False):
loop = loop or asyncio.get_event_loop()
to_run = partial(ytdl.extract_info, url=search, download=download)
data = await loop.run_in_executor(None, to_run)
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
embed = discord.Embed(title="", description=f"Queued [{data['title']}]({data['webpage_url']}) [{ctx.author.mention}]", color=discord.Color.green())
await ctx.send(embed=embed)
if download:
source = ytdl.prepare_filename(data)
else:
if passed:
return {'webpage_url': data['webpage_url'], 'requester': ctx.author, 'title': data['title'], 'duration': data['duration'], 'track': data['track'], 'artist': data['artist']}
else:
return {'webpage_url': data['webpage_url'], 'requester': ctx.author, 'title': data['title'], 'duration': data['duration']}
return cls(discord.FFmpegPCMAudio(source), data=data, requester=ctx.author)
@classmethod
async def regather_stream(cls, data, *, loop):
"""Used for preparing a stream, instead of downloading.
Since Youtube Streaming links expire."""
loop = loop or asyncio.get_event_loop()
requester = data['requester']
to_run = partial(ytdl.extract_info, url=data['webpage_url'], download=False)
data = await loop.run_in_executor(None, to_run)
return cls(discord.FFmpegPCMAudio(data['url']), data=data, requester=requester)
class MusicPlayer:
"""A class which is assigned to each guild using the bot for Music.
This class implements a queue and loop, which allows for different guilds to listen to different playlists
simultaneously.
When the bot disconnects from the Voice it's instance will be destroyed.
"""
__slots__ = ('bot', '_guild', '_channel', '_cog', 'queue', 'next', 'current', 'np', 'volume')
def __init__(self, ctx):
self.bot = ctx.bot
self._guild = ctx.guild
self._channel = ctx.channel
self._cog = ctx.cog
self.queue = asyncio.Queue()
self.next = asyncio.Event()
self.np = None # Now playing message
self.volume = .5
self.current = None
ctx.bot.loop.create_task(self.player_loop())
async def player_loop(self):
"""Our main player loop."""
await self.bot.wait_until_ready()
while not self.bot.is_closed():
self.next.clear()
try:
# Wait for the next song. If we timeout cancel the player and disconnect...
async with timeout(300): # 5 minutes...
source = await self.queue.get()
except asyncio.TimeoutError:
return self.destroy(self._guild)
if not isinstance(source, YTDLSource):
# Source was probably a stream (not downloaded)
# So we should regather to prevent stream expiration
try:
source = await YTDLSource.regather_stream(source, loop=self.bot.loop)
except Exception as e:
await self._channel.send(f'There was an error processing your song.\n'
f'```css\n[{e}]\n```')
continue
source.volume = self.volume
self.current = source
self._guild.voice_client.play(source, after=lambda _: self.bot.loop.call_soon_threadsafe(self.next.set))
embed = discord.Embed(title="Now playing", description=f"[{source.title}]({source.web_url}) [{source.requester.mention}]", color=discord.Color.green())
self.np = await self._channel.send(embed=embed)
await self.next.wait()
# Make sure the FFmpeg process is cleaned up.
source.cleanup()
self.current = None
def destroy(self, guild):
"""Disconnect and cleanup the player."""
return self.bot.loop.create_task(self._cog.cleanup(guild))
class Music(commands.Cog):
"""Music related commands."""
__slots__ = ('bot', 'players')
def __init__(self, bot):
self.bot = bot
self.players = {}
async def cleanup(self, guild):
try:
await guild.voice_client.disconnect()
except AttributeError:
pass
try:
del self.players[guild.id]
except KeyError:
pass
async def __local_check(self, ctx):
"""A local check which applies to all commands in this cog."""
if not ctx.guild:
raise commands.NoPrivateMessage
return True
async def __error(self, ctx, error):
"""A local error handler for all errors arising from commands in this cog."""
if isinstance(error, commands.NoPrivateMessage):
try:
return await ctx.send('This command can not be used in Private Messages.')
except discord.HTTPException:
pass
elif isinstance(error, InvalidVoiceChannel):
await ctx.send('Error connecting to Voice Channel. '
'Please make sure you are in a valid channel or provide me with one')
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
def get_player(self, ctx):
"""Retrieve the guild player, or generate one."""
try:
player = self.players[ctx.guild.id]
except KeyError:
player = MusicPlayer(ctx)
self.players[ctx.guild.id] = player
return player
@commands.command(name='join', aliases=['connect', 'j'], description="connects to voice")
async def connect_(self, ctx, *, channel: discord.VoiceChannel = None):
"""Connect to voice.
Parameters
------------
channel: discord.VoiceChannel [Optional]
The channel to connect to. If a channel is not specified, an attempt to join the voice channel you are in
will be made.
This command also handles moving the bot to different channels.
"""
if not channel:
try:
channel = ctx.author.voice.channel
except AttributeError:
embed = discord.Embed(title="", description="No channel to join. Please call `,join` from a voice channel.", color=discord.Color.green())
await ctx.send(embed=embed)
raise InvalidVoiceChannel('No channel to join. Please either specify a valid channel or join one.')
elif channel and (ctx.author.id != config['my_id']):
return
vc = ctx.voice_client
if vc:
if vc.channel.id == channel.id:
return
try:
await vc.move_to(channel)
except asyncio.TimeoutError:
raise VoiceConnectionError(f'Moving to channel: <{channel}> timed out.')
else:
try:
await channel.connect()
except asyncio.TimeoutError:
raise VoiceConnectionError(f'Connecting to channel: <{channel}> timed out.')
await ctx.invoke(self.hehe)
if random.randint(0, 1) == 0:
await ctx.message.add_reaction('👍')
await ctx.send(f'**Joined `{channel}`**')
@commands.command(description="hehe")
async def hehe(self, ctx):
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="You need to be in a voice channel to use this command", color=discord.Color.green())
return await ctx.send(embed=embed)
global invoke
if invoke:
return
songs = []
if not vc.is_playing():
for filename in os.listdir('./media'):
if filename.endswith('.mp3'):
songs.append(f'media/{filename[:-4]}.mp3')
_file = random.choice(songs)
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(_file))
vc.play(source, after=lambda e: print('Player error: %s' % e) if e else None)
invoke = False
@commands.command(name='play', aliases=['sing','p'], description="streams music")
async def play_(self, ctx, *, search: str):
"""Request a song and add it to the queue.
This command attempts to join a valid voice channel if the bot is not already in one.
Uses YTDL to automatically search and retrieve a song.
Parameters
------------
search: str [Required]
The song to search and retrieve using YTDL. This could be a simple search, an ID or URL.
"""
global invoke
vc = ctx.voice_client
if ctx.author.voice is None and ctx.author.id != config['my_id']:
embed = discord.Embed(title="", description="You need to be in a voice channel to play songs", color=discord.Color.green())
return await ctx.send(embed=embed)
if not vc:
invoke = True
await ctx.invoke(self.connect_)
player = self.get_player(ctx)
await ctx.trigger_typing()
# If download is False, source will be a dict which will be used later to regather the stream.
# If download is True, source will be a discord.FFmpegPCMAudio with a VolumeTransformer.
source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop, download=False)
await player.queue.put(source)
@commands.command(name='lyrics', aliases=['lyric', 'lyrs', 'ly', 'liric', 'lirics', 'lrc', 'lr'], description="shows lyrics for current song")
async def lyrics_(self, ctx):
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I'm not connected to a voice channel", color=discord.Color.green())
return await ctx.send(embed=embed)
if not vc.is_playing():
embed = discord.Embed(title="", description="I am currently not playing anything", color=discord.Color.green())
return await ctx.send(embed=embed)
artist = vc.source.artist
track = vc.source.track
artist = re.sub('[(].*[)]', '', artist)
artist = re.sub('[[].*[]]', '', artist)
track = re.sub('[(].*[)]', '', track)
track = re.sub('[[].*[]]', '', track)
title = f"{artist} - {track}"
try:
title = title.replace('$', 's')
except:
pass
try:
print(title)
except:
pass
try:
artist, creator = get_artist_title(title)
lyric = genius.search_song(creator, artist)
lyrics = lyric.lyrics
pages = [lyrics[i:i+2000] for i in range(0, len(lyrics), 2000)]
pager = Pag(
title=f"Lyrics - {vc.source.title}",
timeout=100,
use_defaults=True,
entries=pages,
length=1
)
await pager.start(ctx)
except:
embed = discord.Embed(title="", description="lyrics not found", color=discord.Color.green())
await ctx.send(embed=embed)
@commands.command(name='pause', description="pauses music")
async def pause_(self, ctx):
"""Pause the currently playing song."""
vc = ctx.voice_client
if not vc or not vc.is_playing():
embed = discord.Embed(title="", description="I am currently not playing anything", color=discord.Color.green())
return await ctx.send(embed=embed)
elif vc.is_paused():
return
vc.pause()
await ctx.send("Paused ⏸️")
@commands.command(name='resume', description="resumes music")
async def resume_(self, ctx):
"""Resume the currently paused song."""
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I'm not connected to a voice channel", color=discord.Color.green())
return await ctx.send(embed=embed)
elif not vc.is_paused():
return
vc.resume()
await ctx.send("Resuming ⏯️")
@commands.command(name='skip', description="skips to next song in queue")
async def skip_(self, ctx):
"""Skip the song."""
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I'm not connected to a voice channel", color=discord.Color.green())
return await ctx.send(embed=embed)
if vc.is_paused():
pass
elif not vc.is_playing():
return
vc.stop()
@commands.command(name='remove', aliases=['rm', 'rem'], description="removes specified song from queue")
async def remove_(self, ctx, pos : int = None):
"""Removes specified song from queue"""
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I'm not connected to a voice channel", color=discord.Color.green())
return await ctx.send(embed=embed)
player = self.get_player(ctx)
if pos is None:
player.queue._queue.pop()
else:
try:
s = player.queue._queue[pos-1]
del player.queue._queue[pos-1]
embed = discord.Embed(title="", description=f"Removed [{s['title']}]({s['webpage_url']}) [{s['requester'].mention}]", color=discord.Color.green())
await ctx.send(embed=embed)
except:
embed = discord.Embed(title="", description=f'Could not find a track for "{pos}"', color=discord.Color.green())
await ctx.send(embed=embed)
@commands.command(name='clear', aliases=['clr', 'cl', 'cr'], description="clears entire queue")
async def clear_(self, ctx):
"""Deletes entire queue of upcoming songs."""
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I'm not connected to a voice channel", color=discord.Color.green())
return await ctx.send(embed=embed)
player = self.get_player(ctx)
player.queue._queue.clear()
await ctx.send('💣 **Cleared**')
def convert(self, seconds):
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
if hour > 0:
duration = "%dh %02dm %02ds" % (hour, minutes, seconds)
else:
duration = "%02dm %02ds" % (minutes, seconds)
return duration
@commands.command(name='queue', aliases=['q', 'playlist', 'que'], description="queues song and shows the queue")
async def queue_info(self, ctx, *, search: str = None):
"""Retrieve a basic queue of upcoming songs."""
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I'm not connected to a voice channel", color=discord.Color.green())
return await ctx.send(embed=embed)
player = self.get_player(ctx)
if search is not None:
if ctx.author.voice is None:
embed = discord.Embed(title="", description="You need to be in a voice channel to queue songs", color=discord.Color.green())
return await ctx.send(embed=embed)
await ctx.trigger_typing()
source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop, download=False)
await player.queue.put(source)
else:
pass
if player.queue.empty() and vc.is_playing():
fmt = f"\n__Now Playing__:\n[{vc.source.title}]({vc.source.web_url}) | ` {self.convert(vc.source.duration % (24*3600))} Requested by: {vc.source.requester}`\n\n**0 songs in queue**"
embed = discord.Embed(title=f'Queue for {ctx.guild.name}', description=fmt, color=discord.Color.green())
embed.set_footer(text=f"{ctx.author.display_name}", icon_url=ctx.author.avatar_url)
return await ctx.send(embed=embed)
elif player.queue.empty() and not vc.is_playing():
embed = discord.Embed(title="", description="queue is empty", color=discord.Color.green())
return await ctx.send(embed=embed)
# Grabs the songs in the queue...
upcoming = list(itertools.islice(player.queue._queue, 0, int(len(player.queue._queue))))
fmt = '\n'.join(f"`{(upcoming.index(_)) + 1}.` [{_['title']}]({_['webpage_url']}) | ` {self.convert(_['duration'] % (24*3600))} Requested by: {_['requester']}`\n" for _ in upcoming)
if len(upcoming) == 1:
song = 'song'
else:
song = 'songs'
fmt = f"\n__Now Playing__:\n[{vc.source.title}]({vc.source.web_url}) | ` {self.convert(vc.source.duration % (24*3600))} Requested by: {vc.source.requester}`\n\n__Up Next:__\n" + fmt + f"\n**{len(upcoming)} {song} in queue**"
embed = discord.Embed(title=f'Queue for {ctx.guild.name}', description=fmt, color=discord.Color.green())
embed.set_footer(text=f"{ctx.author.display_name}", icon_url=ctx.author.avatar_url)
await ctx.send(embed=embed)
@commands.command(name='np', aliases=['song', 'current', 'currentsong', 'playing'], description="shows the current playing song")
async def now_playing_(self, ctx):
"""Display information about the currently playing song."""
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I'm not connected to a voice channel", color=discord.Color.green())
return await ctx.send(embed=embed)
player = self.get_player(ctx)
if not player.current:
embed = discord.Embed(title="", description="I am currently not playing anything", color=discord.Color.green())
return await ctx.send(embed=embed)
seconds = vc.source.duration % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
if hour > 0:
duration = "%dh %02dm %02ds" % (hour, minutes, seconds)
else:
duration = "%02dm %02ds" % (minutes, seconds)
embed = discord.Embed(title="", description=f"[{vc.source.title}]({vc.source.web_url}) [{vc.source.requester.mention}] | `{duration}`", color=discord.Color.green())
embed.set_author(icon_url=self.bot.user.avatar_url, name=f"Now Playing 🎶")
await ctx.send(embed=embed)
@commands.command(name='volume', aliases=['vol', 'v'], description="changes client's volume")
async def change_volume(self, ctx, *, vol: float=None):
"""Change the player volume.
Parameters
------------
volume: float or int [Required]
The volume to set the player to in percentage. This must be between 1 and 100.
"""
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I am not currently connected to voice", color=discord.Color.green())
return await ctx.send(embed=embed)
if not vol:
embed = discord.Embed(title="", description=f"🔊 **{vc.source.volume * 100}%**", color=discord.Color.green())
return await ctx.send(embed=embed)
if not 0 < vol < 101:
embed = discord.Embed(title="", description="Please enter a value between 1 and 100", color=discord.Color.green())
return await ctx.send(embed=embed)
player = self.get_player(ctx)
if vc.source:
vc.source.volume = vol / 100
player.volume = vol / 100
embed = discord.Embed(title="", description=f'**`{ctx.author}`** set the volume to **{vol}%**', color=discord.Color.green())
await ctx.send(embed=embed)
@commands.command(name='leave', aliases=["stop", "dc", "disconnect", "bye", "gtfo"], description="stops music and disconnects from voice")
async def leave_(self, ctx):
"""Stop the currently playing song and destroy the player.
!Warning!
This will destroy the player assigned to your guild, also deleting any queued songs and settings.
"""
vc = ctx.voice_client
if not vc or not vc.is_connected():
embed = discord.Embed(title="", description="I'm not connected to a voice channel", color=discord.Color.green())
return await ctx.send(embed=embed)
if random.randint(0, 1) == 0:
await ctx.message.add_reaction('👋')
await ctx.send('**Successfully dipped**')
await self.cleanup(ctx.guild)
def setup(bot):
bot.add_cog(Music(bot))
@maj113
Copy link

maj113 commented Nov 25, 2022

The link doesn't work for me, but yes, I need to update this for the newer discord.py versions. I'm still using an older one.

My mistake, fixed

@maj113
Copy link

maj113 commented Nov 25, 2022

Replace ctx.trigger_typing with ctx.typing and avatar_url with avatar.url.

Also a bug:
for some reason it just dies while playing for no apparent reason

DEBUG:discord.player:Preparing to terminate ffmpeg process 9816.
INFO:discord.player:ffmpeg process 9816 successfully terminated with return code of 0.
DEBUG:discord.player:Preparing to terminate ffmpeg process 9816.
INFO:discord.player:ffmpeg process 9816 successfully terminated with return code of 0.

issue seems to be in the regather stream thingy since using download=True works fine

Fixed in my test branch by actually using ffmpegopts where needed

@maj113
Copy link

maj113 commented Nov 26, 2022

Also whats with ffmpegopts not being used anywhere?

Fixed in my test branch: maj113/Angel6@866bef3

@adistomar
Copy link
Author

Fixed in my test branch: maj113/Angel6@866bef3

Hey, my current music code is pretty complicated, and I haven't touched it in a long time. So, I've decided to completely replace it with the music code from your main branch (with some changes) for my own bot, since it's much more nicely written and also fixes some of the issues that my code had. Thanks a lot for your comments (and your code!) - I really appreciate it.

@adistomar
Copy link
Author

On another note, I've been looking to add Spotify music support to my bot for a while, but I haven't gotten around to actually implementing it. If you ever end up coding this for your own bot, please let me know!

@maj113
Copy link

maj113 commented Nov 27, 2022

Hey, my current music code is pretty complicated, and I haven't touched it in a long time. So, I've decided to completely replace it with the music code from your main branch (with some changes) for my own bot, since it's much more nicely written and also fixes some of the issues that my code had. Thanks a lot for your comments (and your code!) - I really appreciate it.

No problem!
The issue with Spotify playback is that it has DRM which means YT-dlp can't play it

image

it would probably need a decent amount of code refactoring and changes to get it to actually play

@adistomar
Copy link
Author

it would probably need a decent amount of code refactoring and changes to get it to actually play

Ah, I see - no worries then. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment