我找不到解决方案,所以我来了。我在discord.py中为我的discordserver重写了一个机器人,我想实现一个功能,当用户加入语音频道时,我的机器人会说一些话。
我甚至无法让机器人加入语音聊天,因为它会出现所有可能的错误。提前谢谢。
这些是我尝试过的:
@bot.command()
async def join_voice(self, ctx):
connected = ctx.author.voice
if connected:
await connected.channel.connect()
和这个
@bot.command(pass_context=True)
async def join(ctx):
author = ctx.message.author
channel = author.voice_channel
await bot.join_voice_channel(channel)
他们都给出了这个错误
discord.ext.commands.errors.CommandInvokeError: Command raised an exception:
AttributeError: 'Member' object has no attribute 'voice_channel'
解决方案
好吧,大新闻。我想好了如何连接。你需要有PyNaCl(我没有安装(。
我还调试了一下,发现作者中不存在voice_channel,我用voice.channel替换了它。
@bot.command()
async def join(ctx):
author = ctx.message.author
channel = author.voice.channel
await channel.connect()
print("i'm in the voice channel")
@bot.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
print("i'm out of the voice channel")
尝试安装带有语音支持的discord.py:
# Linux/macOS
python3 -m pip install -U "discord.py[voice]"
# Windows
py -3 -m pip install -U discord.py[voice]
将author.voice_channel
更改为author.voice.channel
在使用该命令之前,您需要加入语音频道。