eris-msgs-backup
Version:
Forward your server messages to another server, with webhooks!
99 lines (92 loc) • 2.96 kB
JavaScript
const Eris = require("eris");
const Backup = async (client, g1, g2, opt) => {
if (!client.token)
return console.error("[ErisMsgsBackup Error]: Invalid client!");
if (!g1.id) g1 = client.guilds.get(g1);
if (!g1) return console.error("[ErisMsgsBackup Error]: Server not found!");
if (!g2.id) g2 = client.guilds.get(g2);
if (!g2)
return console.error("[ErisMsgsBackup Error]: Backup server not found!");
opt = Object.assign(
{
embeds: true,
files: true,
deleteWebhook: false,
autoCreateChannel: false
},
opt
);
console.log(`ErisMsgsBackup is Ready!`);
client.on("messageCreate", async message => {
if (!message.channel.guild) return undefined;
if (message.channel.guild.id == g1.id) {
let ch = g2.channels.find(
c => c.name == message.channel.name && c.type == 0
);
if (!ch) {
if (!opt.autoCreateChannel) return undefined;
ch = await g2.createChannel(
message.channel.name,
0,
"ErisMsgsBackup System."
);
}
let parent1 = g1.channels.get(message.channel.parentID);
let parent2 = g2.channels.find(
c => c.type == 4 && c.name == parent1.name
);
if (parent1 && !parent2) {
parent2 = await g2.createChannel(
parent1.name,
4,
"ErisMsgsBackup System."
);
}
let pp = g2.channels.get(ch.parentID);
if (!pp || parent1.name != pp.name) {
await ch.edit({ parentID: parent2.id });
}
let webhook;
if (!opt.deleteWebhook) {
let whs = await ch.getWebhooks();
webhook = whs[0];
}
if (!webhook) {
webhook = await ch.createWebhook({
name: message.author.username,
avatar: message.author.avatarURL
});
}
let options = {
content: message.cleanContent || "",
username: message.author.tag,
avatarURL: message.author.avatarURL
};
if (message.embeds[0] && opt.embeds == true) {
let em = message.embeds[0];
options.embeds = [em];
} else if (message.attachments[0] && opt.files == true) {
message.attachments.map(at => {
options.content += " " + at.url;
});
}
try {
await client.executeWebhook(webhook.id, webhook.token, options);
if (opt.deleteWebhook == true) {
await client
.deleteWebhook(webhook.id, webhook.token, "ErisMsgsBackup System.")
.catch(() => null);
}
} catch (e) {
console.error("[ErisMsgsBackup Error]: ", e);
}
}
});
/*client.on("channelUpdate", async (ch, old) => {
if (ch.type != 0 || ch.guild.id != g1.id) return undefined;
let ch2 = g2.channels.find(c => c.name == old.name && c.type == 0);
if (!ch2) return undefined;
await ch2.edit({ name: ch.name, nsfw: ch.nsfw });
});*/
};
module.exports = Backup;