Event Help

I dont know how events work and i want to make one where each time someone sends a message, it reads and sees if its that message. Its gonna have a prefix “!” and will look for something like “!message”. Can someone help me?
55 Replies
d.js toolkit
d.js toolkit6mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Samtino
Samtino6mo ago
Prefix commands are not a supported feature of DiscordJS. You will have to build the whole logic on your own I would highly recommend using Slash Commands instead. They're easier to work on as a developer, and easier to use as a user
Skull
SkullOP6mo ago
Awh man. Okay. Thanks for letting me know. You said id have to write my own logic? Would it be in JavaScript?
Amgelo
Amgelo6mo ago
everything you do with djs is in js
Skull
SkullOP6mo ago
Okay.
and
and6mo ago
Listen to the Events.MessageCreate This returns the message object, check if message.content starts with your prefix and then do whatever discord.js should have guides on events if you don't know yet how they work
Skull
SkullOP6mo ago
How would i set the logic? This is what i had. module.exports = { name: 'messageCreate', async execute(message) { if (message.author.bot) return; const prefix = '!'; if (!message.content.startsWith(prefix)) return; const args = message.content.slice(prefix.length).trim().split(/ +/); const commandName = args.shift().toLowerCase(); if (commandName === 'jdad') { message.reply('Here’s your joke, dad 😎'); } } };
and
and6mo ago
And where does that function get called? The part you showed looks correct
Skull
SkullOP6mo ago
Wdym wheres it get called? If you mean the file, I dont know what itd be called.
Amgelo
Amgelo6mo ago
functions don't call themselves you must call them somewhere djs doesn't include any file splitting functionality, that's for you to code
Skull
SkullOP6mo ago
So how do i call it? I have an event handler in “index.js”
Amgelo
Amgelo6mo ago
then it'd be called there
Skull
SkullOP6mo ago
Now how does that work? Are they only called once? Or is it each time a user sends a message?
Amgelo
Amgelo6mo ago
you're supossed to understand your own code the messageCreate event triggers on every message now, whether you coded that execute function to be called on every event of that name, or just the first, it'd be in your code
Skull
SkullOP6mo ago
I’m actually not at my pc right now. Can i @ you when i get home and show you?
Amgelo
Amgelo6mo ago
just send it here but still, you should know what your code does
Skull
SkullOP6mo ago
I might have my files here.
Amgelo
Amgelo6mo ago
not just rely on someone else to understand and explain it to you
Skull
SkullOP6mo ago
I learn easier being taught by someone.
Amgelo
Amgelo6mo ago
but how did you write it if you don't know what it does
Skull
SkullOP6mo ago
I used the guide to build index and the commands. The stuff i have now was made from the guide.
Amgelo
Amgelo6mo ago
and the guide explains each step before you have the full script you should use github instead of that specially since you have your node_modules as well and you leaked your token as well, so I'll delete your message
Skull
SkullOP6mo ago
Right.
Amgelo
Amgelo6mo ago
just in case you should reset it imo
Skull
SkullOP6mo ago
I will
Amgelo
Amgelo6mo ago
looks like it'll be called every time it receives an event of that name
No description
Amgelo
Amgelo6mo ago
client is an EventEmitter
Skull
SkullOP6mo ago
Okay. Ill talk more when im home. So i have a question about this command Mention me
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
I was told the logic is correct, but when i start the bot and message !jdad, it doesnt work
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
One sec /events/messageCreate.js/ module.exports = { name: 'messageCreate', async execute(message) { if (message.author.bot) return; const prefix = '!'; if (!message.content.startsWith(prefix)) return; const args = message.content.slice(prefix.length).trim().split(/ +/); const commandName = args.shift().toLowerCase(); if (commandName === 'jdad') { message.reply(Here’s your joke, dad 😎); } } };
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
pulled in index.js or this one?
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
/*index.js*/ const fs = require('node:fs'); const path = require('node:path'); const { Client, Collection, GatewayIntentBits } = require('discord.js'); const { token } = require('./config.json'); const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.commands = new Collection(); const foldersPath = path.join(__dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); for (const folder of commandFolders) { const commandsPath = path.join(foldersPath, folder); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); } else { console.log([WARNING] The command at ${filePath} is missing a required "data" or "execute" property.); } } } const eventsPath = path.join(__dirname, 'events'); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); for (const file of eventFiles) { const filePath = path.join(eventsPath, file); const event = require(filePath); if (event.once) { client.once(event.name, (...args) => event.execute(...args)); } else { client.on(event.name, (...args) => event.execute(...args)); } } client.login(token); i dont think so?
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs6mo ago
Codeblocks: ```js const Discord = require("discord.js"); // further code ``` becomes
const Discord = require("discord.js");
// further code
const Discord = require("discord.js");
// further code
Inline Code: `console.log('inline!');` becomes console.log('inline!');
Skull
SkullOP6mo ago
oh sorry
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
How would I add the GuildMessages and MessageContent intents?
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
so like this?
/*index.js*/

const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log([WARNING] The command at ${filePath} is missing a required "data" or "execute" property.);
}
}
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}

client.login(token);
/*index.js*/

const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log([WARNING] The command at ${filePath} is missing a required "data" or "execute" property.);
}
}
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}

client.login(token);
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
Oh yeah
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
I wanted to try something else. Okayt
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
Im using these as like separate commands
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
Ik. I just wanted this.
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
It works I found the prefix by asking around.. like ai
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Skull
SkullOP6mo ago
It seems to work fine for me..

Did you find this page helpful?