Add voice, Change yml, Change media

This commit is contained in:
CramMK
2020-02-13 17:05:21 +01:00
parent 05d2829bae
commit 3ac095ec21
12 changed files with 122 additions and 64 deletions

View File

@@ -5,7 +5,7 @@ Make Aqua be able to join voice channel and play audio:
- play
https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html
https://stackoverflow.com/questions/56031159/discord-py-rewrite-what-is-the-source-for-youtubedl-to-play-music
https://stackoverflow.com/questions/56060614/how-to-make-a-discord-bot-play-youtube-audio
"""
# IMPORTS
@@ -59,38 +59,80 @@ class Voice(commands.Cog):
await ctx.send("I'm not connected to a channel!")
# Begin of YouTube Player
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(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'
}
ffmpeg_options = {
'options': '-vn'
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = data.get('url')
# Maybe i can make a fancy embed out of this?
self.uploader = data.get('uploader')
self.uploader_url = data.get('uploader_url')
date = data.get('upload_date')
self.upload_date = date[6:8] + '.' + date[4:6] + '.' + date[0:4]
self.title = data.get('title')
self.thumbnail = data.get('thumbnail')
self.description = data.get('description')
self.duration = self.parse_duration(int(data.get('duration')))
self.tags = data.get('tags')
self.url = data.get('webpage_url')
self.views = data.get('view_count')
self.likes = data.get('like_count')
self.dislikes = data.get('dislike_count')
self.stream_url = data.get('url')
@classmethod
async def from_url(cls, url, *, loop=None, stream=False):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(
None,
lambda: ytdl.extract_info(url, download=not stream))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
@commands.command(name="play", aliases=["p"])
@commands.guild_only()
async def play(self, ctx, url: str):
"""
Plays music from YT link specifies
Plays music from YouTube
"""
# TODO
try:
if os.path.isfile("song.mp3"):
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the current song to end or use the `stop`command")
return
voice = get(bot.voice_clients, guild=ctx.guild)
youtube_dl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YouTubeDL(youtube_dl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"))
voice.volume=25
voice.is_playing()
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop)
ctx.voice.play(
player,
after=lambda e: print('Player error: %s' % e) if e else None))
await ctx.send(f"Now playing: {player.title}")
# End of YouTube Player
# COG ENDING