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

Commit 0b4b9f2

Browse files
authored
Merge pull request #1 from ThePsionic/master
merge latest
2 parents 9327d34 + 206e1aa commit 0b4b9f2

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
node_modules/
3-
config.json
3+
config.json
4+
db.json

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"dependencies": {
55
"discord.js": "^11.0.0",
6-
"request": "2.72.0"
6+
"request": "2.72.0",
7+
"jsonfile": "2.4.0"
78
}
89
}

wikilinker.js

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const Discord = require('discord.js');
22
const request = require('request');
3+
const jsonfile = require('jsonfile');
4+
const fs = require('fs');
35
const config = require('./config.json');
46

57
const bot = new Discord.Client();
68

9+
const dbfile = './db.json';
10+
711
bot.on('message', (msg) => {
812
if (msg.author.bot) return;
913

@@ -12,15 +16,18 @@ bot.on('message', (msg) => {
1216
if (commands.hasOwnProperty(command)) commands[command](msg);
1317
} else {
1418
const mps = ['**Wiki links detected:**'];
15-
const cleaned = msg.cleanContent.replace(/\u200B/g, '');
19+
const removeCodeblocks = msg.cleanContent.replace(/`{3}[\S\s]*?`{3}/gm, '');
20+
const removeInlineCode = removeCodeblocks.replace(/`[\S\s]*?`/gm, '');
21+
const cleaned = removeInlineCode.replace(/\u200B/g, '');
1622

1723
if (/\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g.test(cleaned)) {
1824
const name = cleaned.replace(/.*?\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g, '$1\u200B');
1925
const allLinks = name.split('\u200B').slice(0, -1);
2026
const unique = new Set(allLinks);
2127

2228
unique.forEach((item) => {
23-
mps.push(reqAPI(item.trim()).catch(console.error));
29+
var toPush = dbCheck(msg, item.trim(), false);
30+
if (toPush !== null) mps.push(toPush);
2431
});
2532
}
2633

@@ -30,7 +37,8 @@ bot.on('message', (msg) => {
3037
const unique = new Set(allLinks);
3138

3239
unique.forEach((item) => {
33-
mps.push(reqAPI(`Template:${item.trim()}`).catch(console.error));
40+
var toPush = dbCheck(msg, `Template:${item.trim()}`, false);
41+
if (toPush !== null) mps.push(toPush);
3442
});
3543
}
3644

@@ -40,7 +48,8 @@ bot.on('message', (msg) => {
4048
const unique = new Set(allLinks);
4149

4250
unique.forEach((item) => {
43-
mps.push(`<http://runescape.wikia.com/wiki/${item.trim().replace(/\s/g, '_')}>`);
51+
var toPush = dbCheck(msg, item.trim().replace(/\s/g, '_'), true);
52+
if (toPush !== null) mps.push(toPush);
4453
});
4554
}
4655

@@ -61,6 +70,29 @@ bot.on('message', (msg) => {
6170
bot.login(config.token);
6271
}); */
6372

73+
const dbCheck = (msg, requestname, raw) => {
74+
jsonfile.readFile(dbfile, (err, obj) => {
75+
if (err) {
76+
console.error(err);
77+
return null;
78+
}
79+
if (!msg.guild) {
80+
msg.reply("please don't PM me :(");
81+
return null;
82+
}
83+
if (obj[msg.guild.id] === null || obj[msg.guild.id] === undefined) {
84+
msg.channel.sendMessage('This server has not set a default wiki yet.\nUsers with the "Administrator" permission can do this using %setWiki <wikiname>.');
85+
return null;
86+
}
87+
const wiki = obj[msg.guild.id];
88+
if (raw) {
89+
return `<http://${wiki}.wikia.com/wiki/${requestname.trim().replace(/\s/g, '_')}>`;
90+
} else {
91+
return reqAPI(wiki, requestname);
92+
}
93+
});
94+
};
95+
6496
const commands = {
6597
help: (msg) => {
6698
msg.channel.sendMessage('Syntax and commands: <https://github.com/ThePsionic/RSWikiLinker#syntax>');
@@ -71,13 +103,34 @@ const commands = {
71103
.then(() => {
72104
process.exit(1);
73105
});
106+
},
107+
setWiki: (msg) => {
108+
if (msg.author.id !== config.admin_snowflake || !msg.member.hasPermission('ADMINISTRATOR')) {
109+
return msg.reply('you are not allowed to change the default wiki of this server.');
110+
}
111+
if (!msg.guild) {
112+
return msg.reply('you can\'t set a default wiki privately as of right now.');
113+
}
114+
return msg.reply(jsonfile.readFile(dbfile, (err, obj) => {
115+
if (err) {
116+
console.error(err);
117+
return `error while setting wiki - sorry!`;
118+
}
119+
var wiki = msg.cleanContent.split(/\s/g)[1];
120+
console.log(`wiki = ${wiki}`);
121+
obj[msg.guild.id] = wiki;
122+
jsonfile.writeFile(dbfile, obj, (innererr) => {
123+
console.error(innererr);
124+
});
125+
return `you have set the default wiki of this server to ${wiki}.`;
126+
}));
74127
}
75128
};
76129

77-
const reqAPI = requestname => new Promise((resolve, reject) => {
130+
const reqAPI = (wiki, requestname) => new Promise((resolve, reject) => {
78131
request({
79132
method: 'GET',
80-
uri: `http://runescape.wikia.com/api/v1/Search/List/?query=${requestname}&limit=1&namespaces=0%2C14`,
133+
uri: `http://${wiki}.wikia.com/api/v1/Search/List/?query=${requestname}&limit=1&namespaces=0%2C14`,
81134
json: true
82135
}, (error, response, body) => {
83136
if (!error && response.statusCode === 200) {
@@ -95,5 +148,14 @@ if (config.admin_snowflake === '') {
95148
console.log('Admin snowflake empty. Startup disallowed.');
96149
process.exit(1);
97150
} else {
151+
fs.stat(dbfile, (err) => {
152+
if (err === null) {
153+
console.log('File exists');
154+
} else if (err.code === 'ENOENT') {
155+
fs.writeFile(dbfile, '{}');
156+
} else {
157+
console.error('Bad file error: ', err.code);
158+
}
159+
});
98160
bot.login(config.token);
99161
}

0 commit comments

Comments
 (0)