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

Commit things

This commit is contained in:
Steffo 2019-11-18 19:43:55 +01:00
parent 82a662bd03
commit 7bbf7f334b
9 changed files with 123 additions and 10 deletions

View file

@ -6,6 +6,7 @@ Welcome to the documentation of Royalnet!
.. toctree:: .. toctree::
:maxdepth: 5 :maxdepth: 5
randomdiscoveries
apireference apireference

View file

@ -0,0 +1,13 @@
Random discoveries
==================
Here are some things that were found out while developing the bot.
Discord websocket undocumented error codes
------------------------------------------
====== ===================
Code Reason
====== ===================
1006 Heartbeat stopped
====== ===================

View file

@ -1,7 +1,16 @@
from .ytdlinfo import YtdlInfo from .ytdlinfo import YtdlInfo
from .ytdlfile import YtdlFile from .ytdlfile import YtdlFile
from .ytdlmp3 import YtdlMp3 from .ytdlmp3 import YtdlMp3
from .ytdldiscord import YtdlDiscord
from .errors import * from .errors import *
from .discordbard import DiscordBard
from . import implementations
try:
from .fileaudiosource import FileAudioSource
except ImportError:
FileAudioSource = None
__all__ = [ __all__ = [
"YtdlInfo", "YtdlInfo",
@ -11,4 +20,7 @@ __all__ = [
"YtdlError", "YtdlError",
"NotFoundError", "NotFoundError",
"MultipleFilesError", "MultipleFilesError",
"FileAudioSource",
"implementations",
"DiscordBard",
] ]

View file

@ -18,10 +18,10 @@ class DiscordBard:
"""The :class:`YtdlDiscord` that's currently being played.""" """The :class:`YtdlDiscord` that's currently being played."""
self.generator: \ self.generator: \
AsyncGenerator[FileAudioSource, Tuple[Tuple[Any, ...], Dict[str, Any]]] = self._generate_generator() AsyncGenerator[FileAudioSource, Tuple[Tuple[Any, ...], Dict[str, Any]]] = self._generator()
"""The AsyncGenerator responsible for deciding the next song that should be played.""" """The AsyncGenerator responsible for deciding the next song that should be played."""
async def _generate_generator(self) -> AsyncGenerator[FileAudioSource, Tuple[Tuple[Any, ...], Dict[str, Any]]]: async def _generator(self) -> AsyncGenerator[Optional[FileAudioSource], Tuple[Tuple[Any, ...], Dict[str, Any]]]:
"""Create an async generator that returns the next source to be played; """Create an async generator that returns the next source to be played;
it can take a args+kwargs tuple in input to optionally select a different source. it can take a args+kwargs tuple in input to optionally select a different source.
@ -46,7 +46,7 @@ class DiscordBard:
self.now_playing = fas self.now_playing = fas
return fas return fas
async def add(self, ytd: YtdlDiscord): async def add(self, ytd: YtdlDiscord) -> None:
"""Add a new :class:`YtdlDiscord` to the :class:`DiscordBard`, if possible. """Add a new :class:`YtdlDiscord` to the :class:`DiscordBard`, if possible.
Raises: Raises:
@ -62,7 +62,7 @@ class DiscordBard:
""" """
raise UnsupportedError() raise UnsupportedError()
async def remove(self, ytd: YtdlDiscord): async def remove(self, ytd: YtdlDiscord) -> None:
"""Remove a :class:`YtdlDiscord` from the :class:`DiscordBard`, if possible. """Remove a :class:`YtdlDiscord` from the :class:`DiscordBard`, if possible.
Raises: Raises:
@ -70,7 +70,14 @@ class DiscordBard:
""" """
raise UnsupportedError() raise UnsupportedError()
async def cleanup(self): async def cleanup(self) -> None:
"""Enqueue the deletion of all :class:`YtdlDiscord` contained in the :class:`DiscordBard`, and return only once """Enqueue the deletion of all :class:`YtdlDiscord` contained in the :class:`DiscordBard`, and return only once
all deletions are complete.""" all deletions are complete."""
raise NotImplementedError() raise NotImplementedError()
async def length(self) -> int:
"""Return the length of the :class:`DiscordBard`.
Raises:
UnsupportedError: If :meth:`.peek` is unsupported."""
return len(await self.peek())

View file

@ -15,4 +15,4 @@ class MultipleFilesError(YtdlError):
class UnsupportedError(BardError): class UnsupportedError(BardError):
"""The method you tried to call on a :class:`DiscordBard` is not supported on that particular Bard.""" """The method you tried to call on a :class:`DiscordBard` is not supported on that particular Bard."""

View file

@ -0,0 +1,7 @@
from .dbstack import DBStack
from .dbqueue import DBQueue
__all__ = [
"DBStack",
"DBQueue",
]

View file

@ -0,0 +1,37 @@
from ..fileaudiosource import FileAudioSource
from ..discordbard import DiscordBard
from ..ytdldiscord import YtdlDiscord
from typing import List, AsyncGenerator, Tuple, Any, Dict, Optional
class DBQueue(DiscordBard):
"""A First-In-First-Out music queue.
It is what was once called a ``playlist``."""
def __init__(self):
super().__init__()
self.list: List[YtdlDiscord] = []
async def _generator(self) -> AsyncGenerator[Optional[FileAudioSource], Tuple[Tuple[Any, ...], Dict[str, Any]]]:
yield
while True:
try:
ytd = self.list.pop(0)
except IndexError:
yield None
else:
async with ytd.spawn_audiosource() as fas:
yield fas
async def add(self, ytd: YtdlDiscord):
self.list.append(ytd)
async def peek(self) -> List[YtdlDiscord]:
return self.list
async def remove(self, ytd: YtdlDiscord):
self.list.remove(ytd)
async def cleanup(self) -> None:
for ytd in self.list:
await ytd.delete_asap()

View file

@ -0,0 +1,37 @@
from ..fileaudiosource import FileAudioSource
from ..discordbard import DiscordBard
from ..ytdldiscord import YtdlDiscord
from typing import List, AsyncGenerator, Tuple, Any, Dict, Optional
class DBStack(DiscordBard):
"""A First-In-Last-Out music queue.
Not really sure if it is going to be useful..."""
def __init__(self):
super().__init__()
self.list: List[YtdlDiscord] = []
async def _generator(self) -> AsyncGenerator[Optional[FileAudioSource], Tuple[Tuple[Any, ...], Dict[str, Any]]]:
yield
while True:
try:
ytd = self.list.pop()
except IndexError:
yield None
else:
async with ytd.spawn_audiosource() as fas:
yield fas
async def add(self, ytd: YtdlDiscord):
self.list.append(ytd)
async def peek(self) -> List[YtdlDiscord]:
return self.list
async def remove(self, ytd: YtdlDiscord):
self.list.remove(ytd)
async def cleanup(self) -> None:
for ytd in self.list:
await ytd.delete_asap()

View file

@ -7,10 +7,9 @@ from .ytdlinfo import YtdlInfo
from .ytdlfile import YtdlFile from .ytdlfile import YtdlFile
try: try:
import discord from royalnet.bard.fileaudiosource import FileAudioSource
from royalnet.serf.discord import FileAudioSource
except ImportError: except ImportError:
discord = None FileAudioSource = None
try: try:
import ffmpeg import ffmpeg
@ -71,7 +70,7 @@ class YtdlDiscord:
@asynccontextmanager @asynccontextmanager
async def spawn_audiosource(self): async def spawn_audiosource(self):
if discord is None: if FileAudioSource is None:
raise ImportError("'discord' extra is not installed") raise ImportError("'discord' extra is not installed")
await self.convert_to_pcm() await self.convert_to_pcm()
with open(self.pcm_filename, "rb") as stream: with open(self.pcm_filename, "rb") as stream: