简体   繁体   English

如何修复ReferenceError

[英]How to fix ReferenceError

After some modification, instead of message is not defined, it is receivedMessage.channel.bulkdelete(args[0].then (() => { ReferenceError: receivedmessage is not defined. I am not really sure myself what does that mean because im new to node.js and javascript. If there is any mistakes I made please tell me! 经过一些修改,而不是未定义消息,它是receiveMessage.channel.bulkdelete(args [0] .then(()=> {ReferenceError:receivemessage没有定义。我不确定自己是什么意思,因为im node.js和javascript的新功能。如果我犯了任何错误,请告诉我!

client.on('message', (receivedMessage) => {
    if (receivedMessage.author == client.user) { // Prevent bot from responding to its own messages
        return
    }

    if (receivedMessage.content.startsWith("?")) {
        processCommand(receivedMessage)
    }
})

function processCommand(receivedMessage) {
    let fullCommand = receivedMessage.content.substr(1) // Remove the leading exclamation mark
    let splitCommand = fullCommand.split(" ") // Split the message up in to pieces for each space
    let primaryCommand = splitCommand[0] // The first word directly after the exclamation is the command
    let arguments = splitCommand.slice(1) // All other words are arguments/parameters/options for the command

    console.log("Command received: " + primaryCommand)
    console.log("Arguments: " + arguments) // There may not be any arguments

if (primaryCommand == "help") {
    helpCommand(arguments, receivedMessage)
} else if (primaryCommand == "multiply") {
    multiplyCommand(arguments, receivedMessage)
} else if(primaryCommand == "clear") {
    clearCommand(arguments, receivedMessage)
} else {
    receivedMessage.channel.send("I don't understand the command. Try `?help`, `?multiply` or '?clear'")
}

function helpCommand(arguments, receivedMessage) {
    if (arguments.length > 0) {
        receivedMessage.channel.send("It looks like you might need help with " + arguments + ".Try `!multiply 2 4 10` or `!multiply 5.2 7`")
    } else {
        receivedMessage.channel.send("I'm not sure what you need help with. Try `?help [topic]`")
    }
}

function multiplyCommand(arguments, receivedMessage) {
    if (arguments.length < 2) {
        receivedMessage.channel.send("Not enough values to multiply. Try `!multiply 2 4 10` or `!multiply 5.2 7`")
        return
    }
    let product = 1 
    arguments.forEach((value) => {
        product = product * parseFloat(value)
    })
    receivedMessage.channel.send("The product of " + arguments + " multiplied together is: " + product.toString())
    }
}

function clearCommand (arguments, receivedMessage) {
    if (!recievedMessage.member.hasPermission("MANAGE_MESSAGES")) 
        return receivedmessage.reply("You have no permission to use this command.Sad."); 
    if (!args[0]) 
        return receivedMessage.channel.send("Please specify a number.")
}
        receivedmessage.channel.bulkDelete(args[0]).then(() => {
        receivedMessage.channel.send(`Cleared ${args[0]} messages.`).then(msg => msg.delete(5000));
    }
,)

You need to use receivedMessasge instead of message , as thats the name you choose in that function. 您需要使用receivedMessasge而不是message ,因为这就是您在该函数中选择的名称。

It kinda seems like you dont really have much experience and I would recommend you to read the offical discord.js guide: https://discordjs.guide . 似乎您确实没有太多经验,我建议您阅读正式的discord.js指南: https : //discordjs.guide It will teach you how to write Discord Bots without needing to copy a lot of weired stuff into your Code! 它会教您如何编写Discord Bot,而无需将大量奇怪的内容复制到您的代码中!

1) You have defined receivedMessage instead of messsage 1)您定义receivedMessage messsage而不是messsage

2) The code for clear command is not in any function and it executes once, before any messages. 2)clear命令的代码没有任何功能,并且在任何消息之前执行一次。

You need to use receivedMessage instead of message and insert the code in the processCommand function 您需要使用receivedMessage ,而不是message ,并在插入代码processCommand功能

if (primaryCommand == "help") {
    helpCommand(arguments, receivedMessage)
} else if (primaryCommand == "multiply") {
    multiplyCommand(arguments, receivedMessage)
} else if(primaryCommand == "clear") {
    if (!message.member.hasPermission("MANAGE_MESSAGES")) return message.reply("You have no permission to use this command.Sad."); 
    if (!args[0]) return message.channel.send("Please specify a number.")
    message.channel.bulkDelete(args[0]).then(() => {
        message.channel.send(`Cleared ${args[0]} messages.`).then(msg => msg.delete(5000));
    });
    // or create a function for this command
} else {
    receivedMessage.channel.send("I don't understand the command. Try `?help` or `?multiply`")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM