lightvng
lightvng

Reputation: 15

Is there a way to allow my bot to check DMs and post them in a channel in my server?

How would I make my bot send a message to a specific channel with the message when someone DMs the bot? I was thinking about webhooks, but I’m not sure if that’s the right way to go.

Upvotes: 1

Views: 4226

Answers (1)

JackRed
JackRed

Reputation: 1204

Yes you can.
Now how?

You have first to get the dm message. The Message has the property channel which represent the channel the the message was received in. This property can be of 3 different types, all extending from Channel which have a type property.
This type property can have 6 values:

  • dm - a DM channel
  • group - a Group DM channel
  • text - a guild text channel
  • voice - a guild voice channel
  • category - a guild category channel
  • news - a guild news channel
  • store - a guild store channel

And from the property Message.channel have this indication:

Type: TextChannel or DMChannel or GroupDMChannel

So here we have 3 possible result for message.channel.type: "dm", "text", "group".

Once you've checked if the message is a dm or not, you have to copy it to your server. Again, the Message type have interesting properties for us: content and attachments.

The way to handle attachments is a bit more complicated than the content itself. You'll have to look for the type MessageAttachment and use its property, as url.

But for content it's really easy, it's just a string. So we just have to get our channel and send the message.

In the example below, I get the guild and the server by using their ID. You can hardcode them, put them in a json file, in a database or get them in your message and make the dm a command, like:
+send ID a message with many words.

let channelID = "X";
let guildID = "X";
client.on('message', (message) => {
  if(message.channel.type === 'dm'){
    let embed = new Discord.RichEmbed()
    .setAuthor(client.guilds.get(guildID).members.get(message.author.id).displayName, message.author.displayAvatarURL)
    .setColor('#FAA')
    .setDescription(message.content);
    client.channels.get(channelID).send(embed);
  }
});

enter image description here
enter image description here

Upvotes: 2

Related Questions