How do I forward messages posted on different server to mine using discord.js?
I'm creating a bot on my server, I just need some kind of direction on how to do it or if its even possible.
The bot function is to copy the message that's being posted in different server and post it into my own server. Bare in mind that the bot is only on my server.
The code below forwards the message from text channel to another text channel in my own server. The main objective is to forward a message from different server into mine.
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'ODA3MjAyOD456ODE3NzU2Njcz.4GFDFHA.8MHQ9ZaGSFFFEDSJZQSgv-wB84'; //fake token
client.once('ready', () => {
console.log('The bot is online!');
});
client.on('message', message => {
if (message.channel.type != 'text' || message.author.bot || !message.content.startsWith('*'))
return;
if (message.content === '*pp') {
let channel = client.channels.cache.get('80565937858574763'); // gets the message from this channel
channel.messages.fetch({ limit: 1 }).then(messages => {
let lastMessage = channel.lastMessage.content;
const marketnews = client.channels.cache.get('807577438839489436096'); //sends it to this text channel
marketnews.send(lastMessage);
})
}
});
client.login(token);
Disclaimer: Channel ID and Token are examples and not real.
Solution 1:
its pretty easy, if message guild id is not your server id then it will send that message with information to your server text channel.
client.on("message", async (message) => { //Message Event
if (message.guild.id != "YOUR GUILD ID") { //If Guild ID Is Not Your Guild ID
const Channel = client.channels.cache.get("GUILD LOG MESSAGE CHANNEL ID"); //Finding Log Channel With ID
if (!Channel) return console.log("No Channel Found To Log!"); //If No Channel
const Embed = new Discord.MessageEmbed() //New Embed
.setColor("RANDOM") //Setting Color
.setTitle("New Message") //Setting Title
.setDescription(`Author - ${message.author.username} (${message.author.id})\nGuild - ${message.guild.name} (${message.guild.id})\nMessage -\n${message.content}`) //Description
.setTimestamp(); //Timestamp
return Channel.send(Embed); //Sending Embed To Channel
};
});
Links:
Guild#id
MessageEmbed