async def run( self, *_, ctx: Context, next: Callable, volume: Optional[str] = None, **kw, ): # noqa: D102 state = MiddlewareState.get_state(ctx, State) astate = MiddlewareState.get_state(ctx, audio.State) if state is None or astate is None: return channel = ctx.kwargs["message"].channel audio_state = astate.get_audio_state(channel.guild) player = state.get_player(audio_state) if volume is not None: try: player.volume = float(volume) except ValueError: await channel.send("Only float values are possible.") return # await channel.send(f"Player volume is set to {player.volume}")
def _get_state(ctx: Context) -> EventNormalizationContextState: state = MiddlewareState.get_state(ctx, EventNormalizationContextState) if state is None: state = EventNormalizationContextState() MiddlewareState.set_state(ctx, state) # return state
def _get_state(ctx: Context) -> CommandContextState: state = MiddlewareState.get_state(ctx, CommandContextState) if state is None: state = CommandContextState() MiddlewareState.set_state(ctx, state) # return state
async def run( self, *_, ctx: Context, next: Callable, url: Optional[str] = None, extractor: str = "youtube-dl", **kw, ): # noqa: D102 state = MiddlewareState.get_state(ctx, State) astate = MiddlewareState.get_state(ctx, audio.State) if state is None or astate is None: return channel = ctx.kwargs["message"].channel audio_state = astate.get_audio_state(channel.guild) player = state.get_player(audio_state) if url is None and len(player.playlist.entries) == 0: await channel.send("Provide URL to play.") return elif url: if extractor not in state.extractors: await channel.send("Extractor not found.") return # try: playlist = await state.extractors[extractor].extract( url, ctx.client.loop) player.stop() player.set_playlist(playlist) except UnsupportedURLError: await channel.send("Provided URL is not supported.") return except EmptyStreamError: await channel.send("Nothing to play found by provided URL.") return except PlayerExtensionError: await channel.send("Error during resolving provided URL.") return # if audio_state.voice_client is None: await channel.send("I'm not connected to voice channel.") return # player.play() await channel.send("Playing...")
async def test_running_behaviour_with_class_state(context, sample_parameters): sa, skwa = sample_parameters class State: pass ms = MiddlewareState(State) assert ms.state == State async def next(*args, ctx, **kwargs): assert ctx == context and list(args) == sa and kwargs == skwa assert isinstance(MiddlewareState.get_state(ctx, State), State) return 42 assert await ms.run(*sa, ctx=context, next=next, **skwa) == 42 assert isinstance(ms.state, State) prev_state = ms.state async def next(*args, ctx, **kwargs): assert ctx == context and list(args) == sa and kwargs == skwa assert MiddlewareState.get_state(ctx, State) == prev_state return 42 assert await ms.run(*sa, ctx=context, next=next, **skwa) == 42
async def next(*args, ctx, my_state, **kwargs): assert ctx == context and list(args) == sa and kwargs == skwa assert hasattr(ctx, "states") assert ctx.states.get(State) == state assert MiddlewareState.get_state(ctx, State) == state assert my_state == state return 42
async def run(self, *_, ctx: Context, next: Callable, **kw): # noqa: D102 # State should be always present. It could be None only if middleware # tree is incorrectly built. state: State = MiddlewareState.get_state(ctx, State) if not state.initialized: state.initialized = True state.first_connect_time = pendulum.now(tz=pendulum.UTC) else: state.last_connect_time = pendulum.now(tz=pendulum.UTC)
async def run(self, *_, ctx: Context, next: Callable, **kw): # noqa: D102 state = MiddlewareState.get_state(ctx, State) astate = MiddlewareState.get_state(ctx, audio.State) if state is None or astate is None: return channel = ctx.kwargs["message"].channel audio_state = astate.get_audio_state(channel.guild) player = state.get_player(audio_state) if audio_state.voice_client is None: await channel.send("I'm not connected to voice channel.") return if player.is_stopped(): await channel.send("I'm not playing audio.") return # player.skip() await channel.send("Skipped.")
def __init__(self) -> None: super().__init__() self._state = State() self._extension_middleware = [ chain_of( [ Connect(), MiddlewareState(self._state), EventTypeFilter(EventType.CONNECT), EventNormalization(), ] ), chain_of( [ Message(), MiddlewareState(self._state), Command("status"), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ] ), ]
async def run(self, *_, ctx: Context, next: Callable, **kw): # noqa: D102 # State should be always present. It could be None only if middleware # tree is incorrectly built. client = ctx.client state: State = MiddlewareState.get_state(ctx, State) embed = discord.Embed() await self.set_author(embed, client) self.set_description(embed) self.set_extensions_info(embed, client) await self.set_counters(embed, client) self.set_uptime(embed, state) embed.timestamp = pendulum.now(tz=pendulum.UTC) await ctx.kwargs["message"].channel.send(embed=embed)
async def test_running_behaviour_on_defaults(context, sample_parameters): sa, skwa = sample_parameters class State: pass state = State() ms = MiddlewareState(state) async def next(*args, ctx, **kwargs): assert ctx == context and list(args) == sa and kwargs == skwa assert hasattr(ctx, "states") assert ctx.states.get(State) == state assert MiddlewareState.get_state(ctx, State) == state return 42 assert await ms.run(*sa, ctx=context, next=next, **skwa) == 42
def __init__(self) -> None: super().__init__() self._state = State() self._client_middleware = [MiddlewareState(self._state)] self._extension_middleware = [ chain_of([ Join(), collection_of( OneOfAll, [Command("join"), Command("connect")]), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), chain_of([ Leave(), collection_of( OneOfAll, [Command("leave"), Command("disconnect")]), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), chain_of([ Volume(), Command("volume", rest_pattern="(?P<volume>.+)?"), Command("master"), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), ]
async def next(*args, ctx, **kwargs): assert ctx == context and list(args) == sa and kwargs == skwa assert MiddlewareState.get_state(ctx, State) == prev_state return 42
async def next(*args, ctx, **kwargs): assert ctx == context and list(args) == sa and kwargs == skwa assert isinstance(MiddlewareState.get_state(ctx, State), State) return 42
def __init__(self) -> None: super().__init__() self._state = State() self._extension_middleware = [ chain_of([ Play(), MiddlewareState(self._state), Command("play", rest_pattern="(?P<url>.+)?"), Command("player"), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), chain_of([ Pause(), MiddlewareState(self._state), Command("pause"), Command("player"), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), chain_of([ Resume(), MiddlewareState(self._state), Command("resume"), Command("player"), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), chain_of([ Stop(), MiddlewareState(self._state), Command("stop"), Command("player"), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), chain_of([ Skip(), MiddlewareState(self._state), Command("skip"), Command("player"), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), chain_of([ Volume(), MiddlewareState(self._state), Command("volume", rest_pattern="(?P<volume>.+)?"), Command("player"), ChannelTypeFilter(guild=True), BotFilter(authored_by_bot=False), EventTypeFilter(EventType.MESSAGE), EventNormalization(), ]), ]
def __init__(self): self._client_middleware = [MiddlewareState(FirstState), first_mw] self._extension_middleware = [ MiddlewareState(SecondState), second_mw, ]