python - 如何让类似节奏的机器人在加入语音频道时播放音乐?
问题描述
是否可以让音乐不和谐机器人播放特定歌曲(来自 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)
解决方案
推荐阅读
- sql - Docker-compose 与 H2 docker 数据库的 setup.sql 文件
- css - 用于交互式水平滚动网站的最佳软件是什么?
- basic - 如何在 BASIC V2 中“操作”字符串?
- r - R - 从数据帧中提取时间独立的事件
- javascript - 从ajax调用的结果向传单地图添加标记-未捕获的TypeError:无法读取null的属性'lat'
- c# - Metatrader4 C# 桥接器
- r - 通过闪亮的应用程序渲染word文档,奇怪的行为
- javascript - 复制深层嵌套对象但省略特定键,其中键也是一个数组
- mysql - 如何通过 createQueryBuilder() NESTJS MySql typeORM 验证 typeorm 更新
- sharepoint-2013 - 未找到托管在 Sharepoint 2013 上的 WCF 端点