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}")
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 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
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, **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.")
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 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