【问题标题】:I am getting this error when trying to run my bot commands尝试运行我的机器人命令时出现此错误
【发布时间】:2019-02-02 00:12:52
【问题描述】:

当我尝试运行我的机器人命令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.

【问题讨论】:

  • 你能准确指出是哪一行引发了错误吗?
  • 所以我修复了 message.startsWith 错误但现在它告诉我命令未定义它在部分“(command.slice(prefix.length));
  • 将这些行: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); 并试一试
  • @T.Dirks 所有这些东西都已经在你要我改变什么?对不起,我有时会是个白痴
  • 那东西在里面,没错。不过顺序错了。 on("message", ... 函数中的代码基本上有 3 个部分。第一部分有 3 个if 语句,第二部分有一个let 和一个if,第三部分有3 个let 语句。您需要切换第二部分和第三部分

标签: javascript node.js discord.js


【解决方案1】:

问题是您的代码切换了,请将您的 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);
});

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 2014-09-07
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2021-11-15
    • 2020-12-04
    相关资源
    最近更新 更多