Finalize clips

This commit is contained in:
Marco Thomas
2021-05-23 20:41:40 +02:00
parent 46ae1ba259
commit 492587fb9b
3 changed files with 54 additions and 23 deletions

View File

@@ -3,7 +3,7 @@ FROM python:3.8-alpine
WORKDIR /data WORKDIR /data
COPY . /data COPY . /data
RUN apk add gcc libc-dev \ RUN apk add gcc libc-dev libffi-dev make \
&& python3 -m pip install -r requirements.txt \ && python3 -m pip install -r requirements.txt \
&& apk del gcc libc-dev && apk del gcc libc-dev

View File

@@ -36,6 +36,7 @@ bot = commands.Bot(command_prefix=PREFIX, description="Holy Goddess Aqua!")
# Preloaded Cogs # Preloaded Cogs
cogs = ["cogs.admin", cogs = ["cogs.admin",
"cogs.audio",
"cogs.help", "cogs.help",
"cogs.jikan", "cogs.jikan",
"cogs.meme", "cogs.meme",

View File

@@ -5,7 +5,10 @@ Play some audio samples
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord import FFmpegPCMAudio from discord import FFmpegPCMAudio
from discord import VoiceClient, VoiceChannel
import asyncio
from asyncio import Lock from asyncio import Lock
import youtube_dl
class Player: class Player:
@@ -19,18 +22,10 @@ class Player:
self.__class__.lock.release() self.__class__.lock.release()
@staticmethod @staticmethod
async def play(vc, audio_source): async def connect(bot, voice_channel):
vc.play(audio_source) """
while vc.is_playing(): Let's the bot join a VC
await asyncio.sleep(0.1) """
await vc.disconnect()
class Audio(commands.Cog):
def __init__(self, bot):
self.bot = bot
async def connect(bot: Bot, voice_channel: VoiceChannel) -> VoiceClient:
assert isinstance(voice_channel, VoiceChannel) assert isinstance(voice_channel, VoiceChannel)
if bot.voice_clients: if bot.voice_clients:
vc: VoiceClient = bot.voice_clients[0] vc: VoiceClient = bot.voice_clients[0]
@@ -43,15 +38,46 @@ class Audio(commands.Cog):
await asyncio.sleep(0.3) await asyncio.sleep(0.3)
return vc return vc
def parse(query): @staticmethod
memes = {"schokobons": "https://www.youtube.com/watch?v=qX7V3EVr1BA"} async def disconnect(vc):
await vc.disconnect()
@staticmethod
async def play(vc, audio_source):
vc.play(audio_source)
while vc.is_playing():
await asyncio.sleep(0.1)
class Audio(commands.Cog):
def __init__(self, bot):
self.bot = bot
def parse(self, query):
"""
Parse a request into an uri
"""
memes = {"schokobons": "https://www.youtube.com/watch?v=qX7V3EVr1BA",
"raus": "https://youtu.be/_e1cWuQ8WQw"}
try: try:
return memes[query] return memes[query]
except KeyError as e: except KeyError as e:
return None return None
@commands.command(name="dc")
async def disconnect(self, ctx):
"""
Disconnect from audio
"""
await Player.disconnect(self.bot.voice_clients[0])
@commands.command(name="play") @commands.command(name="play")
async def play(self, ctx, query): async def play(self, ctx, query):
"""
Play an audio clip
- schokobons
- raus
"""
ydl_opts = { ydl_opts = {
'format': 'bestaudio/best', 'format': 'bestaudio/best',
@@ -62,15 +88,19 @@ class Audio(commands.Cog):
}], }],
} }
uri = parse(query) uri = self.parse(query)
if uri is None:
await ctx.send("Clip not found!")
else:
with youtube_dl.YoutubeDL(ydl_opts) as ydl: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
song_info = ydl.extract_info(uri, download=False) song_info = ydl.extract_info(uri, download=False)
audio_stream = song_info["formats"][0]["url"] audio_stream = song_info["formats"][0]["url"]
audio_source = FFmpegPCMAudio(audio_stream) audio_source = FFmpegPCMAudio(audio_stream)
async with self.lock as player: async with Player() as player:
vc = await connect(self.bot, member.voice.channel) vc = await player.connect(self.bot, ctx.author.voice.channel)
await player.play(vc, audio_source) await player.play(vc, audio_source)
await ctx.send(f"Playing: {uri}")
def setup(bot): def setup(bot):