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

Commit b1e022c

Browse files
committed
Experimental rawlink regex and better default channel attempts
Also exchanged the ESLint JSON for YML because nicer
1 parent 115d261 commit b1e022c

File tree

3 files changed

+84
-162
lines changed

3 files changed

+84
-162
lines changed

.eslintrc.json

Lines changed: 0 additions & 144 deletions
This file was deleted.

.eslintrc.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
parserOptions:
3+
ecmaVersion: 6
4+
env:
5+
browser: true
6+
jquery: true
7+
extends: eslint:recommended
8+
rules:
9+
dot-notation: [error, allowPattern: "^[A-Z]"]
10+
id-length: [error, exceptions: [i, j, e, x, y]]
11+
no-caller: error
12+
no-console: off
13+
no-empty: off
14+
no-implied-eval: error
15+
no-mixed-spaces-and-tabs: [error, smart-tabs]
16+
no-unused-vars:
17+
- error
18+
- vars: local
19+
args: none
20+
no-shadow: error
21+
no-unsafe-negation: error
22+
no-with: error
23+
valid-jsdoc:
24+
- error
25+
- requireReturn: false
26+
requireParamDescription: false
27+
requireReturnDescription: false
28+
29+
indent: [error, tab]
30+
quotes: [error, single, avoidEscape: true]
31+
semi: error
32+
brace-style: [error, 1tbs, allowSingleLine: true]
33+
linebreak-style: [error, unix]
34+
semi-spacing: error
35+
comma-spacing: error
36+
space-infix-ops: error
37+
template-curly-spacing: error
38+
space-before-function-paren: [error, never]
39+
space-before-blocks: error
40+
keyword-spacing:
41+
- error
42+
- before: true
43+
after: true
44+
globals:
45+
require: true
46+
Long: true
47+
exports: true
48+
Set: true
49+
Promise: true
50+
process: true

