Reputation: 27
I am new to discord bot development and I am unable to run my events. I applied the same logic as in Commands Handler, but apparently it didn't work. I tried to force the execution of the ready event with const event = client.events.get("ready"); event.run(client);
however it returns undefined as the value of event
.
I apologize if it is 'obvious', but I really haven't found anything that could help me resolve it.
index.js
const fs = require('fs');
const Discord = require('discord.js');
require('dotenv').config();
const client = new Discord.Client();
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
for (const file2 of eventFiles) {
const event = require(`./events/${file2}`);
client.events.set(event.name, event);
}
const event = client.events.get("ready");
event.run(client);
client.login();
ready.js
module.exports = {
event: "ready",
once: true,
run(client) {
console.log('Ready!');
client.user.setActivity('A!help / A!info', { type: 'STREAMING', url: 'https://www.twitch.tv/' });
}
};
files:
-index.js
-events
-ready.js
Upvotes: 0
Views: 613
Reputation: 26
Original Answer:
You are calling the wrong object key for event. Try:
client.events.set(event.event, event);
Edit:
You need to wait for the client to be ready. Then when you login, you need to provide your bot token:
index.js:
const fs = require('fs');
const Discord = require('discord.js');
require('dotenv').config();
const client = new Discord.Client();
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
for (const file2 of eventFiles) {
const event = require(`./events/${file2}`);
client.events.set(event.event, event);
}
client.on('ready', () => {
const event = client.events.get("ready");
event.run(client);
});
client.login(process.env.token);
.env:
token="YOUR BOT TOKEN"
Upvotes: 1
Reputation: 118
The module you are exporting in ready.js
doesn't have the property name
only event
, which is supposed to be the name from the event I assume.
You have 2 solutions:
event
to name
name
to event
Solution Number 1:
// In your ready.js
name: "ready",
Solution 2:
client.events.set(event.event, event);
the console returned another error now
Cannot read property 'setActivity' of null
You are triggering the event before your bot is online. So you need to wait until the client got the event ready
triggered.
This can be done by writing in your index.js
const event = client.events.get("ready");
client.on("ready", () => event.run(client));
Upvotes: 1