javascript - 为什么我的 discord.js 音乐机器人在歌曲结束之前就停止了?
问题描述
我的音乐机器人工作正常,除了歌曲在结束前 10-15 秒停止播放。我的代码工作正常,但这是我唯一的问题,我没有收到任何错误。
这是我的代码:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: 'Joins and plays a video from youtube',
async execute(message, args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('You need to be in a voice channel to use this smh');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins lol');
if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins lol');
if (!args.length) return message.channel.send('Put a song name lol');
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(await ytdl(url, {
filter: format => ['251'],
highWaterMark: 1 << 25
}), {
type: 'opus'
})
.on('finish', () =>{
voiceChannel.leave();
message.channel.send('leaving channel');
});
await message.reply(`<:pepefingergun:759099199949373511> Now Playing ***Your Link!***`)
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: 1})
.on('finish', () =>{
voiceChannel.leave();
message.channel.send('leaving channel');
});
await message.reply(`<:pepefingergun:759099199949373511> Now Playing ***${video.title}***`)
} else {
message.channel.send('No video results found');
}
}
}
解决方案
看起来您在 youtube 上遵循了 codelyon 的教程。这是 Youtube 的一个问题,因为它会在机器人完成读取文件之前尝试断开流。
转到npm-modules/discord.js/src/client/voice/player/BasePlayer.js
并将这些行添加到FFMPEG_ARGUEMENTS
数组中:
'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'
然后只需重新启动您的机器人,它应该得到解决。
如果这对您不起作用,您可能需要替换ytdl-core
并ytdl-core-discord
使用它:
const ytdl = require('ytdl-core-discord');
async function play(connection, url) {
connection.play(await ytdl(url), { type: 'opus' });
}
如果您使用ytdl-core-discord
我FFMPEG_ARGUEMENTS
上面所说的,将会有高质量的音频流。
推荐阅读
- r - 在读取 sas 文件以触发或将数据拉到 r 会话时,有没有办法设置字符编码?
- python-3.x - 从停用词中清除多个术语
- python - tf.keras.layers.Conv3DTranspose 在输入动态形状时抛出 TypeError
- html - 如何在 Symfony 4 表单中的输入上添加占位符?
- python - 删除对象而不刷新django
- amazon-web-services - 对 S3 存储桶中新对象的 PUTS 写后读一致性的警告
- javascript - 当输入、选择等形式发生变化时如何获取计数器?
- node.js - 如何在线正确上传 localhost 网站?
- powershell - 如何比较文件中的两个连续字符串
- cordova - Ionic 4 不能在没有 cordova 的情况下使用 HTTP 插件了吗?