-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (49 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config');
const webhooks = []
function sendNewMessage(message) {
webhooks.forEach((hook) => {
if (hook.channelID == message.channel.id) return;
const avatarURL = message.author.avatarURL();
let username = "Channel Sync";
if (Boolean(message.member.nickname)) {
username = message.member.nickname
} else {
username = message.author.username;
}
hook.send(`**From ${message.guild.name}**\n${message.content}`, { username, avatarURL });
});
}
async function getWebhook(name, channelId) {
const hooks = await client.channels.cache.get(channelId).fetchWebhooks();
const hook = hooks.find(hook => hook.name == name);
return hook;
}
async function createWebhooks() {
Object.keys(config.sync).forEach(async (key) => {
const hook = await getWebhook("Channel Sync", config.sync[key]);
if (!Boolean(hook)) {
client.channels.cache.get(config.sync[key]).createWebhook('Channel Sync', {
avatar: 'https://cdn.discordapp.com/embed/avatars/1.png',
reason: 'Created by Channel Sync'
})
.then((webhook) => webhooks.push(webhook))
.catch((err) => console.log(err));
} else {
webhooks.push(hook);
}
});
}
client.on('message', async (message) => {
if (message.author.bot) return;
if (config.sync[message.guild.id] == message.channel.id) {
if (message.content == "") return;
sendNewMessage(message);
} else return;
});
client.on('ready', async () => {
console.log('bot logged in');
await createWebhooks();
});
client.login(config.discord.token);