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
- 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!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
Awh man. Okay.
Thanks for letting me know.
You said id have to write my own logic? Would it be in JavaScript?
everything you do with djs is in js
Okay.
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
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 where does that function get called?
The part you showed looks correct
Wdym wheres it get called?
If you mean the file, I dont know what itd be called.
functions don't call themselves
you must call them somewhere
djs doesn't include any file splitting functionality, that's for you to code
So how do i call it? I have an event handler in “index.js”
then it'd be called there
Now how does that work? Are they only called once?
Or is it each time a user sends a message?
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
I’m actually not at my pc right now. Can i @ you when i get home and show you?
just send it here
but still, you should know what your code does
I might have my files here.
not just rely on someone else to understand and explain it to you
I learn easier being taught by someone.
but how did you write it if you don't know what it does
I used the guide to build index and the commands. The stuff i have now was made from the guide.
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
Right.
just in case you should reset it imo
I will
looks like it'll be called every time it receives an event of that name

client is an EventEmitter
Okay.
Ill talk more when im home.
So i have a question about this command
Mention me
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
I was told the logic is correct, but when i start the bot and message !jdad, it doesnt work
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
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•6mo ago
Message Not Public
Sign In & Join Server To View
pulled in index.js or this one?
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
/*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•6mo ago
Message Not Public
Sign In & Join Server To View
Codeblocks:
```js
const Discord = require("discord.js");
// further code
```
becomes
Inline Code:
`console.log('inline!');` becomes
console.log('inline!');oh
sorry
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
How would I add the GuildMessages and MessageContent intents?
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
so like this?
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Oh yeah
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
I wanted to try something else.
Okayt
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Im using these as like separate commands
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Ik. I just wanted this.
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
It works
I found the prefix by asking around.. like ai
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
It seems to work fine for me..