javascript - 如何让我的音乐机器人恢复播放?
问题描述
这是我的 play.js。我不是编码员,所以我不理解大部分代码。它需要一些故障排除才能正常工作。
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: 'plays a song or whatever.',
async execute(message, args) {
const voiceChannel = message.member.voice.channel;
if(!voiceChannel) return message.channel.send('lol u need to be in a voice channel first');
const permissions = voiceChannel.permissionsFor(message.client.user);
if(!permissions.has('CONNECT')) return message.channel.send('u dont have the correct permissions smh');
if(!permissions.has('SPEAK')) return message.channel.send('u dont have the correct permissions smh');
if(!args.length) return message.channel.send('ok, but u need to tell me what u want to play');
const validURL = (str) =>{
var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
if(!regex.test(str)){
return false;
} else {
return true;
}
}
if(validURL(args[0])){
const connection = await voiceChannel.join();
const stream = ytdl(args[0], {filter: 'audioonly'});
connection.play(stream, {seek: 0, volume: 1})
.on('finish', () =>{
voiceChannel.leave();
message.channel.send('ok fine ill leave')
});
await message.reply(`ok ill play whatever that is`)
return
}
const connection = await voiceChannel.join();
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
const video = await videoFinder(args.join(' '));
if(video){
const stream = ytdl(video.url, {filter: 'audioonly'});
connection.play(stream, {seek: 0, volume: 0.5})
.on('finish', () =>{
voiceChannel.leave();
});
await message.reply(`ok im now playing ***${video.title}***`)
} else {
message.channel.send('lol i cant find the video ur looking for.')
}
}
}
这是我的 pause.js。我对这个有一点问题,但我花了一段时间和一些搜索来解决它。
name: 'pause',
description: 'makes me pause or whatever',
async execute(message,args, command, client, Discord){
const ytdl = require('ytdl-core');
const stream = ytdl(args[0], {filter: 'audioonly'});
const voiceChannel = message.member.voice.channel;
const connection = await voiceChannel.join();
const streamOptions = {seek:0, volume:1}
DJ = connection.play(stream, streamOptions)
DJ.pause();
}
}
最后,我的 resume.js(它不起作用。)
name: 'resume',
description: 'makes me resume or whatever',
async execute(message,args, command, client, Discord){
const ytdl = require('ytdl-core');
const stream = ytdl(args[0], {filter: 'audioonly'});
const voiceChannel = message.member.voice.channel;
const connection = await voiceChannel.join();
const streamOptions = {seek:0, volume:1}
DJ = connection.play(stream, streamOptions)
DJ.resume();
}
}
我看了一个关于如何制作音乐机器人的教程。但是,他们没有提到如何制作播放/暂停命令。每当我运行 resume.js 时,它都会显示错误:未找到视频 ID:未定义。这是我完成音乐机器人之前的最后一个命令(到目前为止)如果有人能回答我,将不胜感激。谢谢!
解决方案
您将需要一个实例StreamDispatcher
来调用该方法。您可以尝试从消息对象中检索该实例。
const dispatcher = message.guild.voice.connection.dispatcher;
dispatcher.pause();
但请注意,某些属性可能是undefined
. 所以添加安全检查。
if (!message.guild) return;
const voiceState = message.guild.voice;
if (!voiceState || !voiceState.connection) return;
const dispatcher = voiceState.connection.dispatcher;
if (!dispatcher) return;
dispatcher.pause();
您可以使用相同的逻辑dispatcher.resume()
。
编辑: 答案与问题的第一版相匹配。在我能够回答之前,OP 对他的问题进行了重大更改。(我也没有看到对问题进行了编辑。)
推荐阅读
- linux - FC-SAN:我怎么知道有新磁盘添加到 SAN 中?
- html - 为什么我的锚填充超出了父列表项?
- android - 仅使用一个布局文件为约束布局过渡设置动画
- infinite-scroll - Bigcommerce 无限滚动在进行多面搜索后不起作用
- xml - 如何在 ATG BCC 中为用户分析创建新选项卡
- c# - WPF 垂直滚动条与文本对齐,而不是与文本框宽度对齐
- vue.js - 为什么不使用 v-model 设置日期时间?
- sql - SQL Filter Select 语句如果出现在任何列中
- html - 导航页脚页眉等标签不做任何事情?
- python - Python Selenium - 按钮未单击