-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
server.js
273 lines (256 loc) · 11.4 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
//////////////////////////////////////////
// Discord Whitelist //
// Made By FAXES //
//////////////////////////////////////////
/// Config Area ///
var enableWhitelist = true; // Enable the Whitelist/Allowlist
var guildId = "DISCORD_GUILD_ID"; // Guild ID for a Discord Server, see https://faxes.zone/i/zjKnd.png
var botToken = "DISCORD_BOT_TOKEN"; // This must be a Discord bot token, create a bot @ https://discord.com/developers/applications
var whitelistRoles = [ // Roles by ID that are whitelisted.
"ROLE_ID"
];
var blacklistRoles = [ // Roles by Id that are blacklisted.
"ROLE_ID"
];
var notWhitelistedMessage = "You're Not Whitelisted. This sever is whitelisted and requires access to join.";
var noGuildMessage = "Guild Not Detected. It seems you're not in the guild for this community.";
var blacklistMessage = "You're blacklisted from this server.";
var debugMode = false; //
var cacheMaxTime = "1h"; // This is the time it takes for refreshes (cache) to have to reload when fetching Discord roles.
// Time System //
var enableTime = false; // allowed free-access but only for a certain time
var hour_min = 5; // minimum time of access to the server
var hour_max = 20; // maximum time of access to the server
// cmd //
RegisterCommand('Whitelist',(source,args, raw) => {
if(source == 0){
switch(args[0]){
case 0: enableWhitelist = false;
case 1: enableWhitelist = true;
default: console.log("The argument is not valid please put 1 or 0");
}
} else {
emit('chat:addMessage',{
args: [`Only the console can execute this command`]
})
}
})
/// Code ///
var work = true;
var cache = {};
const axios = require('axios').default;
const ms = require('ms');
axios.defaults.baseURL = 'https://discord.com/api/v9';
axios.defaults.headers = {
};
const version = '4.1.2'
getUserDiscord = async function(source, callback) {
if(typeof source == 'string') return source;
if(!GetPlayerName(source)) return false;
let arr = []
for(let index = 0; index <= GetNumPlayerIdentifiers(source); index ++) {
if(GetPlayerIdentifier(source, index)) {
arr.push(GetPlayerIdentifier(source, index))
}
}
setTimeout(() => {
const found = arr.find(e => e.startsWith('discord:'));
if(found) {
callback(found.replace('discord:', ''));
} else {
callback(null);
}
}, 500);
}
setTimeout(async () => {
let botAccount = await axios({
method: 'GET',
url: `https://discord.com/api/v9/users/@me`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bot ${botToken}`
},
}).catch(async (err) => {
console.log(`[${version}] ^5Bot token is incorrect, ensure your token is correct. ^1Stopping...^7`);
work = false;
});
if(botAccount.data) {
await axios({
method: 'GET',
url: `https://discord.com/api/v9/guilds/${guildId}`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bot ${botToken}`
},
}).catch(async (err) => {
console.log(`[${version}] ^5Guild ID is incorrect or bot isn't in guild. ^1Stopping...^7`);
console.log(`[${version}] ^5Invite: https://discord.com/api/oauth2/authorize?client_id=${botAccount.data.id}&permissions=1024&scope=bot ^7`);
work = false;
});
}
}, 2000);
// 0 = Not whitelisted
// 1 = Whitelisted
// 2 = Expired cache / refresh
// 3 = Blacklisted
function checkCache(userId) {
if(cache[userId]) { // Check cache
// Check timeout
if(Date.now() > cache[userId].timeAt) { // cache expired
return 2
} else {
switch(cache[userId].passed){
case 1: return 1
case 3: return 3
default: return 0
}
}
} else {
return 2
}
}
on('playerConnecting', async (name, setKickReason, deferrals) => {
if(!work) return;
if(!enableWhitelist) return;
let src = global.source;
deferrals.defer();
setTimeout(() => {
deferrals.update(`Hello ${name}. Your Discord ID is being checked with our whitelist.`)
setTimeout(async function() {
getUserDiscord(src, async function(userId) {
if(userId) {
let cacheCheck = await checkCache(userId);
switch(cacheCheck){
case 0:
if(debugMode) console.log(`[${version}] ^5${name} is not whitelisted.^7`)
return deferrals.done(notWhitelistedMessage);
case 1:
if(debugMode) console.log(`[${version}] ^5${name} was allowed into the server^7`)
return deferrals.done();
case 3 :
if(debugMode) console.log(`[${version}] ^5${name} is blacklisted from the server^7`)
return deferrals.done(blacklistMessage);
case 2:
let resDis = await axios({
method: 'GET',
url: `https://discord.com/api/v9/guilds/${guildId}/members/${userId}`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bot ${botToken}`
},
}).catch(async (err) => {
// if(debugMode) console.error(JSON.stringify(err))
if(debugMode) console.log(`[${version}] ^5${name} is not in the guild.^7`)
return deferrals.done(noGuildMessage);
});
if(!resDis) {
cache[userId] = {passed: 0,roles: null,timeAt: Date.now() + ms(cacheMaxTime)}
if(debugMode) console.log(`[${version}] ^5Error in Discord call. Maybe consider extending the 'cacheMaxTime' option.^7`)
return deferrals.done('There was an error checking your Discord Id. Please contact the server owner.');
}
if(!resDis.data) {
cache[userId] = {passed: 0,roles: null,timeAt: Date.now() + ms(cacheMaxTime)}
if(debugMode) console.log(`[${version}] ^5${name} is not in the guild. Cache created^7`)
return deferrals.done(noGuildMessage);
}
let hasRole = false;
let hasBlackRole = false;
for (let i = 0; i < whitelistRoles.length; i++) {
if (resDis.data.roles.includes(whitelistRoles[i])) hasRole = true;
};
for (let i = 0; i < blacklistRoles.length; i++) {
if (resDis.data.roles.includes(blacklistRoles[i])) hasBlackRole = true;
}
if(hasBlackRole) {
cache[userId] = {passed: 3,roles: resDis.data.roles,timeAt: Date.now() + ms(cacheMaxTime)}
if(debugMode) console.log(`[${version}] ^5${name} is blacklisted. Cache created^7`)
return deferrals.done(blacklistMessage);
}
if(hasRole) {
cache[userId] = {passed: 1,roles: resDis.data.roles,timeAt: Date.now() + ms(cacheMaxTime)}
if(debugMode) console.log(`[${version}] ^5${name} is whitelisted. Cache created^7`)
return deferrals.done();
} else {
let h = new Date().toLocaleTimeString('en-GB',{hour: 'numeric'});
if(enableTime && h >= hour_min && h < hour_max){
cache[userId] = {passed: 1,roles: resDis.data.roles,timeAt: Date.now() + ms(cacheMaxTime)}
if(debugMode) console.log(`[${version}] ^5${name} is not whitelisted but passed with free-access. Cache created^7`)
return deferrals.done();
} else {
cache[userId] = {passed: 0,roles: resDis.data.roles,timeAt: Date.now() + ms(cacheMaxTime)}
if(debugMode) {
console.log(`[${version}] ^5${name} is not whitelisted. Cache created^7`);
console.log(`[${version}] ^5${name}s Roles: ^7`, resDis.data.roles);
}
return deferrals.done(notWhitelistedMessage);
}
}
}
}
});
}, 0)
}, 0)
});
// API AND EXPORTS
exports('getRoles', (src) => {
if(!work) return;
return new Promise((res, rej) => {
getUserDiscord(src, function(userId) {
if(userId) {
axios(`/guilds/${guildId}/members/${userId}`).then((resDis) => {
if(!resDis.data) {
res([])
}
res(resDis.data.roles)
}).catch((err) => {
res([])
});
} else {
res([])
}
});
});
});
exports('userHasRole', (src, roles) => {
if(!work) return;
return new Promise((res, rej) => {
getUserDiscord(src, function(userId) {
if(userId) {
axios(`/guilds/${guildId}/members/${userId}`).then((resDis) => {
if(!resDis.data) {
res(false)
}
const hasRole = resDis.data.roles.some((cRole, i) => resDis.data.roles.includes(roles[i]));
if(hasRole) {
res(true)
} else {
res(false)
}
}).catch((err) => {
res(false)
});
} else {
res(false)
}
});
});
});
exports('getName', (src) => {
if(!work) return;
return new Promise((res, rej) => {
getUserDiscord(src, function(userId) {
if(userId) {
axios(`/guilds/${guildId}/members/${userId}`).then((resDis) => {
if(!resDis.data) {
res(null)
}
res({username: resDis.data.username, discriminator: resDis.data.discriminator, nickname: resDis.data.nick})
}).catch((err) => {
res(null)
});
} else {
res(null)
}
});
});
});