Message listener only on certain channels?

Hey there! So I have this:
module.exports = {
name: Events.MessageCreate,
async execute(message)
{
message.fetch()
.then(console.log)
.catch(console.error);
}
};
module.exports = {
name: Events.MessageCreate,
async execute(message)
{
message.fetch()
.then(console.log)
.catch(console.error);
}
};
This apparently listens to every channel the bot has access to. Can I restrict it to just one channel or some channels? I can probably check the channel id within the body of execute(message) but I wonder if there's a method that only listens to certain channels in the first place.
8 Replies
d.js toolkit
d.js toolkit4mo 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! - Marked as resolved by OP
inkihh
inkihh4mo ago
$ npm list discord.js
bot@1.0.0 /var/www/trivia/bot
└── discord.js@14.14.1
$ npm list discord.js
bot@1.0.0 /var/www/trivia/bot
└── discord.js@14.14.1
$ node -v
v20.12.2
$ node -v
v20.12.2
chewie
chewie4mo ago
There is no extra method. You already mentioned the correct way in your question. Also fetching the message in that event isn't necessary.
inkihh
inkihh4mo ago
So if a bot is added to a Discord with dozens of channels, Events.MessageCreate would get all messages of the whole server? that seems like a huge overhead, if I only want to monitor the messages in one channel. I remember in the old days you could only listen to one channel, but I can't remember how
chewie
chewie4mo ago
You cant. That event emits every time the bot receives a message, doesn't matter which channel. Always has been like that. What you are probably referring to is using channel.createMessageCollector(), which only collects messages in that channel, but its basically the same as using the messageCreate event and checking the channel.
inkihh
inkihh4mo ago
well but with channel.createMessageCollector() I would save a lot of overhead, no? in terms of cpu on the backend
chewie
chewie4mo ago
that listens to the messageCreate event internally as well, there won't be a difference.
inkihh
inkihh4mo ago
oh ok thank you!