-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
329 lines (194 loc) · 8.13 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//basic constants and functions
var checked;
var checking = [];
const Discord = require("discord.js");
const {usernameorid, token,usernamesp, roles, apiKey, spreadsheetId, range, unremoveableroles, refreshrate} = require("./config/discord.json");
const { google } = require("googleapis");
const connection = google.sheets({
version: "v4",
auth: apiKey
});
const client = new Discord.Client();
let oldRows = [];
//checking for errors
const logError = error => {
console.error("error log " + new Date());
console.error(error);
};
//the spreadsheet fetching
const fetchRows = async (spreadsheetId, range, sheetsConnection) => {
try {
const response = await sheetsConnection.spreadsheets.values.get({
spreadsheetId: spreadsheetId,
range: range
});
//fetching all data
const rows = response.data.values;
//dividing the data between different array using while loop
var i = 0;
var k=0;
while (i < rows.length) {
const checkrow="";
const rows123= rows[i]
//finally putting all roles together
const therolenames = rows123[1]+","+rows123[2]+","+rows123[3]+","+rows123[4];
assignRoles(rows123[0].trim(), therolenames.trim());
rows123[0]==null;
rows123[1]==null;
rows123[2]==null;
rows123[3]==null;
rows123[4]==null; rows123[1]==null;
roles[1]==null;
roles[2]==null;
roles[3]==null;
roles[4]==null;
therolenames==null;
roles.length=0;
rows123.length=0;
i++;
k++;
}
return rows;
} catch (error) {
logError(error);
}
};
const assignRoles = async (usernames, roleNames) => {
var rolenames1 = roleNames.split(",");
try {
const guildMembers = client.guilds.array()[0].members.array();
//number of all members in the group
const guildRoles = client.guilds.array()[0].roles.array();
// deleting unremovable roles from the list
const Roles2 = guildRoles.filter(Role => !unremoveableroles.includes(Role.name));
// * getting Role instances of role names
const removedRoles = guildRoles.filter(Role => unremoveableroles.includes(Role.name));
const Roles = guildRoles.filter(Role => roleNames.includes(Role.name));
//role number of all members in the group
roleNames.length=0;
// * getting GuildMember instances of usernames and setting roles
guildMembers.forEach(async member => {
const username= member.user.username + "#" + member.user.discriminator;
//check if the first column of spreadsheet matches the userid/username of discord
var UNcheck;
if (usernameorid=="UN"){
UNcheck=username
}else{
UNcheck=member.id
}
if (usernames.includes(UNcheck)) {
const notAssignedRoles = [];
Roles.forEach(role => {
//if (!member.roles.array().includes(Roles)) {
//if (!guildRoles.includes(Roles)) {
checked="no"
//Check whether the roles (discord) are in roles (spreadsheet)
if (Roles.includes(member.roles.array()[1])|| removedRoles.includes(member.roles.array()[1])|| member.roles.array()[1]==null){
}else{checked="yes" }
if (Roles.includes(member.roles.array()[2]) || removedRoles.includes(member.roles.array()[2]) || member.roles.array()[2]==null){
}else{checked="yes"; }
if (Roles.includes(member.roles.array()[3]) || removedRoles.includes(member.roles.array()[3])|| member.roles.array()[3]==null){
}else{checked="yes"; }
if (Roles.includes(member.roles.array()[4])|| removedRoles.includes(member.roles.array()[4]) || member.roles.array()[4]==null){
}else{checked="yes"; }
//Check whether the roles (Spreadsheet) are in roles (Discord)
if (member.roles.array().includes(Roles[0])|| removedRoles.includes(Roles[0]) || Roles[0]==null){
}else{checked="yes"; }
if (member.roles.array().includes(Roles[1]) || removedRoles.includes(Roles[1])|| Roles[1]==null){
}else{checked="yes"; }
if (member.roles.array().includes(Roles[2]) || removedRoles.includes(Roles[2]) || Roles[2]==null){
}else{checked="yes"; }
if (member.roles.array().includes(Roles[3])|| removedRoles.includes(Roles[3]) || Roles[3]==null){
}else{checked="yes"; }
if (checked=="yes"){
notAssignedRoles.push(role);
}
});
if (notAssignedRoles.length > 0) {
await member.removeRoles(Roles2); member.addRoles(Roles);
console.log(
"Assigned " +
notAssignedRoles.map(role => role.name) +
" to " +
username + " ["+member.id+"] on " + new Date().toString()
);
} else {
if (Roles[0]==null && Roles[1]==null && Roles[2]==null && Roles[3]==null) {
member.removeRoles(Roles2);
console.log(username + " ["+member.id+"] has all roles removed on"+ new Date().toString());
}else{
console.log(username + " ["+member.id+"] already has all the roles assigned, checked on "+ new Date().toString());
}
}
}
});
} catch (err) {
logError(err);
}
};
const extractNewEntries = (oldRows, rows) => {
let newRows = [];
if (rows.length > oldRows.length) {
newRows = rows.slice(oldRows.length);
}
newRows.forEach(row => {
oldRows.push(row);
});
return newRows;
};
const extractDiscordIDs = rows => {
return rows.map(user => user[0]);
console.log(rows.map(user => user[0]));
};
client.once(
"ready",
() => {
console.log("Bot started with these settings:");
console.log("• Spreadsheet ID: ");
console.log("• Range: " + range);
console.log("• Roles: " + roles.join(", "));
setInterval(async () => {
console.log("\nChecked for new entries on " + new Date().toString());
let rows;
try {
rows = await fetchRows(spreadsheetId, range, connection);
const newEntries = extractNewEntries(oldRows, rows);
if (newEntries.length > 0) {
console.log(`Found ${newEntries.length} new entries`);
const usernames = extractDiscordIDs(newEntries);
} else {
console.log("No new entries");
}
} catch (err) {
if (rows === undefined) console.error("Google Sheet is empty");
logError(err);
rows = [];
}
}, refreshrate); // refresh rate: 60000 milliseconds == 1 minute
},
logError
);
client.on("error", logError);
const start = async () => {
try {
await client.login(token);
} catch (err) {
logError(err);
setTimeout(start, 30000);
}
};
start();
client.on("disconnect", start);
module.exports = {
fetchRows,
assignRoles,
extractNewEntries,
extractDiscordIDs
};
const express = require("express");
const fs = require("fs");
const app = express();
app.use(express.static("public"));
const listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});