【问题标题】:Discord.py @bot cannot play audioDiscord.py @bot 无法播放音频
【发布时间】:2021-09-28 16:37:04
【问题描述】:

看看这段代码:

async def playSound(ctx, channel, file, bot, connected):  
    ctx = ctx.message
    vc = await connected.channel.connect()
    await ctx.channel.send(embed = returnembed(preset = 'playingSound', info = str(channel)))
    player = vc.create_ffmpeg_player(file)
    player.start()
    while not player.is_done():
        await asyncio.sleep(1)
    player.stop()
    await vc.disconnect()
    await os.remove(file)

Notes:
ctx = ...well umm context... more specifically:
@bot.command(pass_context = True)
async def foo(ctx):
    pass
connected = ctx.author.voice
file = WAV_FILENAME
bot = commands.Bot(command_prefix= settings['callbotprefix_str'])
channel = ctx.author.voice.channel

(注意:当我测试这个时,我确实在一个语音通道中。)

我正在尝试在语音通道中播放本地音频文件。虽然机器人每次都成功加入语音频道,并且我可以确认文件没有损坏,但我收到了一个非常“pleating”的异常:

Ignoring exception in command foo:
Traceback (most recent call last):
  File "discord_package", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "bot_file", line 224, in tts
    await playsound(message, connected.channel, fileName, bot, connected)
  File "bot_file", line 318, in playsound
    player = vc.create_ffmpeg_player(file)
AttributeError: 'VoiceClient' object has no attribute 'create_ffmpeg_player'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "discord_package", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "whatever_the_core_thing_is", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "the_core_thing_again", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceClient' object has no attribute 'create_ffmpeg_player'

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    试试这个,

    在vc中使用discord.FFmpegPCMAudio()播放音频

    import discord
    
    async def playSound(ctx, channel, file, bot, connected):  
        # ctx = ctx.message # i dont know what its doing, so i commented it out
        vc = await connected.channel.connect()
        await ctx.channel.send(embed = returnembed(preset = 'playingSound', info = str(channel)))
        vc.play(discord.FFmpegPCMAudio(file))
        while vc.is_playing():
            await asyncio.sleep(1)
        # await vc.stop() # its gonna stop at the end, so i just commented it out too
        await vc.disconnect()
        os.remove(file)
    

    告诉我是否适合你...

    【讨论】:

    • 嘿@GhostOps,它并没有真正起作用。我得到了基本相同的错误:discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: ffmpeg was not found.
    • @AnonyMous 你的电脑里有 ffmpeg 吗?它应该默认安装...
    • 我很确定,例如,当我执行 pip install discord 时,我得到了所有软件包的 Requirement already satisfied:。我也试过pip install FFmegPCAudio,虽然pip找不到包。
    • 抱歉,感谢您的建议
    • @AnonyMous ffmpeg 不是 python 模块,它是一个包,默认在所有 pc 上都可用,FFmegPCMAudiodiscord 下的一个类。如果您知道 ffmpeg 可执行文件(即 ffmpeg.exe)的位置,您可以将文件路径传递给 executable 参数到 FFmpegPCMAudio
    猜你喜欢
    • 2021-02-06
    • 2021-11-07
    • 2020-11-12
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2021-11-18
    相关资源
    最近更新 更多