[英]TypeError: Cannot read properties of undefined (reading 'set') Discord.js
[英]Discord.js - TypeError: Cannot read properties of undefined (reading 'set')
這是在 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 node .
在終端中,我收到此錯誤:
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已經死了,我需要幫助!
編輯一這是我的文件的組織方式:圖片 1
正如@MrMythical 指出的那樣,唯一的問題是我忘記了client.command = new Discord.Collection();
行中的“s” client.command = new Discord.Collection();
. 在我最后加上一個“s”后,它工作正常——沒有其他錯誤,所以這是我自己問題的答案!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.