Koza Nostra
Koza Nostra

Reputation: 196

How can I embed messages ,in multiple channels, using a Discord bot?

I was looking among the questions of the rest users and I found what I was searching for. My problem is that I want the same bot to do the same work on different channels with a different title, footer, etc...

The main code is:

const Discord = require("discord.js");

const client = new Discord.Client();

const channel = client.channels.cache.get("channel id");

client.on("ready", async () => {
    console.log(`Bot has started`);

    client.user.setPresence({ activity: { name: "some status " }, status: "online" });
});
client.on("message", (message) => {
    if (message.author.bot) 
    return;
    const embed = new MessageEmbed()
    .setDescription(message.content)
    .setTitle("")
    .setAuthor(message.author.tag,message.author.displayAvatarURL())
    .setImage(message.attachments.first().url)
    .setFooter("", "")
    .setTimestamp();
    channel.send(embed).catch(console.error);
});

client.login("token");

Upvotes: 0

Views: 585

Answers (1)

Cursed
Cursed

Reputation: 1003

First of all you can declare your message in object and after use them to send to different channels.

    //Here goes the dynamic setting for different channels
    const messages = {
      'IdChannel':{
        embed:{
          title:'foo',
          description: message.content,
          footer: {text:'foo'}
        }
      },
      'scndChannelID':{
        embed:{
          title:'bar',
          description: message.content,
          footer: {text:'bar'}
        }
      }
    }

Then on event message use it to send to channels you need that you decalare on object keys

      Object.keys(messagex).forEach((value,index) => {
        //Here can go the static things
        message.guild.channels.cache.get(value).send(
          {embed:{...messagex[value].embed,
          timestamp: new Date(),
          image: {url:""}
          }})
      })

Upvotes: 1

Related Questions