javascript - 我在尝试运行机器人命令时收到此错误

标签 javascript node.js discord.js

当我尝试运行机器人命令UnhandledPromiseRejectionWarning时,我不断收到此错误。
我尝试查找错误并尝试修复它,但没有出现任何与我的问题相关的结果。

索引.js

fs.readdir("./commands/", (err, files) => {
  if (err) console.error(err);

  let jsfile = files.filter(f => f.split(".").pop() === "js")
  if (jsfile.length <= 0) {
    console.log("Cant find Commands")
    return;
  }

  jsfile.forEach((f, i) => {
    let props = require(`./commands/${f}`)
    console.log(`${f} loaded`);
    bot.commands.set(f, props);
  });

});

bot.on("ready", async() => {

  bot.user.setActivity("Running my code")
  console.log(`${bot.user.username} is online on ${bot.guilds.size} servers!`);
  console.log(bot.commands)
});

bot.on("message", async message => {
  if (message.author.bot) return;
  if (message.channel.type === "dm") return
  if (message.startsWith(prefix)) return;

  let cmd = bot.commands.get(command.slice(prefix.length));
  if (cmd) cmd.run(bot, message, args);

  let messageArray = message.content.split(" ")
  let command = messageArray[0];
  let args = messageArray.slice(1);
});

我尝试运行的命令:

const Discord = module.require("discord.js");

module.exports.run = async(bot, message, args) => {
  let embed = new Discord.RichEmbed()
    .setAuthor(message.author.username)
    .setDescription("Users Info")
    .setColor("#9B59B6")
    .addField("Full Username", `${message.author.username}+${message.author.discriminator}`)
    .addField("ID", message.author.id)
    .addField("Created at", message.author.createdAt);

  message.channel.send(embed);
}

module.exports.help = {
  name: "Userinfo"
}

这只是我的一小部分代码,如果我需要发布更多内容,请告诉我。
这是完整的错误:

(node:26635) UnhandledPromiseRejectionWarning: TypeError: message.startsWith is not a function
at Client.bot.on (/media/jeremiah/DISCORDBOT/Discord bot/index.js:36:16)
at emitOne (events.js:116:13)
at Client.emit (events.js:211:7)
at MessageCreateHandler.handle (/media/jeremiah/DISCORDBOT/Discord bot/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
at WebSocketPacketManager.handle (/media/jeremiah/DISCORDBOT/Discord bot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (/media/jeremiah/DISCORDBOT/Discord bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (/media/jeremiah/DISCORDBOT/Discord bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
at WebSocket.onMessage (/media/jeremiah/DISCORDBOT/Discord bot/node_modules/ws/lib/event-target.js:120:16)
at emitOne (events.js:116:13)
at WebSocket.emit (events.js:211:7)
(node:26635) 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(). (rejection id: 1)
(node:26635) [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.

最佳答案

问题是你的代码被调换了,将你的 on("message", ... 更改为以下内容:

bot.on("message", async message => {
    if (message.author.bot) return;
    if (message.channel.type === "dm") return
    if (message.content.startsWith(prefix)) return;

    let messageArray = message.content.split(" ")
    let command = messageArray[0];
    let args = messageArray.slice(1);

    let cmd = bot.commands.get(command.slice(prefix.length));
    if (cmd) cmd.run(bot, message, args);
});

关于javascript - 我在尝试运行机器人命令时收到此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54481973/

相关文章:

javascript - 重定向特定 channel 中的 Discord DM

javascript - 从提及中获取用户 ID

javascript - 在 setInterval 运行时使用 clearInterval

javascript - document.getElementById ("id").value 的最佳用途是什么

javascript - 从标签中选择随机单词,用斜体括起来

javascript - JS 单元测试无需将整个 HTML 复制到单元测试中?

node.js - 来自 MongoDB 的随机记录

javascript - Ext JS 动态更改 app.json 的区域设置

linux - `npm uninstall` 在没有明显事件的情况下挂起(或非常慢)

node.js - Mongoose model.find({}) 使用 mongoose 4.11 后不起作用; [漏洞??]