编码Discord bot时“无法读取未定义的属性'toLowerCase'”

时间:2020-04-08 08:24:41

标签: javascript node.js discord.js

我最近尝试制作一个不和谐的bot(使用node),并且不断遇到“无法读取未定义的属性'toLowerCase'”。我尝试将“ toLowerCase”更改为“ toUpperCase”,但无法解决问题。

client.on('ready', () => {
    console.log("The bot is online!");
    client.user.setActivity('Servers', { type: 'WATCHING' });


}); 

const isCommand = (message, cmdName) => message.content.toLowerCase().startsWith(PREFIX + cmdName);
const rollDice = () => Math.floor(Math.random() * 6) + 1;
client.on('message', function (message){
    if(message.author.bot) return;
     if(isCommand(message + "hello")) 
        message.reply(" Hi! I didn't want to make you feel lonely so I wanted to say hi.");
        if(isCommand(message , "rolldice")) {
            message.reply("You rolled a "+ rollDice());
   }




});

2 个答案:

答案 0 :(得分:0)

由于未定义message.contenttoLowerCase上没有属性undefined,因此您收到该错误。您需要检查message包含的内容,以及其中是否包含content属性。

正如@Aafaq所说,应该为if(isCommand(message ,"hello")),否则您只是传递一个参数。

即使如此,您仍需要在回调中console.log消息对象,查看消息对象正在接收什么,然后访问适当的属性。

答案 1 :(得分:0)

上面的所有起始代码都可以,可以通过以下方式进行修改:

//startcode
client.on('message', function (message){
const isCommand = (message, cmdName) => message.content.toLowerCase().startsWith(PREFIX + cmdName);
const rollDice = () => Math.floor(Math.random() * 6) + 1;
    if(message.author.bot) return;
     if(isCommand(message + "hello")) 
        message.reply(" Hi! I didn't want to make you feel lonely so I wanted to say hi.");
        if(isCommand(message , "rolldice")) {
            message.reply("You rolled a "+ rollDice());
   }