当我尝试运行机器人命令UnhandledPromiseRejectionWarning
时,总是出现此错误。
我尝试查找错误并尝试解决该错误,但是没有出现与我的问题有关的错误。
Index.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.
答案 0 :(得分:0)
问题在于您已将代码切换了,将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);
});