0
votes

The title says all. I'm making a Discord bot in node.js, and one part that I'm trying to add is a .setup command, to make the bot less dependent on manually changing the value of client.channels.get().send(), allowing it to be more easily set up in the future.

Anyway, in my case, I'm trying to have the user reply to a message with a channel mention (like #welcome for example), and have the bot return that ID and save it to a variable.

Currently, I have this:

function setupChannel() {
  client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
  client.on('message', message => {
    checkChannel = message.mentions.channels.first().id;
    client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to that channel.');
  });
}

message.mentions.channels returns undefined.
I know that the message.mentions.channels.first().id bit works when its a user instead of a channel, is there a different way of getting a channel mention in a message?

2
Normally message.mentions.channels.first().id should workGilles Heinesch
First, I would add a check if there is a mention of a channel, if yes send a message, if not do nothing or whatever you wantGilles Heinesch
@GillesHeinesch this, however, is a bad idea if you want multiple channel mentions in your message as they don't get sorted by mention order but by channel age.Caltrop

2 Answers

1
votes

Here is the most easy and understandable way i found :

message.guild.channels.cache.get(args[0].replace('<#','').replace('>',''))

But it not really what we want we could use <#####9874829874##><<<547372>> and it will work. So i figured out this :

message.guild.channels.cache.get(args[0].substring(2).substring(0,18))
0
votes

I like to just remove all non-integers from the message, as this allows for both IDs and mentions to be used easily, and it will return the ID for you from that, as mentions are just IDs with <# and > around them. To do this you would use

message.content.replace(/\D/g,'')

With this, your code would look like this:

function setupChannel() {
  client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
  client.on('message', message => {
    checkChannel = message.content.replace(/\D/g,'');
    client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to <#' + checkChannel + '>.');
  });
}

From this you will always get an ID. And we can surround the ID to trun it into a mention.