Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit 226257a

Browse files
committed
Cache whitelist into variable
for better performance
1 parent 0f54b5c commit 226257a

File tree

2 files changed

+74
-149
lines changed

2 files changed

+74
-149
lines changed

bot.js

+73-149
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const fs = require('fs');
99

1010
const bot = new Discord.Client();
1111

12+
let whitelist = fs.readFileSync('HDWhitelist.txt','utf8')
1213
let usageData = JSON.parse(fs.readFileSync('usage.json','utf8'))
1314
let ipfsid;
1415

@@ -38,28 +39,16 @@ bot.on('message', (message) => {
3839
message.channel.send(Config.PING_REPLY);
3940
} else if (message.content.startsWith(Config.commandPrefix + 'ipfs ')) {
4041
// Source video
41-
if (Config.sdOnlyMode == true) {
42-
sendMessage(message,Config.ERROR_SD_ONLY_MODE);
43-
return;
44-
}
42+
if (Config.sdOnlyMode == true)
43+
return sendMessage(message,Config.ERROR_SD_ONLY_MODE)
4544

46-
if (Config.hdwhitelistEnabled == true) {
47-
if (fs.existsSync('HDWhitelist.txt')) {
48-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
49-
if (readList.includes(message.author.id) != true) {
50-
// Do not proceed if user is not in whitelist
51-
sendMessage(message,Config.ERROR_NO_PIN_PERMISSION);
52-
return;
53-
}
54-
} else {
55-
sendMessage(message,Config.WHITELIST_FILE404);
56-
return;
57-
}
58-
}
45+
if (Config.hdwhitelistEnabled && !whitelist.includes(message.author.id))
46+
// Do not proceed if user is not in whitelist
47+
return sendMessage(message,Config.ERROR_NO_PIN_PERMISSION)
5948

60-
var command = message.content;
61-
var steemitAuthorPermlink = command.split('/').slice(-2);
62-
var author = steemitAuthorPermlink[0];
49+
let command = message.content
50+
let steemitAuthorPermlink = command.split('/').slice(-2)
51+
let author = steemitAuthorPermlink[0]
6352

6453
if (author.startsWith('@')) {
6554
// Remove @ symbol if it is a steemit/busy link
@@ -116,19 +105,9 @@ bot.on('message', (message) => {
116105
})
117106
});
118107
} else if (message.content.startsWith(Config.commandPrefix + 'ipfs240 ')) {
119-
if (Config.restrictedMode == true) {
120-
if (fs.existsSync('HDWhitelist.txt')) {
121-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
122-
if (readList.includes(message.author.id) != true) {
123-
// Do not proceed if user is not in whitelist
124-
sendMessage(message,Config.ERROR_NO_PIN_PERMISSION_RESTRICTED);
125-
return;
126-
}
127-
} else {
128-
sendMessage(message,Config.WHITELIST_FILE404);
129-
return;
130-
}
131-
}
108+
if (Config.hdwhitelistEnabled && !whitelist.includes(message.author.id))
109+
// Do not proceed if user is not in whitelist
110+
return sendMessage(message,Config.ERROR_NO_PIN_PERMISSION)
132111

133112
// 240p video
134113
var command = message.content;
@@ -188,24 +167,14 @@ bot.on('message', (message) => {
188167
})
189168
});
190169
} else if (message.content.startsWith(Config.commandPrefix + 'ipfs480 ')) {
191-
if (Config.restrictedMode == true) {
192-
if (fs.existsSync('HDWhitelist.txt')) {
193-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
194-
if (readList.includes(message.author.id) != true) {
195-
// Do not proceed if user is not in whitelist
196-
sendMessage(message,Config.ERROR_NO_PIN_PERMISSION_RESTRICTED);
197-
return;
198-
}
199-
} else {
200-
sendMessage(message,Config.WHITELIST_FILE404);
201-
return;
202-
}
203-
}
170+
if (Config.hdwhitelistEnabled && !whitelist.includes(message.author.id))
171+
// Do not proceed if user is not in whitelist
172+
return sendMessage(message,Config.ERROR_NO_PIN_PERMISSION)
204173

205174
// 480p video
206-
var command = message.content;
207-
var steemitAuthorPermlink = command.split('/').slice(-2);
208-
var author = steemitAuthorPermlink[0];
175+
let command = message.content
176+
let steemitAuthorPermlink = command.split('/').slice(-2)
177+
let author = steemitAuthorPermlink[0]
209178

210179
if (author.startsWith('@')) {
211180
// Remove @ symbol if it is a steemit/busy link
@@ -261,28 +230,16 @@ bot.on('message', (message) => {
261230
});
262231
} else if (message.content.startsWith(Config.commandPrefix + 'ipfs720 ')) {
263232
// 720p video
264-
if (Config.sdOnlyMode == true) {
265-
sendMessage(message,Config.ERROR_SD_ONLY_MODE);
266-
return;
267-
}
233+
if (Config.sdOnlyMode == true)
234+
return sendMessage(message,Config.ERROR_SD_ONLY_MODE)
268235

269-
if (Config.hdwhitelistEnabled == true) {
270-
if (fs.existsSync('HDWhitelist.txt')) {
271-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
272-
if (readList.includes(message.author.id) != true) {
273-
// Do not proceed if user is not in whitelist
274-
sendMessage(message,Config.ERROR_NO_PIN_PERMISSION);
275-
return;
276-
}
277-
} else {
278-
sendMessage(message,Config.WHITELIST_FILE404);
279-
return;
280-
}
281-
}
236+
if (Config.hdwhitelistEnabled && !whitelist.includes(message.author.id))
237+
// Do not proceed if user is not in whitelist
238+
return sendMessage(message,Config.ERROR_NO_PIN_PERMISSION)
282239

283-
var command = message.content;
284-
var steemitAuthorPermlink = command.split('/').slice(-2);
285-
var author = steemitAuthorPermlink[0];
240+
let command = message.content
241+
let steemitAuthorPermlink = command.split('/').slice(-2)
242+
let author = steemitAuthorPermlink[0]
286243

287244
if (author.startsWith('@')) {
288245
// Remove @ symbol if it is a steemit/busy link
@@ -338,30 +295,16 @@ bot.on('message', (message) => {
338295
});
339296
} else if (message.content.startsWith(Config.commandPrefix + 'ipfs1080 ')) {
340297
// 1080p video
341-
if (Config.sdOnlyMode == true) {
342-
sendMessage(message,ERROR_SD_ONLY_MODE);
343-
return;
344-
}
345-
346-
if (Config.hdwhitelistEnabled == true) {
347-
if (fs.existsSync('HDWhitelist.txt')) {
348-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
349-
if (readList.includes(message.author.id) != true) {
350-
// Do not proceed if user is not in whitelist
351-
if (Config.restrictedMode == true) {
352-
sendMessage(message,Config.ERROR_NO_PIN_PERMISSION);
353-
return;
354-
}
355-
}
356-
} else {
357-
sendMessage(message,Config.WHITELIST_FILE404);
358-
return;
359-
}
360-
}
298+
if (Config.sdOnlyMode == true)
299+
return sendMessage(message,Config.ERROR_SD_ONLY_MODE)
361300

362-
var command = message.content;
363-
var steemitAuthorPermlink = command.split('/').slice(-2);
364-
var author = steemitAuthorPermlink[0];
301+
if (Config.hdwhitelistEnabled && !whitelist.includes(message.author.id))
302+
// Do not proceed if user is not in whitelist
303+
return sendMessage(message,Config.ERROR_NO_PIN_PERMISSION)
304+
305+
let command = message.content
306+
let steemitAuthorPermlink = command.split('/').slice(-2)
307+
let author = steemitAuthorPermlink[0]
365308

366309
if (author.startsWith('@')) {
367310
// Remove @ symbol if it is a steemit/busy link
@@ -417,25 +360,26 @@ bot.on('message', (message) => {
417360
});
418361
} else if (message.content.startsWith(Config.commandPrefix + 'ipfssound ')) {
419362
// DSound audio
420-
var command = message.content;
421-
var steemitAuthorPermlink = command.split('/').slice(-2);
422-
var author = steemitAuthorPermlink[0];
363+
if (Config.hdwhitelistEnabled && !whitelist.includes(message.author.id))
364+
// Do not proceed if user is not in whitelist
365+
return sendMessage(message,Config.ERROR_NO_PIN_PERMISSION_AUDIO)
366+
367+
let command = message.content
368+
let steemitAuthorPermlink = command.split('/').slice(-2)
369+
let author = steemitAuthorPermlink[0]
423370

424371
if (author.startsWith('@')) {
425372
// Remove @ symbol if it is a steemit/busy link
426373
author = steemitAuthorPermlink[0].slice(1,steemitAuthorPermlink[0].length);
427374
}
428375

429376
Steem.api.getContent(author,steemitAuthorPermlink[1],function(err,result) {
430-
if (err != null) {
431-
sendMessage(message,Config.ERROR_STEEM_GETCONTENT + err);
432-
return;
433-
}
377+
if (err != null)
378+
return sendMessage(message,Config.ERROR_STEEM_GETCONTENT + err)
434379

435380
// Get JSON metadata of post
436-
var jsonmeta = JSON.parse(result.json_metadata);
437-
438-
var dsoundhash = jsonmeta.audio.files.sound;
381+
let jsonmeta = JSON.parse(result.json_metadata)
382+
let dsoundhash = jsonmeta.audio.files.sound
439383

440384
IPFS.pin.ls(dsoundhash,{ type: 'recursive' },(err,pinset) => {
441385
if (err == null && pinset[0].hash === dsoundhash) {
@@ -546,33 +490,22 @@ bot.on('message', (message) => {
546490
}
547491
} else if (message.content == (Config.commandPrefix + 'hdwhitelist check')) {
548492
// Check if user is in whitelist
549-
if (fs.existsSync('HDWhitelist.txt')) {
550-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
551-
if (readList.includes(message.author.id) == true) {
552-
replyMessage(message,Config.WHITELIST_TRUE);
553-
} else {
554-
replyMessage(message,Config.WHITELIST_FALSE);
555-
}
493+
if (whitelist.includes(message.author.id)) {
494+
replyMessage(message,Config.WHITELIST_TRUE);
556495
} else {
557-
message.channel.send(Config.WHITELIST_FILE404);
496+
replyMessage(message,Config.WHITELIST_FALSE);
558497
}
559498
} else if (message.content.startsWith(Config.commandPrefix + 'hdwhitelist add ')) {
560499
// Add user to whitelist
561500
if (message.member.hasPermission('ADMINISTRATOR') == true) {
562501
let uidToWhitelist = message.mentions.members.first().user.id;
563502

564-
if (fs.existsSync('HDWhitelist.txt')) {
565-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
566-
if (readList.includes(uidToWhitelist) == true) {
567-
sendMessage(message,Config.WHITELIST_ALREADY_IN);
568-
return;
569-
}
570-
fs.writeFileSync('HDWhitelist.txt', readList + uidToWhitelist + '\n');
571-
} else {
572-
fs.writeFileSync('HDWhitelist.txt', uidToWhitelist + '\n');
573-
}
503+
if (whitelist.includes(uidToWhitelist))
504+
return sendMessage(message,Config.WHITELIST_ALREADY_IN)
574505

575-
sendMessage(message,'<@' + uidToWhitelist + '> ' + Config.WHITELIST_ADD_SUCCESS);
506+
whitelist = whitelist + uidToWhitelist + '\n'
507+
fs.writeFileSync('HDWhitelist.txt',whitelist)
508+
sendMessage(message,'<@' + uidToWhitelist + '> ' + Config.WHITELIST_ADD_SUCCESS)
576509
} else {
577510
sendMessage(message,Config.ERROR_NO_PERMISSION);
578511
}
@@ -581,42 +514,33 @@ bot.on('message', (message) => {
581514
if (message.member.hasPermission('ADMINISTRATOR') == true) {
582515
let uidToRemove = message.mentions.members.first().user.id;
583516

584-
if (fs.existsSync('HDWhitelist.txt')) {
585-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
586-
if (readList.includes(uidToRemove) == true) {
587-
var newList = readList.replace(uidToRemove + '\n', '');
588-
fs.writeFileSync('HDWhitelist.txt', newList);
589-
sendMessage(message,'<@' + uidToRemove + '> ' + Config.WHITELIST_RM_SUCCESS);
590-
} else {
591-
sendMessage(message,Config.WHITELIST_RM_UID404);
592-
}
517+
if (whitelist.includes(uidToRemove)) {
518+
let newList = whitelist.replace(uidToRemove + '\n', '')
519+
whitelist = newList
520+
fs.writeFileSync('HDWhitelist.txt',whitelist)
521+
sendMessage(message,'<@' + uidToRemove + '> ' + Config.WHITELIST_RM_SUCCESS)
593522
} else {
594-
sendMessage(message,Config.WHITELIST_FILE404);
523+
sendMessage(message,Config.WHITELIST_RM_UID404)
595524
}
596525
} else {
597526
sendMessage(message,Config.ERROR_NO_PERMISSION);
598527
}
599528
} else if (message.content == (Config.commandPrefix + 'hdwhitelist ls')) {
600529
// List all users in whitelist in DM
601530
if (message.member.hasPermission('ADMINISTRATOR') == true && Config.silentModeEnabled != true) {
602-
if (fs.existsSync('HDWhitelist.txt')) {
603-
var readList = fs.readFileSync('HDWhitelist.txt', 'utf8');
604-
var uidListArray = readList.split('\n');
605-
606-
for (var i = 0; i < uidListArray.length; i++) {
607-
uidListArray[i] = '<@' + uidListArray[i] + '>';
608-
}
609-
610-
var uidList = uidListArray.toString();
611-
var finalListToDM = uidList.replace(/,/g, '\n');
612-
finalListToDM = finalListToDM.slice(0,-4);
613-
finalListToDM = 'Whitelisted users: \n' + finalListToDM;
614-
if (Config.silentModeEnabled != true) {
615-
message.react('📬')
616-
message.member.send(finalListToDM);
617-
}
618-
} else {
619-
sendMessage(message,Config.WHITELIST_FILE404);
531+
let uidListArray = whitelist.split('\n')
532+
533+
for (var i = 0; i < uidListArray.length; i++) {
534+
uidListArray[i] = '<@' + uidListArray[i] + '>'
535+
}
536+
537+
let uidList = uidListArray.toString()
538+
let finalListToDM = uidList.replace(/,/g, '\n')
539+
finalListToDM = finalListToDM.slice(0,-4)
540+
finalListToDM = 'Whitelisted users: \n' + finalListToDM
541+
if (Config.silentModeEnabled != true) {
542+
message.react('📬')
543+
message.member.send(finalListToDM)
620544
}
621545
} else {
622546
sendMessage(message,Config.ERROR_NO_PERMISSION);

config.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"ERROR_SD_ONLY_MODE": "720p, 1080p and source IPFS pinning commands have been disabled.",
3434
"ERROR_NO_PERMISSION": "Sorry, you do not have the required permission to execute this command.",
3535
"ERROR_NO_PIN_PERMISSION": "Sorry, you're not in the whitelist, therefore you can't pin 720p, 1080p or source videos!",
36+
"ERROR_NO_PIN_PERMISSION_AUDIO": "Sorry, you're not in the whitelist, therefore you can't pin audio files!",
3637
"ERROR_NO_PIN_PERMISSION_RESTRICTED": "Sorry, you're not in the whitelist, therefore you can't run any pinning commands!",
3738
"BOT_SOURCE": "__***View the source code of the bot on GitHub:***__\nhttps://github.com/techcoderx/DTube-IPFS-Bot",
3839
"HASH_NOT_FOUND_ON_STEEM_BLOCKCHAIN": "IPFS hash for specified resolution for this video is not found on the Steem blockchain.",

0 commit comments

Comments
 (0)