【发布时间】:2021-10-16 23:54:40
【问题描述】:
这是在 main.js 中:
const { Client, Intents, DiscordAPIError } = require('discord.js');
const Discord = require('discord.js');
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = 'js!';
const fs = require('fs');
client.command = new Discord.Collection();
fs.readdir("./commands/", (err, files) => {
if(err) console.error(error)
let jsfiles = files.filter(f => f.split(".").pop() === "js")
if (jsfiles.length <= 0) {
return console.log("No commands to log in FOLDER NAME")
}
console.log(`Loading ${jsfiles.length} commands from FOLDER NAME...`)
jsfiles.forEach((f,i) => {
let props = require(`./commands/${f}`)
console.log(`${i + 1}: ${f} loaded!`)
client.commands.set(f, props)
})
});
client.once('ready',() => {
console.log('bot is on')
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
let cmd = client.commands.get(command+".js")
if (cmd) cmd.run(bot, message, args, prefix)
}
});
client.login('Prefix')
这是在 ping.js 中:
const { Client, Intents, DiscordAPIError } = require('discord.js');
const Discord = require('discord.js')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
module.exports.run = async (client, message, args, prefix) => {
var ping = Date.now() - message.createdTimestamp + " ms";
message.channel.send('Ma ping is' + ` ${Date.now() - message.createdTimestamp}` + 'ms')
};
module.exports.help = {
name: "ping",
usage: "Ping Command",
};
但如果我在终端中使用命令node . 运行 main.js,我会收到此错误:
Loading 1 commands from FOLDER NAME...
1: ping.js loaded!
/home/ender/Desktop/Projects & Stuff/Coding/Discord Bot (JS)/main.js:23
client.commands.set(f, props)
^
TypeError: Cannot read properties of undefined (reading 'set')
at /home/ender/Desktop/Projects & Stuff/Coding/Discord Bot (JS)/main.js:23:23
at Array.forEach (<anonymous>)
at /home/ender/Desktop/Projects & Stuff/Coding/Discord Bot (JS)/main.js:20:13
at FSReqCallback.oncomplete (node:fs:188:23)
我使用linux作为我的主要操作系统,我使用的node.js版本是v16.11.1,npm版本是8.0.0!
请帮忙,我最近开始使用 discord.js,因为我意识到 discord.py 已经死了,我需要帮助!
编辑一个 这就是我的文件的组织方式:picture 1
【问题讨论】:
-
您能否编辑您的问题以添加存放您的机器人命令的文件夹的图像,以便我们查看是否存在导致问题的文件名/扩展名?
-
您在顶部将其声明为
client.command(无 s),但您将其设置为client.commands -
@MrMythical 现在我看到了,在我编辑后它现在可以工作了!我试图找出问题所在 2 天.. 感谢您帮助我!
标签: javascript discord.js