pythondiscorddiscord.py

VoiceClient.play() missing 1 required positional argument: 'source' error when trying to play audio in Discord bot


I am using discord.py with the default app_commands

from discord import Intents, Client, Message, app_commands
from discord.voice_client import VoiceClient
@tree.command(
    name="play",
    description="insert a youtube url for music",
    guild = discord.Object(id=806555220468432906)
)
async def play_command(command) -> None:
    source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio("Music.m4a"))
    VoiceClient.play(source)`

This code produces the error: TypeError: VoiceClient.play() missing 1 required positional argument: 'source'

I've tried changing the encoding of FFmpeg while still getting the same error. help


Solution

  • The play() method should be called on an instance of VoiceClient rather than the class itself.

    Here is a possible fix, which gets a VoiceClient by connecting to the voice channel the user who ran the command is in (in reality you should verify whether the user is in any voice channel at all and check whether the bot is already in a VC):

    @tree.command(
        name="play",
        description="insert a youtube url for music",
        guild=discord.Object(id=806555220468432906)
    )
    async def play_command(command: discord.Interaction) -> None:
        source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio("Music.m4a"))
        voice_channel = command.user.voice.channel
        voice_client = await voice_channel.connect()
        voice_client.play(source)
        await voice_client.disconnect()