javascript - 为什么自从我切换到 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?”
我希望你能帮帮我!提前致谢 :)
对不起,如果我的英语不好,但我是法语,不是英语!
解决方案
在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) => { // ... }
推荐阅读
- c# - UDP客户端未在Linux机器上接收消息
- ruby-on-rails - 我们如何在 rspec 中打印响应正文
- android - 即使在打盹模式下也每分钟发送一次网络请求
- git - git pull --recurse-submodules '无法访问子模块'
- java - 如何从 Springboot 提供 *.jsp
- android - 我正在尝试将我的 sqlite 数据库文件从本地存储备份到 Firebase 存储
- node.js - Heroku 和 MongoDB 地图集连接
- amazon-web-services - 旧数据仍来自 Auto Scaling 组中的 EC2 实例
- postgresql - postgres error: column doesn't exist error in Postgesql 11.6
- python - 多线程 Python 代码中的数据不一致