如何让我的不和谐机器人播放YouTub的音频

2024-05-29 10:04:44 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我的问题是如何将变量url设置为有人在yt之后键入的内容?例如!yt[youtbube link]使url=[youtube link]

这是我目前的代码,任何帮助都将不胜感激,谢谢:)

@client.command(pass_context=True)
async def yt(ctx):
    url = 'https://www.youtube.com/watch?v=rPOUewuNKFE'

    author = ctx.message.author
    voice_channel = author.voice_channel
    vc = await client.join_voice_channel(voice_channel)

    player = await vc.create_ytdl_player(url)
    player.start()

Tags: clienturl内容键入youtubechannellinkawait
3条回答

我想你已经知道了,但是有一些机器人可以帮你做到这一点。如果你没有,你可以从YouTube上给它一个链接,并确保bot和你一起在语音频道。我不确定你是否想让它在多个语音频道播放不同的链接。我非常怀疑机器人能在多个语音频道播放不同的歌曲,因为它必须与你在语音频道。

@client.command(pass_context=True)
    async def yt(ctx):
        url = ctx.message.content
        url = url.strip('!yt ')

        author = ctx.message.author
        voice_channel = author.voice_channel
        vc = await client.join_voice_channel(voice_channel)

        player = await vc.create_ytdl_player(url)
        player.start()

其实很简单。只需将其作为参数添加到函数中。

@client.command(pass_context=True)
async def yt(ctx, url):

    author = ctx.message.author
    voice_channel = author.voice_channel
    vc = await client.join_voice_channel(voice_channel)

    player = await vc.create_ytdl_player(url)
    player.start()

如果愿意,还可以使用标准函数注释(async def yt(ctx, url: str):)指定参数的类型,但这并不是很有用,因为默认情况是无论如何将它们作为字符串传递。

相关问题 更多 >

    热门问题