【问题标题】:Play Local Audio File Discord.py播放本地音频文件 Discord.py
【发布时间】:2020-12-11 02:17:38
【问题描述】:

在此处链接的类似帖子中看到了这个问题:Discord.py Bot How to play audio from local files

我对此稍作调整,但我不断收到“未使用的变量‘通道’。 这是我的代码。

async def test(ctx):
    voice_channel = ctx.author.channel
    channel = None
    if voice_channel != None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(executable="D:/FFMPEG/ffmpeg.exe", source="<file directory goes here>"))
        while vc.is_playing():
            time.sleep(.1)
        await vc.disconnect()
    else:
        await ctx.send(str(ctx.author.name) + "is not in a channel.")

【问题讨论】:

  • 使用await asyncio.sleep()而不是time.sleep,它会阻塞你的整个代码,当你播放音乐时你将无法做任何其他事情。

标签: python audio bots discord.py


【解决方案1】:

您收到的警报可能是因为您从未使用过通道变量。如果要删除警报,请执行以下操作

async def test(ctx):
    voice_channel = ctx.author.channel
    channel = None
    if voice_channel != None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(executable="D:/FFMPEG/ffmpeg.exe", source="<file directory goes here>"))
        await ctx.send("Connected to " + channel)
        while vc.is_playing():
            await asyncio.sleep(.1)
        await vc.disconnect()
    else:
        await ctx.send(str(ctx.author.name) + "is not in a channel.")

如 Łukasz Kwieciński 所述,还可以使用 asyncio。您也可以只注释掉该行,因为它在您当前的代码中没有用

【讨论】:

    猜你喜欢
    • 2020-11-12
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多