首页 > 解决方案 > 为什么自从我切换到 discord js 13 后,messageCreate Event 出现错误?

问题描述

我解释了我的问题:我在我的事件的命令之外使用了一个 js 文件,我的印象是我的 messageCreate.js 中有些东西不能正常工作,但之前在版本 12 中与 Discord .JS 一起工作。对于例如,当我想在机器人发送消息后立即返回时,它无法通过 messageCreate.js 工作,我必须将其放入我的所有命令等中。

在我的命令中,我注意到参数不起作用,而之前我没有问题。我能够通过放置一个来验证这一点

if(args == undefined) return console.log("test")

我一尝试,控制台中就会显示消息“测试”。

我把我的代码放在下面,希望你能帮助我。:)

我的 index.js 中处理事件的部分:

fs.readdir("./Events/", (err, files) => {
    Debug.logs(`[${chalk.cyan(moment(Date.now()).format('h:mm:ss'))}] ${chalk.cyan('Chargement des évènements ...')}`)

    if (err) return Debug.logs(err)

    files.forEach(async (file) => {
        if (file.endsWith(".js")) {
            const event = require(`./Events/${file}`)
            let eventName = file.split(".")[0]
            try {
                bot.on(eventName, event.bind(null, bot))
                delete require.cache[require.resolve(`./events/${file}`)]
                Debug.logs(`[${chalk.cyan(moment(Date.now()).format('h:mm:ss'))}] ${chalk.green('Event Chargé :')} ${chalk.cyan(file)}`)
            } catch (error) {
                Debug.logs(error)
            }
        } else {
            return
        }
    })
})

我的 messageCreate.js :

const env = process.env
const chalk = require('chalk')
const moment = require('moment')
const Debug = require('../utils/Debug')
const config = require("../config.json")

module.exports = async (bot, message) => {
    
    if(message.channel.type === "DM"){
        if(message.author.bot) return;
        message.reply("Les commandes en **messages privés** sont actuellement **désactivées** !")
        Debug.logs(`[${chalk.cyan(moment(Date.now()).format('h:mm:ss'))}] [${chalk.yellow(message.author.tag)}] a envoyé ${chalk.green(message.content)} en DM`)
    }else{
    if (!message.author.bot) {
        if (message.content.startsWith(config.prefix)) {
            const args = message.content.slice(config.prefix.length).trim().split(/ +/g)
            const command = args.shift().toLowerCase()
            const cmd = bot.commands.get(command)
        
            if (cmd) {
                await cmd.run(bot, message, args)
                Debug.logs(`[${chalk.cyan(moment(Date.now()).format('h:mm:ss'))}] [${chalk.yellow(message.author.tag)}] a utilisé ${chalk.green(command)} ${chalk.cyan(args.join(" "))}`)
            } else {
                return
            }
        } else {
            return
        }
    } else {
        return
    }
}
}

以及一个不能与 messageCreate.js 一起使用的命令示例:

const Discord = require("discord.js");

module.exports.run = async (bot, message, config, args) => {

    message.delete();

        if(args == undefined) return console.log("wtf that not work ?")
}

module.exports.help = {
    name:"test",
    desc:"test commands !",
    usage:"test",
    group:"autre",
    examples:"$test"
}

module.exports.settings = {
    permissions:"false",
    disabled:"false",
    owner:"false"
}

只要我运行带有参数或不带任何参数的命令,在这两种情况下,控制台都会收到消息“wtf that not work?”

我希望你能帮帮我!提前致谢 :)

对不起,如果我的英语不好,但我是法语,不是英语!

标签: javascriptnode.jsdiscord.js

解决方案


messageCreate.js中,您使用以下命令调用您的命令:

// 3 arguments
await cmd.run(bot, message, args);

但是,您的命令的 run 函数使用 4 个参数定义:

async (bot, message, config, args) => {
  // config = 'args', and args = undefined
}

要解决此问题,请执行以下任一操作:

  • 为 传递一个值config,例如null
    await cmd.run(bot, message, null, args);
    
  • 使您的函数只有 3 个参数:
    async (bot, message, args) => {
      // ...
    }
    

推荐阅读