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

Commit 41f4ef1

Browse files
committed
Several minor fixes
1 parent ec59f75 commit 41f4ef1

File tree

1 file changed

+78
-76
lines changed

1 file changed

+78
-76
lines changed

wikilinker.js

Lines changed: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ bot.once('ready', () => {
2323
});
2424
});
2525
trulyReady = true;
26-
bot.client.setUsername('WikiaLinker');
2726
console.log(`Ready: serving ${bot.guilds.size} guilds, in ${bot.channels.size} channels, for ${bot.users.size} users.`);
2827
});
2928

@@ -56,69 +55,69 @@ bot.on('message', (msg) => {
5655
]);
5756
} else if (!row.broadcastChannel) {
5857
return msg.channel.send([
59-
'This server has not set a default wiki yet.',
60-
'Users with the "Administrator" permission can do this using wl~swiki <wikiname>.'
58+
'This server has not set a broadcast channel yet.',
59+
'Users with the "Administrator" permission can do this using wl~bchan <channel mention>.'
6160
]);
6261
}
63-
});
6462

65-
sql.get(`SELECT mainWiki FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
66-
let wiki = row.mainWiki;
63+
sql.get(`SELECT mainWiki FROM guilds WHERE id="${msg.guild.id}"`).then(lowrow => {
64+
let wiki = lowrow.mainWiki;
6765

68-
sql.all(`SELECT * FROM overrides WHERE guildID="${msg.guild.id}"`).then(rows => {
69-
if (rows.length !== 0) {
70-
for (let i = 0; i < rows.length; i++) {
71-
if (rows[i].channelID === msg.channel.id) {
72-
wiki = rows[i].wiki;
66+
sql.all(`SELECT * FROM overrides WHERE guildID="${msg.guild.id}"`).then(rows => {
67+
if (rows.length !== 0) {
68+
for (let i = 0; i < rows.length; i++) {
69+
if (rows[i].channelID === msg.channel.id) {
70+
wiki = rows[i].wiki;
71+
}
7372
}
7473
}
75-
}
76-
const mps = ['**Wiki links detected:**'];
77-
const removeCodeblocks = msg.cleanContent.replace(/`{3}[\S\s]*?`{3}/gm, '');
78-
const removeInlineCode = removeCodeblocks.replace(/`[\S\s]*?`/gm, '');
79-
const cleaned = removeInlineCode.replace(/\u200B/g, '');
74+
const mps = ['**Wiki links detected:**'];
75+
const removeCodeblocks = msg.cleanContent.replace(/`{3}[\S\s]*?`{3}/gm, '');
76+
const removeInlineCode = removeCodeblocks.replace(/`[\S\s]*?`/gm, '');
77+
const cleaned = removeInlineCode.replace(/\u200B/g, '');
8078

81-
if (/\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g.test(cleaned)) {
82-
const name = cleaned.replace(/.*?\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g, '$1\u200B');
83-
const allLinks = name.split('\u200B').slice(0, -1);
84-
const unique = new Set(allLinks);
79+
if (/\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g.test(cleaned)) {
80+
const name = cleaned.replace(/.*?\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g, '$1\u200B');
81+
const allLinks = name.split('\u200B').slice(0, -1);
82+
const unique = new Set(allLinks);
8583

86-
unique.forEach((item) => {
87-
mps.push(reqAPI(wiki, item.trim()).catch(console.error));
88-
});
89-
}
84+
unique.forEach((item) => {
85+
mps.push(reqAPI(wiki, item.trim()).catch(console.error));
86+
});
87+
}
9088

91-
if (/\{\{([^}|]+)(?:|[^}]+)?\}\}/g.test(cleaned)) {
92-
const name = cleaned.replace(/.*?\{\{([^}|]+)(?:|[^}]+)?\}\}/g, '$1\u200B');
93-
const allLinks = name.split('\u200B').slice(0, -1);
94-
const unique = new Set(allLinks);
89+
if (/\{\{([^}|]+)(?:|[^}]+)?\}\}/g.test(cleaned)) {
90+
const name = cleaned.replace(/.*?\{\{([^}|]+)(?:|[^}]+)?\}\}/g, '$1\u200B');
91+
const allLinks = name.split('\u200B').slice(0, -1);
92+
const unique = new Set(allLinks);
9593

96-
unique.forEach((item) => {
97-
mps.push(reqAPI(wiki, `Template:${item.trim()}`).catch(console.error));
98-
});
99-
}
94+
unique.forEach((item) => {
95+
mps.push(reqAPI(wiki, `Template:${item.trim()}`).catch(console.error));
96+
});
97+
}
10098

101-
if (/--([^\-|]+)(?:|[^-]+)?--/g.test(cleaned)) {
102-
const name = cleaned.replace(/.*?--([^\-|]+)(?:|[^-]+)?--/g, '$1\u200B');
103-
const allLinks = name.split('\u200B').slice(0, -1);
104-
const unique = new Set(allLinks);
99+
if (/--([^\-|]+)(?:|[^-]+)?--/g.test(cleaned)) {
100+
const name = cleaned.replace(/.*?--([^\-|]+)(?:|[^-]+)?--/g, '$1\u200B');
101+
const allLinks = name.split('\u200B').slice(0, -1);
102+
const unique = new Set(allLinks);
105103

106-
unique.forEach((item) => {
107-
mps.push(`<http://${wiki}.wikia.com/wiki/${item.trim().replace(/\s/g, '_')}>`);
108-
});
109-
}
104+
unique.forEach((item) => {
105+
mps.push(`<http://${wiki}.wikia.com/wiki/${item.trim().replace(/\s/g, '_')}>`);
106+
});
107+
}
110108

