使音乐机器人不和谐。引发错误的代码:
import config
import discord
import youtube_dl
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
player = {}
@client.event
async def on_ready():
print('Logged on as BOT!')
@client.command(pass_context=True)
async def join(ctx):
voice_channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(voice_channel)
print("Joined channel" + str(voice_channel))
我尝试更改了voice_channel = ctx.message.author.voice.voice_channel的值 voice_channel = ctx.message.author.voice.channel
使用命令.join时,在控制台中出现以下问题:
Ignoring exception in command join:
Traceback (most recent call last):
File "C:\Users\diram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
ret = await coro(*args, **kwargs)
File "music.py", line 15, in join
voice_channel = ctx.message.author.voice.voice_channel
AttributeError: 'VoiceState' object has no attribute 'voice_channel'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\diram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\diram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\diram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceState' object has no attribute 'voice_channel'
答案 0 :(得分:0)
请改为尝试voice_channel = ctx.message.author.voice.channel
。根据文档https://discordpy.readthedocs.io/en/latest/api.html,它是.channel
,而不是.voice_channel
。