我有这个机器人,每当我使用任何命令时,我都会收到此错误:
<块引用>UnhandledPromiseRejectionWarning:未处理的承诺
拒绝。此错误源于抛出异步
没有 catch 块的函数,或者通过拒绝一个承诺
不使用 .catch() 处理。在未处理的节点上终止节点进程
承诺拒绝,使用 CLI 标志 --unhandled-rejections=strict
(见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。
(rejection id: 1) (node:164) [DEP0018] DeprecationWarning: Unhandled
不推荐使用承诺拒绝。在未来,承诺拒绝
未处理的将终止 Node.js 进程
非零退出代码。
但我不明白错误是什么意思!我试图添加一个 catch
函数,但我收到了另一个这样的错误:
"code".catch 不是函数。
和几个人交谈过,他们也不知道发生了什么。大多数命令仍然按预期工作,但有一些不能。
这是我的代码:
const Discord = require('discord.js');
const client = new Discord.Client();
const DisTube = require('distube');
const { setMaxListeners } = 40
const distube = new DisTube(client, { searchSongs: false, emitNewSongOnly: true });
const { token } = require('./info.json');
const prefix = '>'
client.on("ready", () => {
console.log(`${client.user.tag} has logged in!`);
client.user.setActivity('>play', { type: 'WATCHING' });
});
// Queue status template
const status = (queue) => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filter || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode == 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``;
// DisTube event listeners, more in the documentation page
distube
.on("playSong", (message, queue, song) => message.channel.send(
`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`
))
.on("addSong", (message, queue, song) => message.channel.send(
`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`
))
.on("playList", (message, queue, playlist, song) => message.channel.send(
`Play \`${playlist.name}\` playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing \`${song.name}\` - \`${song.formattedDuration}\`\n${status(queue)}`
))
.on("addList", (message, queue, playlist) => message.channel.send(
`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`
))
// DisTubeOptions.searchSongs = true
.on("searchResult", (message, result) => {
let i = 0;
message.channel.send(`**Choose an option from below**\n${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`);
})
distube.on("finish", message => message.channel.send("No more song in queue"))
// DisTubeOptions.searchSongs = true
.on("searchCancel", (message) => message.channel.send(`Searching canceled`))
distube.on("error", (message, err) => message.channel.send(
"An error encountered: " + err
));
client.on("message", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift();
if (message.author.bot) return;
if (command == "play") {
if (!message.member.voice.channel) return message.reply('Join a voice channel to play some music!')
if (!args[0]) return message.reply('You must state something to play!')
distube.play(message, args.join("joined vc! :D"));
}
if (command == "stop") {
const bot = message.guild.members.cache.get(client.user.id);
if (!message.member.voice.channel) return message.reply('Join a voice channel to stop the music!');
if (bot.voice.channel !== message.member.voice.channel) return message.channel.send('You are not in the same voice channel as me!');
distube.stop(message);
message.channel.send('You have stopped the music.');
}
if (command == "skip") {
distube.skip(message);
message.channel.send('Song Skipped')
}
if (["loop", "repeat"].includes(command)) {
let mode = distube.setRepeatMode(message, parseInt(args[0]));
mode = mode ? mode == 2 ? "Repeat queue" : "Repeat song" : "Off";
message.channel.send("Set repeat mode to `" + mode + "`");
}
if (command == "queue") {
let queue = distube.getQueue(message);
message.channel.send('Current queue:\n' + queue.songs.map((song, id) =>
`**${id+1}**. [${song.name}](${song.url}) - \`${song.formattedDuration}\``
).join("\n"));
}
if (command == "volume") {
distube.setVolume(message, args[0]);
message.reply("Volume set to " + args + ".")
}
if (command == "shuffle") {
distube.shuffle(message);
}
if (command = 'seek') {
distube.seek(message, Number(args[0]));
}
if (command == 'autoplay') {
let mode = distube.toggleAutoplay(message);
message.channel.send("Set autoplay mode to `" + (mode ? "On" : "Off") + "`");
}
if ([`3d`, `bassboost`, `echo`, `karaoke`, `nightcore`, `vaporwave`].includes(command)) {
let filter = distube.setFilter(message, command);
message.channel.send("Current queue filter: " + (filter || "Off"));
}
if (command == "ping") {
message.channel.send(`?Latency is ${Date.now() - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
}
if (command == "pause") {
distube.pause
message.channel.send("Current song paused. >unpause to unpase it!")
}
if (command == "unpause") {
distube.unpause
message.channel.send("song unpaused")
}
if (command == "autoplay") {
let mode = distube.toggleAutoplay(message);
message.channel.send("Set autoplay mode to `" + (mode ? "On" : "Off") + "`");
}
});
client.login(token);
答案 0 :(得分:0)
首先,欢迎来到stackoverflow!
您代码中的问题是 client.on('message', [the_callback])
中的回调是一个异步函数,并且在其中抛出了错误,但它没有被 try catch 块包裹(这基本上就是错误消息说)。
所以你需要像这样包装代码:
client.on('message', () => {
try {
your code...
} catch (err) {
console.log(err);
}
});
这将修复 UnhandledPromiseRejectionWarning
并可能导致代码中出现其他错误。