[英]JavaScript(discord.js) TypeError: Cannot read property 'startsWith' of undefined
When i was compiling the source code of my Discord bot, i get this:当我编译我的 Discord 机器人的源代码时,我得到了这个:
TypeError: Cannot read property 'startsWith' of undefined
This is the code:这是代码:
const { PREFIX } = require("../config.json");
module.exports = (client, aliases, callBack) => {
if (typeof aliases === "string")
{
aliases = [aliases];
}
client.on("message", message => {
const { CONTENT } = message;
aliases.forEach(alias => {
const COMMAND = ${PREFIX}${alias};
if (CONTENT.startsWith(`${COMMAND}`) || CONTENT === COMMAND)
{
console.log(Asuna is running the command "${COMMAND}"...);
callBack(message);
}
});
});
} }
I think you're not getting the actual message content from the message's object (Therefore it is undefined
).You could try to just set the message content to a constant.我认为您没有从消息的 object 中获取实际的消息内容(因此它是
undefined
)。您可以尝试将消息内容设置为常量。
const msg = message.content;
Then just use msg
in your code.然后只需在您的代码中使用
msg
即可。
if (msg.startsWith(`${COMMAND}`) || msg === COMMAND)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.