111-
Promise.all(mps)
112-
.then(preparedSend => {
113-
preparedSend = preparedSend.filter(item => item !== undefined);
114-
if (preparedSend.length > 1) {
115-
console.log('Sending message...');
116-
msg.channel.send(preparedSend);
117-
}
118-
})
119-
.catch(console.error);
120-
});
121-
});
109+
Promise.all(mps)
110+
.then(preparedSend => {
111+
preparedSend = preparedSend.filter(item => item !== undefined);
112+
if (preparedSend.length > 1) {
113+
console.log('Sending message...');
114+
msg.channel.send(preparedSend);
115+
}
116+
})
117+
.catch(console.error);
118+
}).catch(console.error);
119+
}).catch(console.error);
120+
}).catch(console.error);
122121
}
123122
});
124123

@@ -143,24 +142,19 @@ const commands = {
143142
msg.reply("you don't get to yell at everyone!");
144143
return;
145144
}
146-
/* var joinedGuilds = Array.from(bot.guilds.keys());
147-
for (var i = 0; i < joinedGuilds.length; i++) {
148-
var guildID = joinedGuilds[i];
149-
if (db[guildID].broadcastChannel) {
150-
return bot.channels.get(db[guildID].broadcastChannel).send(globalMessage);
151-
}
152-
}*/
153145

