首页 > 解决方案 > 如何让类似节奏的机器人在加入语音频道时播放音乐?

问题描述

是否可以让音乐不和谐机器人播放特定歌曲(来自 youtube 链接),直到它有其他东西可以播放?

就上下文而言,我希望这个机器人播放“待机”,目前是指向 LBP pod 音乐的 youtube 链接。像 Rythm 这样的机器人会加入一个频道并等待有人给它一个命令来播放特定的歌曲。我希望这个机器人加入一个频道并播放一首歌曲,直到它被播放另一首歌曲。

例如:如果前缀是“.”

节奏:

.join --> Rythm 加入语音频道。

.play https://youtube.com/watch?v=BLAHBLAHBLAH --> 节奏播放 v=BLAHBLAHBLAH

我想用我的机器人实现的目标:

.join --> Bot加入语音频道并待机

待机 = https://youtube.com/watch?v=SPECIFICLINK

.play https://youtube.com/watch?v=BLAHBLAHBLAH --> Bot 停止播放待机并开始播放 v=BLAHBLAHBLAH

import discord
from discord.ext import commands
import youtube_dl
import random
from quotes import *
import time

standby = "https://www.youtube.com/watch?v=5T4wrSDaQvY"

class music(commands.Cog):
    def __init__(self, client):
        self.client = client
    
    @commands.command()
    async def join(self,ctx):
        if ctx.author.voice is None:
            await ctx.send(random.choice(notinvc))
        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)

    @commands.command()
    async def disconnect(self,ctx):
        voice_channel = ctx.author.voice.channel
        await ctx.voice_client.disconnect()
    @commands.command()
    async def leave(self,ctx):
        voice_channel = ctx.author.voice.channel
        await ctx.voice_client.disconnect()




    @commands.command()
    async def play(self,ctx,url):
        if ctx.voice_client is None:
            voice_channel = ctx.author.voice.channel
            await voice_channel.connect() 
        if ctx.voice_client is not ctx.author.voice:
            voice_channel = ctx.author.voice.channel
            time.sleep(1)
            await ctx.voice_client.move_to(voice_channel)
            await ctx.send("joining alternate vc")
            await ctx.send("i've bumped into an issue. please try again.")
            
        ctx.voice_client.stop()
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
        YDL_OPTIONS = {'format':"bestaudio"}
        vc = ctx.voice_client

        with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
            info = ydl.extract_info(url, download=False)
            url2 = info['formats'][0]['url']
            source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
            vc.play(source)

标签: pythondiscorddiscord.pycommandyoutube-dl

解决方案


推荐阅读