Hey guys, I want to make a bot for my server only just to show how many <word> are on the server (since the bot has been invited, not earlier it's ok), and to do a calculation with that value. So I need 2 commands.
For now, I followed the discord.js guide (very useful actually), but the guide don't indicate me what I want.
I swear I read lot of sites and discord posts to resolve my problems but I didn't find anything who works (it's always failing at one moment. Ah, and english is not my native language so I have some difficulty to understand absolutely all what is saying).
Well, here is my index.js, like on the discord.js guide, and my command file. As I said, I followed the guide at first and then I tried to find how can I do what I want.
I hope some nice people will help me, I really don't where to go at this point. Thanks.
#Make a bot counting a word on the server (v14.7.0)
166 messages · Page 1 of 1 (latest)
• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.
I use v18.12.1 for node
Ah, and if there is a french person passing here, I will be very enchanted 👀
you need MessageContent intent
I think I need to back up the value "count" from index.js to dukou.js but idk how
in index.js ?
also dont nest listeners
in your client constructor
Yes but then ? Because I already did tests with MessageContent and Idk where I had to go with that so it always failed
same what you did with Guilds.
I have to create a new client ?
Anyway, counting is another problem, here my main problem is to take a value from index.js and export it to dukou.js, to show this value when we use this command
intents is an array dude
basic js here
... Yeah I know but you said "like you did with Guilds", I only use Guilds to create client
Maybe, but everytime I found a solution it didn't work for me
Say it if you're annoyed with me :')
set module.exports to the count
Yes ok but it does the same
My problem is to incremate count every time the bot sees the word AND the command has to show this value, but idk how to make the link between index.js and dukou.js.
My error in cmd is this :
[WARNING] The command at C:(blabla)\commands\dukou.js is missing a required "data" or "execute" property.
you shouldnt use nested listeners instead push the count to the array and get the value
why push it to the array
oh wait, i didnt read the last one
instead use a global variable from client and increment count every time
and you can access it from everywhere you want
bad practice
Ok that's better, thank you very much ^^
So now, this line :
await interaction.reply("du coup" has been used ${count} times from (date));
is still correct ?
the command replies " "du coup" has been used undefined times from (date)", so apparently there is something I still have to do
.
what you mean its a bad practice
its a best practice instead of exporting the value
its not
its better to use a global variable like
client.count = 0;
instead of exporting the value like this
not a global variable though
Actually, using exports and import always gave me errors like "fatal: Parsing error: 'import' and 'export' may only appear at the top level" or "(node:15612) Warning: Accessing non-existent property 'count' of module exports inside circular dependency", so I prefer global variable
...
see
literally just read what the first error says
if yall cant deal with simplest error messages i cant help ya
well i think you can use exports.count thing but you need to import it like this
import { count } from '...';
or you can use this and export the client then you can access it by client.count
client.count = 0;
... Thanks I always solved but what I mean is that it didn't worked everytime
choose either one which one is better for you
why would u export the client
why do people never use interaction.client
client is EVERYWHERE in djs
^ this also can
because i never realized interaction.client exist
Ok so now I have this, but it says me "TypeError: Cannot read properties of undefined (reading 'count')"
show me your index file
I just changed that
no bro
:(
what your do is correct
exports.count = count;
I'm so sorry, it seems to be simple but it's an enigma for me
Ah ?
but your importing it wrong
instead
// this a default import
import count from '...';
// use this instead
import { count } from '...';
But you said I had to use global values
ya but thats not what i tell you to do it
if you're using a variable from client you dont have to export it so you can use it like this
client.count = client.count + 1;
Ah, and this said "import { count } from '../index.js';" to me ._.
Ohhhh okay
ya since you're exporting it like
exports.count = count;
shit, I paste the wrong thing
it says "fatal: Parsing error: 'import' and 'export' may appear only with 'sourceType: module'"
sorry
show me the updated code
const { SlashCommandBuilder } = require('discord.js');
import { count } from '../index.js';module.exports = {
data: new SlashCommandBuilder()
.setName('dukou')
.setDescription('Compte le nombre de "du coup" écrits dans le serveur'),
async execute(interaction, client) {
await interaction.reply("du coup" has been used ${client.count} times from (date));
},
};
your index
client.count = 0;
client.on('ready', () => {
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing });
client.on('message', (message) => {
if (message.content.includes('du coup')) {
client.count = client.count + 1;
exports.count = count;
}
});
});
you dont have to export it
instead use this
interaction.client.count
I'm almost there, now the command says " "du coup" has been used 0 times from (date)"
then no one is used it
If I reach what I want because of you, I will always remember you bro
No I mean, I did tests (typing "du coup" while the bot runing), but it shows "0" times
can you try log client.count everytime you used du coup
well nothing happens in log...
and it shows me "undefined" instead of a number
can you show both updated code
client.counts = new Collection();
client.on('ready', () => {
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing });
client.on('message', (message) => {
if (message.content.includes('du coup')) {
client.count = client.count + 1;
console.log(client.count);
}
});
});
for my index
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('dukou')
.setDescription('Compte le nombre de "du coup" écrits dans le serveur'),
async execute(interaction) {
await interaction.reply(`"du coup" has been used ${interaction.client.count} times from (date)`);
},
};```
for my dukou
Codeblocks:
```js
const Discord = require("discord.js");
// further code
```
becomes
const Discord = require("discord.js");
// further code
Inline Code:
`console.log('inline!');` becomes console.log('inline!');
thanks
why collection....
@fathom radish do you have GuildMessages and MessageContent intents?
this means you need to add them if you dont have them
i just realized he doesnt have this intents
well @fathom radish you need to know when your bot crashed or something it will reset back to 0 times
The problem is, when I read I had to use them, I added them yes but in fact I didn't have to use them so it showed me "no-unused-vars: 'GuildMessages' is assigned a value but never used."
you need to use it on Client intents array
Yeah to keep the value I have to make a db I think
ye
Okay I just saw where to put them, I'm sorry
Ok now I have a problem with something I didn't see until now : "RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined."
Ew the new font hurt my eyes
show the updated code
const fs = require('node:fs');
const path = require('node:path');
// Require the necessary discord.js classes
const { Client, Collection, GatewayIntentBits, ActivityType, GuildMessages, MessageContent } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds, GuildMessages, MessageContent] });
// Events
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));
}
}
// Commands
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
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);
// Set a new item in the Collection with the key as the command name and the value as the exported module
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.`);
}
}
// initialize count
client.counts.set('count', 0);
client.on('ready', () => {
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing });
client.on('message', (message) => {
if (message.content.includes('du coup')) {
client.counts.set('count', client.counts.get('count') + 1);
console.log(client.count);
}
});
});
// Log in to Discord with your client's token
client.login(token);
bro you need to use
GatewayIntentBits.GuildMessages
GatewayIntentBits.MessageContent
and remove GuildMessages, MessageContent from discord.js
aaahhhh
Ok but I have always this "TypeError: Cannot read properties of undefined (reading 'set')"
for this line client.counts.set('count', 0);
remove it and use client.count++
define ```js
client.count = 0;
and also what is your discord.js version ?
Determining your discord.js version:
• npm list discord.js
• Make sure you use the right documentation for your installed verison (selector on the left)
Well, with "${interaction.client.counts}" it always shows me "undefined"
14.7.0
change it to interaction.client.count
if you're already defined this
It's always 0 now ;w;
// initialize count
client.count = 0;
client.on('ready', () => {
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing });
client.on('message', (message) => {
if (message.content.includes('du coup')) {
client.count++;
console.log(client.count);
}
});
});```
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('dukou')
.setDescription('Compte le nombre de "du coup" écrits dans le serveur'),
async execute(interaction) {
await interaction.reply(`"du coup" has been used ${interaction.client.count} times from (date)`);
},
};
your event name change from message to messageCreate
and also dont nested listeners instead do this
client.on('ready', ...);
client.on('messageCreate', ...);
Well when I try this for both it says "fatal: Parsing error: Unexpected token (blabla)"
show me the updated code
make sure your syntax is correct
I try multiple things, like that
client.on('ready', () => {
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing });
client.on('message', (messageCreate),
if (messageCreate.content.includes('du coup')) {
client.count++;
console.log(client.count);
}
));
));```
shit no
uh
no bro
client.on('ready',
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing }),
client.on('message', (messageCreate),
if (messageCreate.content.includes('du coup')) {
client.count++;
console.log(client.count);
}
));
));```
i told you to do this
Yes but it changed nothing
client.on('ready', ...);
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing });
client.on('messageCreate', ...);
if (messageCreate.content.includes('du coup')) {
client.count++;
console.log(client.count);
}
});
});```
what
thats a wrong syntax
client.on('ready', () => {
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing });
});
you need to learn how syntax work on javascript
i suggest you to read the guide before code
That's exactly what I did before
yes but you have nested message event listeners
Ok I just understood what you mean...
But now I have this
client.on('messageCreate',
if (messageCreate.content.includes('du coup')) {
client.count++;
console.log(client.count);
}
);```
And it says "fatal: Parsing error: Unexpected token if"
But I already read it ;w;
not for voice but the others
thats still a wrong syntax
client.on('messageCreate', (messageCreate) => {
// rest code here
});
I thought you told to not do that...
Anyway, now I have this again "TypeError: Cannot read properties of null (reading 'setActivity')"
I tried to do client.on('ready', () => { again but it didn't work
updated code ?
client.count = 0;
client.on('ready',
client.user.setActivity('calculer des trucs', { type : ActivityType.Playing }),
);
client.on('messageCreate', (messageCreate) => {
if (messageCreate.content.includes('du coup')) {
client.count++;
console.log(client.count);
}
});
the ready is wrong syntax
Yea I saw but Idk where
it should be a same thing like messageCreate but without the messageCreate parameter
(messageCreate) -> ()
Ok I just saw there was the problem
AND IT WORKS
Omg I will stop here for tonight
Thank you very very much, you were my savior @robust relic
do note that this isn't really a server for basic javascript support
I didn't know it was a "basic javascript" problem sorry
syntax issues are very much lack of knowledge of the language
Ok bro I understood don't be rude wtf
i'm not rude

Getting Started: Introduction