【问题标题】:Discord.py error : object NoneType can't be used in "await" expression when trying to make the bot leave a voice channelDiscord.py 错误:当试图让机器人离开语音通道时,对象 NoneType 不能用于“等待”表达式
【发布时间】:2020-09-06 17:22:56
【问题描述】:

我目前正在尝试在 python 中编写 discord 机器人,并尝试使其加入频道、播放声音然后离开。

我想出了这个代码:

@bot.command()
async def sound(ctx):
    channel = ctx.author.voice.channel
    await join(ctx)
    voice = get(bot.voice_clients, guild=ctx.guild)
    source = FFmpegPCMAudio('sound.mp3')
    await voice.play(source)
    await channel.disconnect()

但是,当我尝试它时,播放声音后,它并没有断开连接,并且在我的 shell 中有一个错误:

discord.ext.commands.errors.CommandInvokeError:命令引发了 异常:TypeError:对象 NoneType 不能在“等待”中使用 表达

我在论坛上看到这可能是异步问题,但我不知道如何解决。

有人可以帮帮我吗?

编辑:我的程序中的导入:

import discord
import asyncio
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio

编辑 v2:join() 命令:

@bot.command(pass_context=True)
async def join(ctx):
    channel = ctx.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)
    if not channel:
        await ctx.send("You are not in a vocal channel.")
        return
    if voice and voice.is_connected():
        await voice.move_to(channel)
    elif voice == channel:
        return
    else:
        await channel.connect()

【问题讨论】:

  • 请更新您的问题以包含运行此问题所需的所有代码。例如,您有 await join(ctx) 但不是 join 所做的,因为它不包括在内。这只是一个猜测,但请尝试await voice.disconnet()
  • 嘿,首先,感谢您抽出宝贵时间。我用底部的 join() 函数编辑了我的帖子。我也尝试使用 voice.disconnect() 运行我的代码,但我最终得到了相同的错误消息
  • 它是否也显示错误发生在哪一行?
  • 错误出现在“await channel.disconnect()”行

标签: python python-3.x python-asyncio discord.py discord.py-rewrite


【解决方案1】:

您可以让您的join 命令返回VoiceClient 对象并直接使用它来播放声音,而不是使用discord.utils.get。在检查机器人是否已连接到语音通道时,您还可以直接从bot.voice_clients 列表中获取VoiceClient 对象,而不是使用get

请注意,voice.play 不需要 await,因为它不是协程。你还需要在断开之前检查bot是否停止使用while voice.is_playing()播放音频,否则它会在开始播放声音后立即断开。

from discord.ext import commands
from discord import FFmpegPCMAudio

bot = commands.Bot(command_prefix='?')


@bot.command()
async def join(ctx):
    channel = ctx.author.voice.channel

    voice = None
    for vc in bot.voice_clients:
        if vc.guild == ctx.guild:
            voice = vc

    if not channel:
        await ctx.send("You are not in a vocal channel.")
        return
    if voice and voice.is_connected():
        vc = await voice.move_to(channel)
    elif voice == channel:
        return
    else:
        vc = await channel.connect()
    return vc


@bot.command()
async def sound(ctx):
    voice = await join(ctx)
    source = FFmpegPCMAudio('sound.m4a')
    voice.play(source)
    while voice.is_playing():
        continue
    await voice.disconnect()

bot.run('token')

【讨论】:

    【解决方案2】:

    while voice.is_playing():
            continue
        await voice.disconnect()
    

    会弄乱音频。

    改为这样做:

    while voice.is_playing():
            time.sleep(1)
    

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 2019-04-08
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2021-09-28
      • 2021-04-24
      相关资源
      最近更新 更多