当前位置: 首页 > 知识库问答 >
问题:

我正在创建一个系统,这样用户就可以进行bug报告,一切正常,但控制台抛出了一个错误,我想解决这个问题。js

方焱
2023-03-14
const { RichEmbed, MessageEmbedImage } = require("discord.js");
const { promptMessage } = require("../../functions.js");

module.exports = {
  name: "bugreport",
  aliases: ["bugreports"],
  usage: "bugreport <your report>",
  description: "Send your report",
  category: "server",
  run: async (client, message, args) => {
    const channel = message.guild.channels.find(c => c.name === "「❗」reports");

    if (message.deletable) message.delete();
    
    if(!args[0]) {
        return message.reply("Please write your report").then(m => m.delete(10000));
        }
    if(!channel) {
      return message.reply("There is no channel with name '#「❗」reports'").then(m => m.delete(10000));
        }
        
    const bugreportMessage = {
      color: 51199,
      author: {
        name: "Report created by "+message.author.username,
        icon_url: message.author.avatarURL
      },
      title: "New bug reported:",
      url: "",
      description: args.slice(0).join(" "),
      thumbnail: {
        url: message.author.avatarURL,
      },
      timestamp: new Date(),
      footer: {
        icon_url: client.user.avatarURL, text: "♛ Space Network"
      }
    }

    message.channel.send("Sending your report to " + channel).then(m => m.delete(10000))
    await channel.send({ embed: bugreportMessage });
    }
}

错误:(节点:8420)未经处理的PromisejectionWarning:DiscordAPIError:未知消息位于E:\GitHub\space bot\node\u modules\discord。js\src\client\rest\RequestHandlers\Sequential。js:85:15,地址为E:\GitHub\space bot\node\u modules\snekfetch\src\index。js:215:21在ProcessTicks和Rejections(internal/process/task_queues.js:93:5)(使用节点--跟踪警告…显示警告的创建位置)(节点:8420)未处理Promisejection警告:未处理的promise拒绝。此错误源于在没有catch块的情况下抛出异步函数的内部,或者拒绝使用未处理的promise。catch()。要在未处理的promise拒绝时终止节点进程,请使用CLI标志--unhandled rejections=strict(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)(拒绝id:2)(节点:8420)[DEP0018]弃用警告:未处理的promise拒绝被弃用。将来,未处理的promise拒绝将终止节点。具有非零退出代码的js进程。

运行命令时出现不一致:在此处输入图像描述

运行后控制台命令:在此处输入图像描述

共有1个答案

井唯
2023-03-14

代码中有很多方法可以返回promise,我看不到任何catch()es来处理拒绝。我想是那种。delete()s找不到要删除的邮件。

如果您已经在使用异步函数,那么可以去掉那些then()s,改为使用async wait,并将代码包装在一个try-catch块中,如下所示:

run: async (client, message, args) => {
  try {
    const channel = message.guild.channels.find(
      (c) => c.name === '「❗」reports',
    );

    if (message.deletable) message.delete();

    if (!args[0]) {
      const msg = await message.reply('Please write your report');
      return msg.delete(10000);
    }

    if (!channel) {
      const msg = await message.reply("There is no channel with name '#「❗」reports'");
      return msg.delete(10000);
    }

    const bugreportMessage = {
      color: 51199,
      author: {
        name: 'Report created by ' + message.author.username,
        icon_url: message.author.avatarURL,
      },
      title: 'New bug reported:',
      url: '',
      description: args.slice(0).join(' '),
      thumbnail: {
        url: message.author.avatarURL,
      },
      timestamp: new Date(),
      footer: {
        icon_url: client.user.avatarURL,
        text: '♛ Space Network',
      },
    };

    const msg = await message.channel.send('Sending your report to ' + channel);
    msg.delete(10000);

    channel.send({ embed: bugreportMessage });
  } catch (error) {
    console.log(error);
  }
};
 类似资料: