这是我的 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:未定义。这是我完成音乐机器人之前的最后一个命令(到目前为止)如果有人能回答我,我将不胜感激。谢谢!
答案 0 :(得分:0)
您将需要一个 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 对他的问题进行了重大更改。 (我也没有看到对问题进行了编辑。)