修复与discord.js音乐机器人,停止播放音乐之前的歌曲实际上是结束,尝试了一切



discord.js 12v

bot工作正确,唯一的问题是它停止播放,在大多数歌曲中,在它的最后三分之一部分,没有在控制台留下任何错误信息。

我已经看到,主要问题是在FFMPEG可执行文件中从网站主机获得损坏的数据包,这导致它终止。我看到另一个帖子说添加这行:

'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'

更改npm-modules/discord.js/src/client/voice/player/BasePlayer.js中的ffmpeg_arguments数组应该可以解决问题,如果不能,则将ytdl-core更改为ytdl-core-discord。我已经这样做了,音乐还在停止。

以下是play命令的代码:
const ytdl = require("ytdl-core-discord");
const ytSearch = require("yt-search");
const queue = new Map();
module.exports = {
name: "play",
aliases: ["skip", "stop"],
description: "Walter pone musica",
async execute(message, args, cmd, client, Discord){
const voice_channel = message.member.voice.channel;
if(!voice_channel) return message.channel.send("No estas en ningun canal, genio.")
const permissions = voice_channel.permissionsFor(message.client.user);
if(!permissions.has("CONNECT")) return message.channel.send("No tienes los permisos para eso pa, pide que te los den.");
if(!permissions.has("SPEAK")) return message.channel.send("No tienes los permisos para eso pa, pide que te los den.");
const server_queue = queue.get(message.guild.id);
if(cmd === "play"){
if (!args.length) return message.channel.send("Pero dime una cancion, animal.");
let song = {};
if(ytdl.validateURL(args[0])){
const song_info = await ytdl.getInfo(args[0]);
song = {title: song_info.videoDetails.title, url: song_info.videoDetails.video_url};
}else{
const video_finder = async (query) => {
const videoResult = await ytSearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
const video = await video_finder(args.join(" "));
if(video){
song = {title: video.title, url: video.url};
}else{
message.channel.send("No encontre nada al respecto");
}
}
if(!server_queue){
const queue_constructor = {
voice_channel: voice_channel,
text_channel: message.channel,
connection: null,
songs: []
}
queue.set(message.guild.id, queue_constructor);
queue_constructor.songs.push(song);
try{
queue_constructor.connection = await voice_channel.join();
video_player(message.guild, queue_constructor.songs[0]);
}
catch(error){
queue.delete(message.guild.id);
message.channel.send("Hubo pedos para conectarse al canal de voz, perdon banda");
throw error;
}
}else{
server_queue.songs.push(song);
return message.channel.send(`***${song.title}*** añadido a tu cola`);
}
}
}
}
const video_player = async (guild, song) => {
const song_queue = queue.get(guild.id);
if(!song){
song_queue.voice_channel.leave();
queue.delete(guild.id);
return;
}
const stream = await ytdl(song.url, {filter: 'audioonly'});
song_queue.connection.play(stream, {seek: 0, volume: 1, type: "opus"})
.on("finish", () => {
song_queue.songs.shift();
video_player(guild, song_queue.songs[0]);
});
await song_queue.text_channel.send(`Me ando tocando ***${song.title}***`);
}

我的ffmpeg_arguments数组是这样的:

const FFMPEG_ARGUMENTS = ['-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '4', '-analyzeduration', '0', '-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', '2'];

试试这个,它会延迟2秒停止歌曲

song_queue.connection.play(stream, {seek: 0, volume: 1, type: "opus"})
.on("finish", async () => {
let t = await (new Promise((resolve, reject)=>{setTimeout(resolve, 2000)}))
song_queue.songs.shift();
video_player(guild, song_queue.songs[0]);
});

最新更新