【问题标题】:I've an error but I don't find the problem我有一个错误,但我没有找到问题
【发布时间】:2020-03-18 20:59:54
【问题描述】:

我想执行这个命令,但是当我执行这个命令时,我有这个错误信息: 错误代码:

    (node:8616) UnhandledPromiseRejectionWarning: Error: 400 Bad Request
    at C:\Users\Eleve\Desktop\FA Bot\node_modules\snekfetch\src\index.js:195:23
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:8616) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:8616) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections 
that are not handled will terminate the Node.js process with a non-zero exit code.

这是代码:

    const Discord = require("discord.js")
const botconfig = require("../botconfig.json");
const colours = require("../colours.json");
const prefix = botconfig.prefix
const webhook = require("webhook-discord")

module.exports.run = async (bot, message, args) => {

const Hook = new webhook.Webhook("https://discordapp.com/api/webhooks/689803124957118476/xiz3sE3dhQhjW7CoJrTIVB60LCckSHYm_0X9OwlGEQwDnmKYHzFuvekImwxjSaOuhoN-")
const msg = new webhook.MessageBuilder()
                .setName(message.author.username)
                .setColor("RANDOM")
                .setText("Test")
                .setTime();

Hook.send(msg)

}

module.exports.config = {
    name: "webhook",
    aliases: ["wh"],
    usage: "!usage",
    description: "",
    accessableby: "Membres"
}

你能帮帮我吗?

【问题讨论】:

  • 您收到 HTTP 错误 400 Bad Request,这意味着您的请求不正确。 discord-webhook 上次更新是 6 个月前,所以我猜 Discord API 发生了重大变化。我建议你使用 discord.js 网络钩子,它们自己工作得很好

标签: javascript node.js discord.js


【解决方案1】:

错误本身已经告诉你了。出现错误的原因是 未处理的承诺拒绝

原因:

1. This error originated either by throwing inside of an async function without a catch block, or
2. by rejecting a promise which was not handled with .catch().

这意味着您可以通过以下方式处理问题:

1. You can move you async function into a try and catch block in order to catch the error when the function has failed 
2. You can add a .catch() chain function as node suggested. 

错误信息通常会得到答案,学习如何阅读它们非常重要。我还在学习如何阅读它。希望能帮助到你。

材料:

  1. Ian Segers 在 JS 中使用 Async/Await 处理错误,link

【讨论】:

  • 还不如仅仅阅读错误信息那么清楚。是的,它提供了寻找线索的线索,但仅仅捕获错误而不是找到引发错误的原因并不总是解决方案。通常情况下,我们想要错误,以便我们知道我们的代码有问题。
  • 你解释了肤浅的问题,而不是原因。这里真正的问题是对 Discord webhook 端点的格式错误的请求。
猜你喜欢
  • 2017-05-26
  • 2016-09-16
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多