-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
302 lines (284 loc) · 8.69 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const Discord = require("discord.js");
require("dotenv").config();
const jsonfile = require("jsonfile");
const welcomeMessageFile = "./messageFiles/welcome.json";
const configFile = "./configuration.json";
const tools = require("./tools");
// Create an instance of a Discord client
const client = new Discord.Client();
// The token of your bot - https://discordapp.com/developers/applications/me
const token = process.env.BOTTOKEN;
let botMessages = [];
let botConfig = null;
// The ready event is vital, it means that your bot will only start reacting to information
// from Discord _after_ ready is emitted
client.on("ready", () => {
jsonfile.readFile(welcomeMessageFile, function(err, obj) {
if (err) {
console.log("No file");
botMessages = [];
} else {
botMessages = obj;
}
readConfig();
});
});
client.on("guildDelete", (guild) => {
removeBotMessage(guild.id);
console.log("Deleted Guild");
});
client.on("channelDelete", (channel) => {
if (channel.name === botConfig.welcomeChannelName) {
removeBotMessage(channel.guild.id);
console.log("Deleted Channel");
}
});
const getRoleMessageIds = (guildId) => {
for (const msg of botMessages) {
if (msg.guildId === guildId) {
return msg.messages;
}
}
return null;
};
// Create an event listener for messages
client.on("message", (message) => {
if (message.author.bot) {
return;
}
const str = message.content;
if (str.startsWith("!editBotMessage")) {
editBotMessage(message, str);
return;
}
for (const msg of botConfig.serverMessages) {
if (str === msg.reactMessage) {
// send answer from configuration json
message.channel.send(msg.botAnswer);
}
break;
}
if (str === "what is my avatar") {
// Send the user's avatar URL
message.reply(message.author.avatarURL);
}
});
client.on("messageReactionAdd", (reaction, user) => {
if (
!user.bot &&
roleMessageExist(reaction.message.guild.id, reaction.message.id)
) {
const roleUser = reaction.message.guild.members.get(user.id);
for (const welcomeText of botConfig.welcomeChannelMessages) {
for (const role of welcomeText.roles) {
const icon = tools.findEmoji(role.icon, client);
if (icon === reaction.emoji.name || icon.name === reaction.emoji.name) {
roleUser.addRole(roleUser.guild.roles.find("name", role.roleName));
const msgText = role.roleSetText.replace("[username]", user.username);
reaction.message.channel.send(msgText).then((msg) => {
msg.delete(15000);
});
}
}
}
}
});
client.on("messageReactionRemove", (reaction, user) => {
if (
!user.bot &&
roleMessageExist(reaction.message.guild.id, reaction.message.id)
) {
const roleUser = reaction.message.guild.members.get(user.id);
for (const welcomeText of botConfig.welcomeChannelMessages) {
for (const role of welcomeText.roles) {
const icon = tools.findEmoji(role.icon, client);
if (icon === reaction.emoji.name || icon.name === reaction.emoji.name) {
roleUser.removeRole(roleUser.guild.roles.find("name", role.roleName));
const msgText = role.roleRemoveText.replace(
"[username]",
user.username
);
reaction.message.channel.send(msgText).then((msg) => {
msg.delete(15000);
});
}
}
}
}
});
const editBotMessage = (message, str) => {
const roleUser = message.guild.members.get(message.author.id);
if (!roleUser.hasPermission("ADMINISTRATOR ")) {
message.channel.sendMessage("Nice try, Mr. Non-Administrator");
return;
}
const num = parseInt(str.substring(str.indexOf("[") + 1, str.indexOf("]")));
if (num && num > 0) {
const ids = getRoleMessageIds(message.guild.id);
const channels = message.guild.channels;
const welcomeChannel = channels.find("name", botConfig.welcomeChannelName);
if (welcomeChannel) {
welcomeChannel.fetchPinnedMessages().then((stickies) => {
if (stickies) {
const stickyMsg = stickies.find("id", ids[num - 1]);
if (stickyMsg) {
stickyMsg
.edit(str.substr(str.indexOf("]") + 1, str.length - 1))
.then((msg) => {
console.log(`New message content: ${msg}`);
fetchCachedMessages(welcomeChannel);
})
.catch(console.error);
}
}
});
}
}
};
const roleMessageExist = (guildId, messageId) => {
for (const msg of botMessages) {
if (msg.guildId === guildId) {
for (const msgId of msg.messages) {
if (messageId === msgId) {
return true;
}
}
}
}
return false;
};
const sendWelcomeMessage = (welcomeChannel, msg) => {
welcomeChannel.send(msg.text).then((newMessage) => {
newMessage.pin().then((myMessage) => {
let found = false;
let guildFound = false;
let messageToAddGuild = null;
for (const roleMsg of botMessages) {
if (roleMsg.guildId === myMessage.guild.id) {
guildFound = true;
messageToAddGuild = roleMsg;
for (const roleMsgId of roleMsg.messages) {
if (roleMsgId === myMessage.id) {
found = true;
break;
}
}
}
}
if (!found) {
if (!guildFound) {
botMessages.push({
messages: [myMessage.id],
guildId: welcomeChannel.guild.id
});
} else {
messageToAddGuild.messages.push(myMessage.id);
}
}
jsonfile.writeFile(welcomeMessageFile, botMessages, (err) => {
if (err) {
console.error(err);
}
});
for (const role of msg.roles) {
const icon = tools.findEmoji(role.icon, client);
myMessage.react(icon);
}
});
});
};
const checkGuildRoles = (guild) => {
for (const welcomeText of botConfig.welcomeChannelMessages) {
for (const role of welcomeText.roles) {
const roleExist = guild.roles.find("name", role.roleName);
if (!roleExist) {
guild.createRole({
name: role.roleName,
color: role.color
});
}
}
}
};
const fetchCachedMessages = (welcomeChannel) => {
welcomeChannel
.fetchMessages()
.then((cachedMessages) => {
cachedMessages.forEach(function(cmsg, csmgId) {
cmsg.reactions.forEach(function(reaction, reactionId) {
reaction.fetchUsers();
});
});
})
.catch(console.error);
};
const removeBotMessage = (guildId) => {
botMessages.forEach((ele, index) => {
if (ele.guildId === guildId) {
botMessages.splice(index, 1);
jsonfile.writeFile(welcomeMessageFile, botMessages, (err) => {
if (err) {
console.error(err);
}
return;
});
}
});
};
const readConfig = () => {
jsonfile.readFile(configFile, function(err, obj) {
if (err) {
console.log("No config file");
} else {
botConfig = obj;
for (const configGuild of botConfig.yourServerNames) {
const guild = client.guilds.find("name", configGuild);
if (guild) {
checkGuildRoles(guild);
const channels = guild.channels;
const welcomeChannel = channels.find(
"name",
botConfig.welcomeChannelName
);
if (welcomeChannel) {
fetchCachedMessages(welcomeChannel);
welcomeChannel.fetchPinnedMessages().then((stickies) => {
if (stickies) {
let roleMessageIds = getRoleMessageIds(guild.id);
if (!roleMessageIds) {
for (const msg of botConfig.welcomeChannelMessages) {
sendWelcomeMessage(welcomeChannel, msg);
}
} else {
if (!stickies.find("id", roleMessageIds[0])) {
removeBotMessage(guild.id);
for (const msg of botConfig.welcomeChannelMessages) {
sendWelcomeMessage(welcomeChannel, msg);
}
}
}
}
});
} else {
guild
.createChannel(botConfig.welcomeChannelName)
.then((newWelcomeChannel) => {
for (const msg of botConfig.welcomeChannelMessages) {
sendWelcomeMessage(newWelcomeChannel, msg);
}
});
}
} else {
console.log(
"Can't find " +
configGuild +
"! Check if configuration.json is configured properly and Bot is assigend to this Server"
);
}
}
console.log("I am ready!");
}
});
};
// Log our bot in
client.login(token);