Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.75k stars 3.75k forks source link

basic_voice.py example does not work (on macOs and on Ubuntu) #9070

Open mgiugliano opened 1 year ago

mgiugliano commented 1 year ago

Summary

BOT connects to a voice channel but no sound is played

Reproduction Steps

Simply launching the sample code basic_voice.py and exploring all (BOT) commands made it clear that:

Minimal Reproducible Code

Just the basic_voice.py: it did not work for me.

Expected Results

I would have expected a .mp3 or .wav file to be played in the voice chat.

Actual Results

BOT joins the voice channel but does not play any sound (YouTube, files, stream, etc.)

Intents

intents = discord.Intents.default()

System Information

Checklist

Additional Context

After several days of struggle, I finally converged on a minimal and working example - below.

As I am not fluent in Discord.py, I would like to understand why the sample code provided by the Author of the library does not work and my "patchwork" shy attempt does.

https://gist.github.com/mgiugliano/25428789c00b8ba1c1073c3e3f2f8294

#
# Minimal code for an audio file player BOT in a voice channel
#   
# Entirely based on the example from Rapptz (that was NOT working for me)
# https://github.com/Rapptz/discord.py/blob/master/examples/basic_voice.py
#
# Nov 15 2022

import asyncio
import discord
from discord.ext import commands

from config_private import *        # Import private credentials (prefs.py)

ffmpeg_options = {              # type > ffmpeg --help, to list options
    'options': '-vn',           # disable video
}

class AudioTest(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def testme(self, ctx):
        """USAGE  !testme (joins the voice chan specified by CHANNEL, and plays a file)"""

        # This is the filename to play
        filename = './a.mp3'        # it works with *.wav files too

        channel = await self.bot.fetch_channel(CHANNEL)

        vc = await channel.connect()
        await ctx.send(f'Connected!')

        vc.play(discord.FFmpegPCMAudio(filename))
        vc.source = discord.PCMVolumeTransformer(vc.source)
        vc.source.volume = 0.5

        while vc.is_playing():
            await ctx.send(f'Still playing...')
            await asyncio.sleep(0.5)

        await vc.disconnect(force=True) 
        await ctx.send(f'Disconnected!')

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(
    command_prefix=commands.when_mentioned_or("!"),
    description='Minimal audio-file player bot example',
    intents=intents,
)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user} (ID: {bot.user.id})')
    print('------')

async def main():
    async with bot:
        await bot.add_cog(AudioTest(bot))
        await bot.start(TOKEN)

asyncio.run(main())
Subcode commented 1 year ago

It doesnt work for me either. Python 3.10.6 discord.py version 2.1.0 OS: ubuntu 22.04

Just running the example works, it joins the server. The help command also works, but /join etc do nothing. (i changed the command prefix to /. using ! also does not work)

On discord it returns: The application did not respond

uaimio commented 1 year ago

I've found the inconsistency related to this issue in lines 85 and 95 of the example: voice_client.play(...) is called before the variable player has been instantiated because the main loop calls YTDLSource.from_url after the ctx.typing() closure.

EDIT: After a couple of minutes it seems to work, there is an undefined behaviour in some way.

mgiugliano commented 1 year ago

@Rapptz May I ask you to explain why "no repro" tag is used in this case? It seems other people reported similar problems I did. Thank you for your work.

imayhaveborkedit commented 12 months ago

This issue is a bit old, but the voice connection code for the library has been rewritten. If you're still having this issue, do you mind seeing if it still happens on the latest commit?