Javascript 我该如何解决这个问题?未处理的PromisejectionWarning

Javascript 我该如何解决这个问题?未处理的PromisejectionWarning,javascript,discord.js,distube,Javascript,Discord.js,Distube,我有这个机器人,每当我使用任何命令时,我都会出现以下错误: 未处理的PromisejectionWarning:未处理的承诺 拒绝。此错误源于在异步 函数没有catch块,或者拒绝了 未使用.catch()处理。在未处理的服务器上终止节点进程 承诺拒绝,使用CLI标志--未处理拒绝=严格 (见附件)。 (拒绝id:1)(节点:164)[DEP0018]拒绝警告:未处理 拒绝承诺是不推荐的。在未来,承诺拒绝 未处理的将使用 非零退出代码 但是我不明白这个错误是什么意思!我试图添加一个catch函数

我有这个机器人,每当我使用任何命令时,我都会出现以下错误:

未处理的PromisejectionWarning:未处理的承诺 拒绝。此错误源于在异步 函数没有catch块,或者拒绝了 未使用.catch()处理。在未处理的服务器上终止节点进程 承诺拒绝,使用CLI标志
--未处理拒绝=严格
(见附件)。 (拒绝id:1)(节点:164)[DEP0018]拒绝警告:未处理 拒绝承诺是不推荐的。在未来,承诺拒绝 未处理的将使用 非零退出代码

但是我不明白这个错误是什么意思!我试图添加一个
catch
函数,但我遇到了另一个类似这样的错误:

“代码”。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(`First of all, welcome to stackoverflow!

The problem in your code is that the callback in the
client.on('message', [the_callback])
is an async function and there are errors being thrown inside of it but it isn't wrapped by a try catch block (thats basically what the error message says). So you need to wrap the code like this:

client.on('message', () => {
  try {
    your code...
  } catch (err) {
    console.log(err);
  }
});
const Discord=require('Discord.js');
const client=new Discord.client();
const DisTube=require('DisTube');
常量{setMaxListeners}=40
const distube=new distube(客户端,{searchSongs:false,emitNewSongOnly:true});
const{token}=require('./info.json');
常量前缀='>'
client.on(“ready”,()=>{
log(`${client.user.tag}已登录!`);
client.user.setActivity('>play',{type:'WATCHING'});
});
//队列状态模板
const status=(queue)=>`Volume:\`${queue.Volume}%\\\`Filter:\`${queue.Filter}\\\\\\\'Off}\\`Loop:\`${queue.repeatMode?queue.repeatMode==2?“所有队列”:“这首歌”:“Off”}`;自动播放:\${queue.Autoplay?“开”:“Off”;
//DisTube事件侦听器,更多信息请参见文档页面
蒸馏器
.on(“播放歌曲”,(消息、队列、歌曲)=>message.channel.send(
`播放\${song.name}`-\`${song.formattedDuration}`\n请求者:${song.user}\n${status(queue)}`
))
.on(“addSong”,(消息、队列、歌曲)=>message.channel.send(
`通过${song.user}将${song.name}-\`${song.formattedDuration}`添加到队列中`
))
.on(“播放列表”,(消息、队列、播放列表、歌曲)=>message.channel.send(
`播放\${playlist.name}\`播放列表(${playlist.songs.length}歌曲)。\n请求者:${song.user}\n正在播放\${song.name}\-\${song.formattedDuration}\\\\n${状态(队列)}`
))
.on(“addList”,(消息、队列、播放列表)=>message.channel.send(
`已将\`${playlist.name}\`播放列表(${playlist.songs.length}歌曲)添加到队列\n${status(queue)}`
))
//DisTubeOptions.searchSongs=true
.on(“搜索结果”,(消息、结果)=>{
设i=0;
message.channel.send(`**从下面选择一个选项**\n${result.map(song=>`**${++i}**.${song.name}-\`${song.formattedDuration}\`)。join(“\n”)}\n*输入任何其他内容或等待60秒取消*`);
})
distube.on(“finish”,message=>message.channel.send(“队列中不再有歌曲”))
//DisTubeOptions.searchSongs=true
.on(“searchCancel”,(message)=>message.channel.send(`searchcanceled`)
distube.on(“错误”,“消息,错误)=>message.channel.send(
遇到错误:“+err”
));
client.on(“message”,异步(message)=>{
if(message.author.bot)返回;
如果(!message.content.startsWith(prefix))返回;
const args=message.content.slice(prefix.length.trim().split(+/+/g);
const命令=args.shift();
if(message.author.bot)返回;
如果(命令==“播放”){
if(!message.member.voice.channel)返回message.reply('加入语音频道播放音乐!')
if(!args[0])返回message.reply('您必须声明要播放的内容!')
distube.play(消息,args.join(“joined vc!:D”);
}
如果(命令==“停止”){
const bot=message.guild.members.cache.get(client.user.id);
如果(!message.member.voice.channel)返回message.reply('加入语音频道以停止音乐!');
if(bot.voice.channel!==message.member.voice.channel)返回message.channel.send('您和我不在同一个语音频道!');
distube.stop(消息);
message.channel.send('您已停止播放音乐');
}
如果(命令==“跳过”){
distube.skip(消息);
message.channel.send('跳过歌曲')
}
如果([“循环”,“重复”].包括(命令)){
let mode=distube.setRepeatMode(消息,parseInt(args[0]);
模式=模式?模式=2?“重复队列”:“重复歌曲”:“关闭”;
message.channel.send(“将重复模式设置为“+”模式+”);
}
如果(命令==“队列”){
让queue=distube.getQueue(消息);
message.channel.send('当前队列:\n'+queue.songs.map((song,id)=>
`**${id+1}**.[${song.name}](${song.url})\`${song.formattedDuration}\``
).join(“\n”);
}
如果(命令==“卷”){
setVolume(消息,参数[0]);
message.reply(“卷设置为“+args+”)
} 
如果(命令==“洗牌”){
distube.shuffle(消息);
}
if(命令='seek'){
seek(消息,编号(args[0]);
}   
如果(命令=='autoplay'){
let mode=distube.toggleAutoplay(消息);
message.channel.send(“将自动播放模式设置为”+(模式?“打开”):“关闭”)+“`”;
}
if([`3d`、`bassboost`、`echo`、`karaoke`、`nightcore`、`vaporwave`].includes(命令)){
let filter=distube.setFilter(消息、命令);
message.channel.send(“当前队列筛选器:+(筛选器| |“关闭”));
}
如果(命令==“ping”){

message.channel.send(`首先,欢迎来到stackoverflow

代码中的问题是
客户机上的回调('message',[u callback])
是一个异步函数,存在错误