1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Working bot audio!

This commit is contained in:
Steffo 2019-04-19 21:10:24 +02:00
parent 10c77a20d0
commit 9cc7ea29e0
6 changed files with 43 additions and 45 deletions

View file

@ -1,5 +1,6 @@
from .playmodes import PlayMode, Playlist, Pool from .playmodes import PlayMode, Playlist, Pool
from .youtubedl import YtdlFile, YtdlInfo from .youtubedl import YtdlFile, YtdlInfo
from .royalpcmfile import RoyalPCMFile from .royalpcmfile import RoyalPCMFile
from .royalpcmaudio import RoyalPCMAudio
__all__ = ["PlayMode", "Playlist", "Pool", "YtdlFile", "YtdlInfo", "RoyalPCMFile"] __all__ = ["PlayMode", "Playlist", "Pool", "YtdlFile", "YtdlInfo", "RoyalPCMFile", "RoyalPCMAudio"]

View file

@ -1,8 +1,7 @@
from discord import AudioSource from discord import AudioSource
from discord.opus import Encoder as OpusEncoder from discord.opus import Encoder as OpusEncoder
import typing import typing
if typing.TYPE_CHECKING: from .royalpcmfile import RoyalPCMFile
from .royalpcmfile import RoyalPCMFile
class RoyalPCMAudio(AudioSource): class RoyalPCMAudio(AudioSource):
@ -10,6 +9,11 @@ class RoyalPCMAudio(AudioSource):
self.rpf: "RoyalPCMFile" = rpf self.rpf: "RoyalPCMFile" = rpf
self._file = open(rpf.audio_filename, "rb") self._file = open(rpf.audio_filename, "rb")
@staticmethod
def create_from_url(url) -> typing.List["RoyalPCMAudio"]:
rpf_list = RoyalPCMFile.create_from_url(url)
return [RoyalPCMAudio(rpf) for rpf in rpf_list]
def cleanup(self): def cleanup(self):
self._file.close() self._file.close()

View file

@ -1,11 +1,8 @@
import discord
import ffmpeg import ffmpeg
import re
import os import os
import typing import typing
import logging as _logging import logging as _logging
from .youtubedl import YtdlFile, YtdlInfo from .youtubedl import YtdlFile, YtdlInfo
from .royalpcmaudio import RoyalPCMAudio
log = _logging.getLogger(__name__) log = _logging.getLogger(__name__)
@ -50,8 +47,6 @@ class RoyalPCMFile(YtdlFile):
def audio_filename(self): def audio_filename(self):
return f"./downloads/{self.info.title}-{self.info.id}.pcm" return f"./downloads/{self.info.title}-{self.info.id}.pcm"
def create_audio_source(self): def __del__(self):
return RoyalPCMAudio(self) log.info(f"Deleting {self.audio_filename}")
def delete_audio_file(self):
os.remove(self.audio_filename) os.remove(self.audio_filename)

View file

@ -8,7 +8,7 @@ from ..utils import asyncify, Call, Command
from ..error import UnregisteredError, NoneFoundError, TooManyFoundError, InvalidConfigError from ..error import UnregisteredError, NoneFoundError, TooManyFoundError, InvalidConfigError
from ..network import Message, RoyalnetConfig from ..network import Message, RoyalnetConfig
from ..database import DatabaseConfig from ..database import DatabaseConfig
from ..audio import PlayMode, Playlist from ..audio import PlayMode, Playlist, RoyalPCMAudio
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
log = _logging.getLogger(__name__) log = _logging.getLogger(__name__)
@ -108,6 +108,10 @@ class DiscordBot(GenericBot):
# Skip non-text messages # Skip non-text messages
if not text: if not text:
return return
# Skip bot messages
author: typing.Union[discord.User] = message.author
if author.bot:
return
# Find and clean parameters # Find and clean parameters
command_text, *parameters = text.split(" ") command_text, *parameters = text.split(" ")
# Call the command # Call the command
@ -193,35 +197,29 @@ class DiscordBot(GenericBot):
await self.client.connect() await self.client.connect()
# TODO: how to stop? # TODO: how to stop?
# class DiscordBot: async def add_to_music_data(self, url: str, guild: discord.Guild):
# async def add_to_music_data(self, url: str, guild: discord.Guild): """Add a file to the corresponding music_data object."""
# """Add a file to the corresponding music_data object.""" log.debug(f"Adding {url} to music_data of {guild}")
# log.debug(f"Downloading {url} to add to music_data") guild_music_data = self.music_data[guild]
# files: typing.List[RoyalPCMFile] = await asyncify(RoyalPCMFile.create_from_url, url) audio_sources = RoyalPCMAudio.create_from_url(url)
# guild_music_data = self.music_data[guild] for audio_source in audio_sources:
# for file in files: log.debug(f"Adding {audio_source} to music_data")
# log.debug(f"Adding {file} to music_data") guild_music_data.add(audio_source)
# guild_music_data.add(file) if guild_music_data.now_playing is None:
# if guild_music_data.now_playing is None: log.debug(f"Starting playback chain")
# log.debug(f"Starting playback chain") await self.advance_music_data(guild)
# await self.advance_music_data(guild)
# async def advance_music_data(self, guild: discord.Guild):
# async def advance_music_data(self, guild: discord.Guild): """Try to play the next song, while it exists. Otherwise, just return."""
# """Try to play the next song, while it exists. Otherwise, just return.""" guild_music_data = self.music_data[guild]
# guild_music_data = self.music_data[guild] voice_client = self.client.find_voice_client_by_guild(guild)
# voice_client = self.find_voice_client(guild) next_source: RoyalPCMAudio = await guild_music_data.next()
# next_file: RoyalPCMFile = await guild_music_data.next() if next_source is None:
# if next_file is None: log.debug(f"Ending playback chain")
# log.debug(f"Ending playback chain") return
# return
# def advance(error=None):
# def advance(error=None): loop.create_task(self.advance_music_data(guild))
# log.debug(f"Deleting {next_file}")
# next_file.delete_audio_file() log.debug(f"Starting playback of {next_source}")
# loop.create_task(self.advance_music_data(guild)) voice_client.play(next_source, after=advance)
#
# log.debug(f"Creating AudioSource of {next_file}")
# next_source = next_file.create_audio_source()
# log.debug(f"Starting playback of {next_source}")
# voice_client.play(next_source, after=advance)
#

View file

@ -21,7 +21,7 @@ class PlayNH(NetworkHandler):
message_type = PlayMessage message_type = PlayMessage
@classmethod @classmethod
async def nh_play(cls, bot: "DiscordBot", message: PlayMessage): async def discord(cls, bot: "DiscordBot", message: PlayMessage):
"""Handle a play Royalnet request. That is, add audio to a PlayMode.""" """Handle a play Royalnet request. That is, add audio to a PlayMode."""
# Find the matching guild # Find the matching guild
if message.guild_identifier: if message.guild_identifier:

View file

@ -48,7 +48,7 @@ class SummonCommand(Command):
@classmethod @classmethod
async def discord(cls, call: Call): async def discord(cls, call: Call):
bot = call.interface_obj.bot bot = call.interface_obj.client
message: discord.Message = call.kwargs["message"] message: discord.Message = call.kwargs["message"]
channel_name: str = call.args.optional(0) channel_name: str = call.args.optional(0)
if channel_name: if channel_name: