Complete code:
const Discord = require('discord.js');
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"]});
client.commands = new Discord.Collection()
client.events = new Discord.Collection()
['command', 'event'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord)
});
client.login('tokenremoved')
This error is actually caused by "automatic semicolon injection". If you don't put semicolons in your code, Javascript will add them under the hood to the best of its ability. It's very accurate, however, there are a few instances such as this one where it misunderstands what's written.
// javascript sees this:
client.events = new Discord.Collection()
['command', 'event'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord)
});
// and translates it to this:
client.events = new Discord.Collection()['command', 'event'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord)
});
Javascript thinks you're trying to access a parameter off Discord.Collection()
with the brackets. However, since Collection#command
doesn't exist, it returns undefined. Thus:
TypeError: Cannot read property 'forEach' of undefined
const test = 'hello' [true, 123].forEach(() => 'this throws an error')
The solution is just to add a semicolon after Discord.Collection()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.