wikilinker.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bot.on('message', (msg) => {
4545
const args = msg.content.slice(config.prefix.length).split(/ (.+)/);
4646
const command = args.shift();
4747
if (commands.hasOwnProperty(command)) commands[command](msg, args);
48-
} else if (/\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g.test(msg.cleanContent) || /\{\{([^}|]+)(?:|[^}]+)?\}\}/g.test(msg.cleanContent) || /--([^\-|]+)(?:|[^-]+)?--/g.test(msg.cleanContent)) {
48+
} else if (/\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g.test(msg.cleanContent) || /\{\{([^}|]+)(?:|[^}]+)?\}\}/g.test(msg.cleanContent) || /--([^|]+?)--/g.test(msg.cleanContent)) {
4949
// eslint-disable-next-line consistent-return
5050
sql.get(`SELECT * FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
5151
if (!row.mainWiki) {
@@ -91,8 +91,8 @@ bot.on('message', (msg) => {
9191
});
9292
}
9393

94-
if (/--([^\-|]+)(?:|[^-]+)?--/g.test(cleaned)) {
95-
const name = cleaned.replace(/.*?--([^\-|]+)(?:|[^-]+)?--/g, '$1\u200B').replace(/.*(?:\n|\r)/g, '');
94+
if (/--([^|]+?)--/g.test(cleaned)) {
95+
const name = cleaned.replace(/.*?--([^|]+?)--/g, '$1\u200B').replace(/.*(?:\n|\r)/g, '');
9696
const allLinks = name.split('\u200B').slice(0, -1);
9797
const unique = new Set(allLinks);
9898

@@ -102,14 +102,14 @@ bot.on('message', (msg) => {
102102
}
103103

104104
Promise.all(mps)
105-
.then(preparedSend => {
106-
preparedSend = preparedSend.filter(item => item !== undefined);
107-
if (preparedSend.length > 1) {
108-
console.log('Sending message...');
109-
msg.channel.send(preparedSend);
110-
}
111-
})
112-
.catch(console.error);
105+
.then(preparedSend => {
106+
preparedSend = preparedSend.filter(item => item !== undefined);
107+
if (preparedSend.length > 1) {
108+
console.log('Sending message...');
109+
msg.channel.send(preparedSend);
110+
}
111+
})
112+
.catch(console.error);
113113
}).catch(console.error);
114114
}).catch(console.error);
115115
}).catch(console.error);
@@ -138,13 +138,15 @@ const commands = {
138138
return;
139139
}
140140

141-
sql.each(`SELECT * FROM guilds`, (err, row) => {
141+
sql.each('SELECT * FROM guilds', (err, row) => {
142142
if (row.broadcastChannel && !err) {
143143
if (row.broadcastChannel !== '-1') {
144144
bot.channels.get(row.broadcastChannel).send(globalMessage);
145145
}
146-
} else if (bot.guilds.get(row.id)) {
147-
bot.guilds.get(row.id).defaultChannel.send(globalMessage);
146+
} else if (bot.guilds.has(row.id)) {
147+
defaultChannel(bot.guilds.get(row.id)).then(channel => {
148+
channel.send(globalMessage);
149+
});
148150
}
149151
}).catch(console.error);
150152
},
@@ -162,7 +164,7 @@ const commands = {
162164
msg.reply(`Wiki is now set to: ${wiki}`)
163165
).catch(() => msg.reply('Database error - please contact the developer!'));
164166
} else {
165-
sql.run(`UPDATE guilds SET mainWiki=? WHERE id=?`, [wiki, msg.guild.id]).then(() =>
167+
sql.run('UPDATE guilds SET mainWiki=? WHERE id=?', [wiki, msg.guild.id]).then(() =>
166168
msg.reply(`Wiki is now set to: ${wiki}`));
167169
}
168170
}).catch(console.error);
@@ -180,9 +182,9 @@ const commands = {
180182
wiki = wiki.split(' ')[0];
181183
sql.get(`SELECT * FROM overrides WHERE guildID="${msg.guild.id}" AND channelID="${msg.channel.id}"`).then(row => {
182184
if (row) {
183-
sql.run(`UPDATE overrides SET wiki=? WHERE guildID=? AND channelID=?`, [wiki, msg.guild.id, msg.channel.id]);
185+
sql.run('UPDATE overrides SET wiki=? WHERE guildID=? AND channelID=?', [wiki, msg.guild.id, msg.channel.id]);
184186
} else {
185-
sql.run(`INSERT INTO overrides (guildID, channelID, wiki) VALUES (?,?,?)`, [msg.guild.id, msg.channel.id, wiki]);
187+
sql.run('INSERT INTO overrides (guildID, channelID, wiki) VALUES (?,?,?)', [msg.guild.id, msg.channel.id, wiki]);
186188
}
187189
}).then(() => msg.reply(`The wiki override for channel ${msg.channel.name} is now set to ${wiki}`)).catch(console.error);
188190
},
@@ -209,7 +211,7 @@ const commands = {
209211
sql.get(`SELECT * FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
210212
console.log(row);
211213
if (row) {
212-
sql.run(`UPDATE guilds SET broadcastChannel=? WHERE id=?`, [channel.id, msg.guild.id]).then(() =>
214+
sql.run('UPDATE guilds SET broadcastChannel=? WHERE id=?', [channel.id, msg.guild.id]).then(() =>
213215
msg.reply(`The broadcast channel for this server is now set to: ${channel.name}.`)
214216
);
215217
} else {
@@ -262,6 +264,20 @@ const reqAPI = (wiki, requestname) => new Promise((resolve, reject) => {
262264
});
263265
});
264266

267+
const defaultChannel = (guild) => new Promise((resolve, reject) => {
268+
guild.channels.forEach((value, key, map) => {
269+
if (value.name === 'general') {
270+
return resolve(value);
271+
}
272+
});
273+
let alt = guild.channels.filter((channel) => channel.type === 'text' && channel.permissionsFor(bot.user).has('SEND_MESSAGES')).first();
274+
if (alt) {
275+
return resolve(alt);
276+
} else {
277+
return reject('No applicable channel found.');
278+
}
279+
});
280+
265281
if (config.admin_snowflake === '') {
266282
console.log('Admin snowflake empty. Startup disallowed.');
267283
process.exit(1);

0 commit comments

Comments
 (0)