This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
messageHandler.js
67 lines (60 loc) · 2.18 KB
/
messageHandler.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
60
61
62
63
64
65
66
67
const Discord = require('azrael-djs');
const handler = {};
let ClientID = null,
Guild = null;
let addTextChannel = (textChannel, title, body, color, footer, thumbnail) => {
handler[textChannel] = {
title,
body,
color,
footer,
thumbnail
};
};
let add = (channel, title, body, color, footer, thumbnail) => {
switch (Guild.channels.cache.get(channel).type) {
case 'GUILD_CATEGORY':
Guild.channels.cache.filter(c => c.parentID === channel).forEach(textChannel => {
addTextChannel(textChannel.id, title, body, color, footer, thumbnail);
});
break;
case 'GUILD_TEXT':
addTextChannel(channel, title, body, color, footer, thumbnail);
break;
}
};
let sendMessage = channel => {
Guild.channels.cache.get(channel).send(new Discord.MessageEmbed({
title: handler[channel]['title'],
fields: [{
name: "** **",
value: handler[channel]['body']
}]
}).setColor(handler[channel]['color']).setThumbnail(handler[channel]['thumbnail']).setFooter(handler[channel]['footer']));
};
let deleteLastMessage = (channel, callback) => {
channel.messages.fetch({
limit: 100
}).then(messages => {
let lastMessage = messages.find(message => message.author.id === ClientID);
if (!lastMessage) return callback();
lastMessage.delete().then(() => callback());
});
};
module.exports = {
init: (clientId, guild, messages) => {
ClientID = clientId;
Guild = guild;
messages.forEach(item => {
if (item['channels']) return item['channels'].forEach(channel => {
add(channel, item['title'], item['body'], item['color'] || '#FFFFFF', item['footer'], item['thumbnail']);
});
add(item['channel'], item['title'], item['body'], item['color'] || '#FFFFFF', item['footer'], item['thumbnail']);
});
Object.keys(handler).forEach(channel => deleteLastMessage(Guild.channels.cache.get(channel), () => sendMessage(channel)));
},
handle: message => {
if (!handler[message.channel.id] || message.channel.lastMessage.author.id === ClientID) return;
deleteLastMessage(message.channel, () => sendMessage(message.channel.id));
}
};