大家晚上好。我目前正在为我的朋友们编写一个Discord机器人,并且在我们的语音通道中播放YouTube URL中的音乐。我一直在使用discord.py的API参考来创建它,但是我遇到了一个令人讨厌的错误。在运行时,我看到以下内容:
AttributeError: 'Channel' object has no attribute 'create_ytdl_player'
我也确实安装了youtube-dl模块。下面是我的完整代码。谢谢您的任何评论。
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import random
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def play(ctx):
voice = ctx.message.author.voice.voice_channel
await bot.join_voice_channel(voice)
url = 'some_url'
player = await voice.create_ytdl_player(url)
bot.run(NO TOKEN 4 U)
答案 0 :(得分:1)
我认为您是在错误的对象上调用create_ytdl_player
方法。您不想在voice
上调用它,而是想在对bot.join_voice_channel
进行的异步调用的返回值上调用它。
The API reference如下所示:
voice = await client.join_voice_channel(channel)
player = await voice.create_ytdl_player('https://www.youtube.com/watch?v=d62TYemN6MQ')
player.start()
但是示例中的voice
变量与代码中的变量有所不同。您的voice
等同于示例代码的channel
。
尝试类似的操作,并添加一些新的变量名称:
voice_channel = ctx.message.author.voice.voice_channel
voice_client = await bot.join_voice_channel(voice_channel)
url = 'some_url'
player = await voice_client.create_ytdl_player(url)
您可能还需要在播放器上致电start
。