X
X
x4zx2021-07-31 01:25:07
Python
x4zx, 2021-07-31 01:25:07

How to make two commands in one?

Hello, there are two commands `connect` and `play` the code below, and I need to make sure that when the `play` command is used, the bot joins the channel in which the author is located and has already played there, and accordingly, if the author of the command is not in the channel, then issue an error

@commands.command()
    async def connect(self, ctx, *, channel: discord.VoiceChannel=None):
        """Connect to voice.
        Parameters
        ------------
        channel: discord.VoiceChannel [Optional]
            The channel to connect to. If a channel is not specified, an attempt to join the voice channel you are in
            will be made.
        This command also handles moving the bot to different channels.
        """
        if not channel:
            try:
                channel = ctx.author.voice.channel
            except AttributeError:
                embed = discord.Embed(title="", description="No channel to join. Please call `,join` from a voice channel.", color=discord.Color.green())
                await ctx.send(embed=embed)
                raise InvalidVoiceChannel('No channel to join. Please either specify a valid channel or join one.')

        vc = ctx.voice_client

        if vc:
            if vc.channel.id == channel.id:
                return
            try:
                await vc.move_to(channel)
            except asyncio.TimeoutError:
                raise VoiceConnectionError(f'Moving to channel: <{channel}> timed out.')
        else:
            try:
                await channel.connect()
            except asyncio.TimeoutError:
                raise VoiceConnectionError(f'Connecting to channel: <{channel}> timed out.')
        if (random.randint(0, 1) == 0):
            await ctx.message.add_reaction('')
        await ctx.send(f'**Joined `{channel}`**')

    @commands.command()
    async def play(self, ctx, *, search: str, channel: discord.VoiceChannel = None):
        
        """Request a song and add it to the queue.
        This command attempts to join a valid voice channel if the bot is not already in one.
        Uses YTDL to automatically search and retrieve a song.
        Parameters
        ------------
        search: str [Required]
            The song to search and retrieve using YTDL. This could be a simple search, an ID or URL.
        """

        if not channel:
            channel = ctx.author.voice.channel

        await ctx.trigger_typing()

        vc = ctx.voice_client

        if not vc:
            await ctx.invoke(self.connect_)

        player = self.get_player(ctx)

        # If download is False, source will be a dict which will be used later to regather the stream.
        # If download is True, source will be a discord.FFmpegPCMAudio with a VolumeTransformer.
        source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop, download=False)

        await player.queue.put(source)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sasha Filinsky, 2021-08-02
@UkaUkaa

The bot needs to remember who called it, and already through the on_voice_state_update event, check whether the member (the one who called the bot) did not leave the channel from which he called

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question