154146
sql.each(`SELECT * FROM guilds`, (err, row) => {
155147
if (row.broadcastChannel && !err) {
156148
bot.channels.get(row.broadcastChannel).send(globalMessage);
157149
}
158-
});
150+
}).catch(console.error);
159151
},
160152
swiki: (msg, [wiki]) => {
161-
if (msg.author.id !== config.admin_snowflake || !msg.member.hasPermission('ADMINISTRATOR')) {
162-
msg.reply('You are not allowed to change the default wiki of this server.');
163-
return;
153+
if (msg.author.id !== config.admin_snowflake) {
154+
if (!msg.member.hasPermission('ADMINISTRATOR')) {
155+
msg.reply('You are not allowed to change the default wiki of this server.');
156+
return;
157+
}
164158
}
165159
wiki = wiki.split(' ')[0];
166160
sql.get(`SELECT * FROM guilds WHERE id=${msg.guild.id}`).then(row => {
@@ -169,40 +163,46 @@ const commands = {
169163
msg.reply(`Wiki is now set to: ${wiki}`)
170164
).catch(() => msg.reply('Database error - please contact the developer!'));
171165
} else {
172-
sql.run(`UPDATE guilds SET mainWiki="?" WHERE id="${msg.guild.id}"`, [wiki]).then(() =>
166+
sql.run(`UPDATE guilds SET mainWiki=? WHERE id=?`, [wiki, msg.guild.id]).then(() =>
173167
msg.reply(`Wiki is now set to: ${wiki}`));
174168
}
175-
});
169+
}).catch(console.error);
176170
},
177171
cwiki: (msg, [wiki]) => {
178-
if (msg.author.id !== config.admin_snowflake || !msg.member.hasPermission('ADMINISTRATOR')) {
179-
msg.reply('You are not allowed to override the wiki of this channel.'); return;
172+
if (msg.author.id !== config.admin_snowflake) {
173+
if (!msg.member.hasPermission('ADMINISTRATOR')) {
174+
msg.reply('You are not allowed to change the default wiki of this server.');
175+
return;
176+
}
180177
} else if (msg.channel.id === msg.guild.id) {
181178
msg.reply('You can\'t override the default channel of a server.'); return;
182179
}
183180
console.log(wiki);
184181
wiki = wiki.split(' ')[0];
185182
sql.get(`SELECT * FROM overrides WHERE guildID="${msg.guild.id}" AND channelID="${msg.channel.id}"`).then(row => {
186183
if (row) {
187-
sql.run(`UPDATE overrides SET wiki="?" WHERE guildID="${msg.guild.id}" AND channelID="${msg.channel.id}"`, [wiki]);
184+
sql.run(`UPDATE overrides SET wiki=? WHERE guildID=? AND channelID=?`, [wiki, msg.guild.id, msg.channel.id]);
188185
} else {
189186
sql.run(`INSERT INTO overrides (guildID, channelID, wiki) VALUES (?,?,?)`, [msg.guild.id, msg.channel.id, wiki]);
190187
}
191-
}).then(() => msg.reply(`The wiki override for channel ${msg.channel.name} is now set to ${wiki}`));
188+
}).then(() => msg.reply(`The wiki override for channel ${msg.channel.name} is now set to ${wiki}`)).catch(console.error);
192189
},
193190
bchan: (msg) => {
194-
if (!msg.mentions.channels || msg.mentions.channels.size > 1) {
191+
if (msg.author.id !== config.admin_snowflake) {
192+
if (!msg.member.hasPermission('ADMINISTRATOR')) {
193+
msg.reply('You are not allowed to change the broadcast channel of this server.');
194+
return;
195+
}
196+
} else if (!msg.mentions.channels || msg.mentions.channels.size > 1) {
195197
msg.reply('You need to mention exactly one channel to be set as broadcast channel.');
196198
return;
197199
} else {
198200
var channel = msg.mentions.channels.first();
199-
/* db[msg.guild.id].broadcastChannel = channel.id;
200-
return saveDB().then(() => {
201-
msg.reply(`The broadcast channel for this server is now set to: ${channel.name}.`);
202-
}); */
201+
console.log(`Channel is ${channel.name}`);
203202
sql.get(`SELECT * FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
203+
console.log(row);
204204
if (row) {
205-
sql.run(`UPDATE guilds SET broadcastChannel="?" WHERE id="${msg.guild.id}"`, [channel.id]).then(() =>
205+
sql.run(`UPDATE guilds SET broadcastChannel=? WHERE id=?`, [channel.id, msg.guild.id]).then(() =>
206206
msg.reply(`The broadcast channel for this server is now set to: ${channel.name}.`)
207207
);
208208
} else {
@@ -233,8 +233,8 @@ const commands = {
233233

234234
totalMessage += '\n```';
235235
msg.channel.send(totalMessage);
236-
});
237-
});
236+
}).catch(console.error);
237+
}).catch(console.error);
238238
}
239239
};
240240

@@ -261,3 +261,5 @@ if (config.admin_snowflake === '') {
261261
} else {
262262
bot.login(config.token);
263263
}
264+
265+
process.on('unhandledRejection', re => console.log(re));

0 commit comments

Comments
 (0)