【发布时间】: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