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

Commit 206e1aa

Browse files
committed
Stuff's broken rn tbh
1 parent 8b7c0ca commit 206e1aa

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
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: 65 additions & 5 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

@@ -22,7 +26,8 @@ bot.on('message', (msg) => {
2226
const unique = new Set(allLinks);
2327

2428
unique.forEach((item) => {
25-
mps.push(reqAPI(item.trim()).catch(console.error));
29+
var toPush = dbCheck(msg, item.trim(), false);
30+
if (toPush !== null) mps.push(toPush);
2631
});
2732
}
2833

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

3439
unique.forEach((item) => {
35-
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);
3642
});
3743
}
3844

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

4450
unique.forEach((item) => {
45-
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);
4653
});
4754
}
4855

@@ -63,6 +70,29 @@ bot.on('message', (msg) => {
6370
bot.login(config.token);
6471
}); */
6572

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+
6696
const commands = {
6797
help: (msg) => {
6898
msg.channel.sendMessage('Syntax and commands: <https://github.com/ThePsionic/RSWikiLinker#syntax>');
@@ -73,13 +103,34 @@ const commands = {
73103
.then(() => {
74104
process.exit(1);
75105
});
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+
}));
76127
}
77128
};
78129

79-
const reqAPI = requestname => new Promise((resolve, reject) => {
130+
const reqAPI = (wiki, requestname) => new Promise((resolve, reject) => {
80131
request({
81132
method: 'GET',
82-
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`,
83134
json: true
84135
}, (error, response, body) => {
85136
if (!error && response.statusCode === 200) {
@@ -97,5 +148,14 @@ if (config.admin_snowflake === '') {
97148
console.log('Admin snowflake empty. Startup disallowed.');
98149
process.exit(1);
99150
} 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+
});
100160
bot.login(config.token);
101161
}

0 commit comments

Comments
 (0)