From e11fd430d1a47fcdb44fca6ade3e867ed5088619 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 20 Nov 2019 16:55:33 +0100 Subject: [PATCH] yes --- royalnet/serf/discord/__init__.py | 2 ++ royalnet/serf/discord/discordbard/discordbard.py | 3 +++ royalnet/serf/discord/discordserf.py | 3 +++ royalnet/{bard => serf/discord}/fileaudiosource.py | 13 ++++++++----- royalnet/{bard => serf/discord}/ytdldiscord.py | 5 ++--- 5 files changed, 18 insertions(+), 8 deletions(-) rename royalnet/{bard => serf/discord}/fileaudiosource.py (78%) rename royalnet/{bard => serf/discord}/ytdldiscord.py (95%) diff --git a/royalnet/serf/discord/__init__.py b/royalnet/serf/discord/__init__.py index 6fe4e67b..05842174 100644 --- a/royalnet/serf/discord/__init__.py +++ b/royalnet/serf/discord/__init__.py @@ -1,6 +1,7 @@ from .createrichembed import create_rich_embed from .escape import escape from .discordserf import DiscordSerf +from .fileaudiosource import FileAudioSource from . import discordbard __all__ = [ @@ -8,4 +9,5 @@ __all__ = [ "escape", "DiscordSerf", "discordbard", + "FileAudioSource", ] diff --git a/royalnet/serf/discord/discordbard/discordbard.py b/royalnet/serf/discord/discordbard/discordbard.py index f2645dd8..530211cd 100644 --- a/royalnet/serf/discord/discordbard/discordbard.py +++ b/royalnet/serf/discord/discordbard/discordbard.py @@ -52,6 +52,9 @@ class DiscordBard: self.now_playing = fas return fas + async def cut(self): + """Immediatly end the playback of the current file""" + async def add(self, ytd: YtdlDiscord) -> None: """Add a new :class:`YtdlDiscord` to the :class:`DiscordBard`, if possible. diff --git a/royalnet/serf/discord/discordserf.py b/royalnet/serf/discord/discordserf.py index 2de5344c..1d163a27 100644 --- a/royalnet/serf/discord/discordserf.py +++ b/royalnet/serf/discord/discordserf.py @@ -248,3 +248,6 @@ class DiscordSerf(Serf): raise CommandError("The bot is already connected in another channel.\n" " Please disconnect it before resummoning!") self.bards[channel.guild] = DBQueue(voice_client=voice_client) + + async def voice_change(self, guild: "discord.Guild", bard: Type[DiscordBard]): + """Safely change the :class:`DiscordBard` for a guild.""" diff --git a/royalnet/bard/fileaudiosource.py b/royalnet/serf/discord/fileaudiosource.py similarity index 78% rename from royalnet/bard/fileaudiosource.py rename to royalnet/serf/discord/fileaudiosource.py index c0695761..3be44d40 100644 --- a/royalnet/bard/fileaudiosource.py +++ b/royalnet/serf/discord/fileaudiosource.py @@ -1,6 +1,3 @@ -from typing import Optional -from .ytdlinfo import YtdlInfo - try: import discord except ImportError: @@ -21,6 +18,7 @@ class FileAudioSource(discord.AudioSource): Arguments: file: the file to be played back.""" self.file = file + self._stopped = False def __repr__(self): if self.file.seekable(): @@ -35,13 +33,18 @@ class FileAudioSource(discord.AudioSource): ``False``.""" return False + def stop(self): + """Stop the FileAudioSource. Once stopped, a FileAudioSource will immediatly stop reading more bytes from the + file.""" + self._stopped = True + def read(self): - """Reads 20ms worth of audio. + """Reads 20ms of audio. If the stream has ended, then return an empty :py:class:`bytes`-like object.""" data: bytes = self.file.read(discord.opus.Encoder.FRAME_SIZE) # If there is no more data to be streamed - if len(data) != discord.opus.Encoder.FRAME_SIZE: + if self._stopped or len(data) != discord.opus.Encoder.FRAME_SIZE: # Return that the stream has ended return b"" return data diff --git a/royalnet/bard/ytdldiscord.py b/royalnet/serf/discord/ytdldiscord.py similarity index 95% rename from royalnet/bard/ytdldiscord.py rename to royalnet/serf/discord/ytdldiscord.py index dc28e02f..51eb86d8 100644 --- a/royalnet/bard/ytdldiscord.py +++ b/royalnet/serf/discord/ytdldiscord.py @@ -3,11 +3,10 @@ import re import os from contextlib import asynccontextmanager from royalnet.utils import asyncify, MultiLock -from .ytdlinfo import YtdlInfo -from .ytdlfile import YtdlFile +from royalnet.bard import YtdlInfo, YtdlFile try: - from royalnet.bard.fileaudiosource import FileAudioSource + from royalnet.serf.discord.fileaudiosource import FileAudioSource except ImportError: FileAudioSource = None