首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复ReferenceError

如何修复ReferenceError
EN

Stack Overflow用户
提问于 2019-08-18 19:07:18
回答 2查看 137关注 0票数 0

经过一些修改,没有定义message,而是receivedMessage.channel.bulkdelete(args.then (() => { ReferenceError: receivedmessage is not defined。我自己也不太确定这意味着什么,因为我是node.js和javascript的新手。如果我犯了什么错误,请告诉我!

代码语言: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));
    }
,)
EN

回答 2

Stack Overflow用户

发布于 2019-08-19 00:13:01

您需要使用receivedMessasge而不是message,因为这是您在该函数中选择的名称。

看起来您并没有多少经验,我建议您阅读官方的discord.js指南:https://discordjs.guide。它将教你如何编写不和谐的机器人,而不需要将许多奇怪的东西复制到你的代码中!

票数 0
EN

Stack Overflow用户

发布于 2019-08-20 00:30:07

1)您定义的是receivedMessage而不是messsage

2) clear命令的代码不在任何函数中,它在任何消息之前执行一次。

您需要使用receivedMessage而不是message,并在processCommand函数中插入代码

代码语言:javascript
复制
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`")
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57543935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档