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

Commit 42ff34a

Browse files
committed
Rework admin check
Introduced by #27, fixes #29
1 parent 60a935e commit 42ff34a

File tree

1 file changed

+107
-105
lines changed

1 file changed

+107
-105
lines changed

wikilinker.js

Lines changed: 107 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ bot.on('message', (msg) => {
4646
const command = args.shift();
4747
if (commands.hasOwnProperty(command)) {
4848
commands[command](msg, args);
49-
}
49+
}
5050
} else if (/\[\[([^\]|]+)(?:|[^\]]+)?\]\]/g.test(msg.cleanContent) || /\{\{([^}|]+)(?:|[^}]+)?\}\}/g.test(msg.cleanContent) || /--([^|]+?)--/g.test(msg.cleanContent)) {
5151
// eslint-disable-next-line consistent-return
5252
sql.get(`SELECT * FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
@@ -123,145 +123,147 @@ bot.on('message', (msg) => {
123123
bot.login(config.token);
124124
}); */
125125

126-
const sentByAdmin = (msg) => {
127-
return msg.author.id === config.admin_snowflake;
126+
const sentByBotAdmin = (msg) => {
127+
return msg.author.id === config.admin_snowflake;
128+
};
129+
130+
const sentByServerAdmin = (msg) => {
131+
return msg.member.hasPermission(Discord.Permissions.FLAGS.MANAGE_GUILD);
132+
};
133+
134+
const sentByAnyAdmin = (msg) => {
135+
return sentByBotAdmin(msg) || sentByServerAdmin(msg);
128136
};
129137

130138
const commands = {
131139
help: (msg) => {
132140
msg.channel.send('Syntax and commands: <http://thepsionic.com/WikiaLinker/>');
133141
},
134142
restart: (msg) => {
135-
if (!sentByAdmin(msg)) {
136-
msg.channel.send("Sorry, Dave. I can't let you do that.");
137-
} else {
138-
msg.channel.send('**Bot restarting!**')
139-
.then(() => {
140-
process.exit(1);
141-
});
142-
}
143+
if (!sentByBotAdmin(msg)) {
144+
msg.channel.send("Sorry, Dave. I can't let you do that.");
145+
} else {
146+
msg.channel.send('**Bot restarting!**')
147+
.then(() => {
148+
process.exit(1);
149+
});
150+
}
143151
},
144152
bc: (msg, [globalMessage]) => {
145-
if (!sentByAdmin(msg)) {
153+
if (!sentByBotAdmin(msg)) {
146154
msg.reply("you don't get to yell at everyone!");
147155
} else {
148-
sql.each('SELECT * FROM guilds', (err, row) => {
149-
if (row.broadcastChannel && !err) {
150-
if (row.broadcastChannel !== '-1') {
151-
bot.channels.get(row.broadcastChannel).send(globalMessage);
152-
}
153-
} else if (bot.guilds.has(row.id)) {
154-
defaultChannel(bot.guilds.get(row.id)).then(channel => {
155-
channel.send(globalMessage);
156-
});
157-
}
158-
}).catch(console.error);
159-
}
156+
sql.each('SELECT * FROM guilds', (err, row) => {
157+
if (row.broadcastChannel && !err) {
158+
if (row.broadcastChannel !== '-1') {
159+
bot.channels.get(row.broadcastChannel).send(globalMessage);
160+
}
161+
} else if (bot.guilds.has(row.id)) {
162+
defaultChannel(bot.guilds.get(row.id)).then(channel => {
163+
channel.send(globalMessage);
164+
});
165+
}
166+
}).catch(console.error);
167+
}
160168
},
161169
swiki: (msg, [wiki]) => {
162-
if (!sentByAdmin(msg)) {
163-
if (!msg.member.hasPermission('ADMINISTRATOR')) {
164-
msg.reply('You are not allowed to change the default wiki of this server.');
165-
}
170+
if (!sentByAnyAdmin(msg)) {
171+
msg.reply('You are not allowed to change the default wiki of this server.');
166172
} else {
167-
wiki = wiki.split(' ')[0];
168-
sql.get(`SELECT * FROM guilds WHERE id=${msg.guild.id}`).then(row => {
169-
if (!row) {
170-
sql.run('INSERT INTO guilds (mainWiki) VALUES (?)', [wiki]).then(() =>
171-
msg.reply(`Wiki is now set to: ${wiki}`)
172-
).catch(() => msg.reply('Database error - please contact the developer!'));
173-
} else {
174-
sql.run('UPDATE guilds SET mainWiki=? WHERE id=?', [wiki, msg.guild.id]).then(() =>
175-
msg.reply(`Wiki is now set to: ${wiki}`));
176-
}
177-
}).catch(console.error);
178-
}
173+
wiki = wiki.split(' ')[0];
174+
sql.get(`SELECT * FROM guilds WHERE id=${msg.guild.id}`).then(row => {
175+
if (!row) {
176+
sql.run('INSERT INTO guilds (mainWiki) VALUES (?)', [wiki]).then(() =>
177+
msg.reply(`Wiki is now set to: ${wiki}`)
178+
).catch(() => msg.reply('Database error - please contact the developer!'));
179+
} else {
180+
sql.run('UPDATE guilds SET mainWiki=? WHERE id=?', [wiki, msg.guild.id]).then(() =>
181+
msg.reply(`Wiki is now set to: ${wiki}`));
182+
}
183+
}).catch(console.error);
184+
}
179185
},
180186
cwiki: (msg, [wiki]) => {
181-
if (!sentByAdmin(msg)) {
182-
if (!msg.member.hasPermission('ADMINISTRATOR')) {
183-
msg.reply('You are not allowed to change the default wiki of this server.');
184-
}
187+
if (!sentByAnyAdmin(msg)) {
188+
msg.reply('You are not allowed to change the default wiki of this server.');
185189
} else if (msg.channel.id === msg.guild.id) {
186190
msg.reply('You can\'t override the default channel of a server.');
187191
} else {
188-
console.log(wiki);
189-
wiki = wiki.split(' ')[0];
190-
sql.get(`SELECT * FROM overrides WHERE guildID="${msg.guild.id}" AND channelID="${msg.channel.id}"`).then(row => {
191-
if (row) {
192-
sql.run('UPDATE overrides SET wiki=? WHERE guildID=? AND channelID=?', [wiki, msg.guild.id, msg.channel.id]);
193-
} else {
194-
sql.run('INSERT INTO overrides (guildID, channelID, wiki) VALUES (?,?,?)', [msg.guild.id, msg.channel.id, wiki]);
195-
}
196-
}).then(() => msg.reply(`The wiki override for channel ${msg.channel.name} is now set to ${wiki}`)).catch(console.error);
197-
}
192+
console.log(wiki);
193+
wiki = wiki.split(' ')[0];
194+
sql.get(`SELECT * FROM overrides WHERE guildID="${msg.guild.id}" AND channelID="${msg.channel.id}"`).then(row => {
195+
if (row) {
196+
sql.run('UPDATE overrides SET wiki=? WHERE guildID=? AND channelID=?', [wiki, msg.guild.id, msg.channel.id]);
197+
} else {
198+
sql.run('INSERT INTO overrides (guildID, channelID, wiki) VALUES (?,?,?)', [msg.guild.id, msg.channel.id, wiki]);
199+
}
200+
}).then(() => msg.reply(`The wiki override for channel ${msg.channel.name} is now set to ${wiki}`)).catch(console.error);
201+
}
198202
},
199203
bchan: (msg) => {
200204
let channel;
201-
if (!sentByAdmin(msg)) {
202-
if (!msg.member.hasPermission('ADMINISTRATOR')) {
203-
msg.reply('You are not allowed to change the broadcast channel of this server.');
204-
}
205+
if (!sentByAnyAdmin(msg)) {
206+
msg.reply('You are not allowed to change the broadcast channel of this server.');
205207
} else if (msg.mentions.channels.size > 1) {
206208
msg.reply('You need to mention exactly one channel to be set as broadcast channel.');
207209
} else {
208210

209-
if (msg.cleanContent.split(' ')[1] === 'off') {
210-
channel = {name: 'off', id: '-1'};
211-
} else if (msg.mentions.channels.size === 0) {
212-
channel = msg.channel;
213-
} else {
214-
channel = msg.mentions.channels.first();
215-
}
216-
console.log(`Channel is ${channel.name}`);
217-
sql.get(`SELECT * FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
218-
console.log(row);
219-
if (row) {
220-
sql.run('UPDATE guilds SET broadcastChannel=? WHERE id=?', [channel.id, msg.guild.id]).then(() =>
221-
msg.reply(`The broadcast channel for this server is now set to: ${channel.name}.`)
222-
);
223-
} else {
224-
msg.reply('Database error - please contact the developer!');
225-
}
226-
});
227-
}
211+
if (msg.cleanContent.split(' ')[1] === 'off') {
212+
channel = {name: 'off', id: '-1'};
213+
} else if (msg.mentions.channels.size === 0) {
214+
channel = msg.channel;
215+
} else {
216+
channel = msg.mentions.channels.first();
217+
}
218+
console.log(`Channel is ${channel.name}`);
219+
sql.get(`SELECT * FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
220+
console.log(row);
221+
if (row) {
222+
sql.run('UPDATE guilds SET broadcastChannel=? WHERE id=?', [channel.id, msg.guild.id]).then(() =>
223+
msg.reply(`The broadcast channel for this server is now set to: ${channel.name}.`)
224+
);
225+
} else {
226+
msg.reply('Database error - please contact the developer!');
227+
}
228+
});
229+
}
228230
},
229231
sinfo: (msg) => {
230232
if (!msg.guild) {
231233
// do nothing
232234

233235
} else {
234-
sql.get(`SELECT * FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
235-
let totalMessage = `\`\`\`\nInfo for server: ${msg.guild.name}`;
236-
if (!row.broadcastChannel) {
237-
totalMessage += '\nNo broadcast channel set';
238-
} else if (row.broadcastChannel === '-1') {
239-
totalMessage += '\nBroadcasting turned off for this server';
240-
} else {
241-
totalMessage += `\nBroadcast channel: ${msg.guild.channels.get(row.broadcastChannel).name}`;
242-
}
236+
sql.get(`SELECT * FROM guilds WHERE id="${msg.guild.id}"`).then(row => {
237+
let totalMessage = `\`\`\`\nInfo for server: ${msg.guild.name}`;
238+
if (!row.broadcastChannel) {
239+
totalMessage += '\nNo broadcast channel set';
240+
} else if (row.broadcastChannel === '-1') {
241+
totalMessage += '\nBroadcasting turned off for this server';
242+
} else {
243+
totalMessage += `\nBroadcast channel: ${msg.guild.channels.get(row.broadcastChannel).name}`;
244+
}
243245

244-
if (!row.mainWiki) {
245-
totalMessage += '\nNo main wiki set';
246-
} else {
247-
totalMessage += `\nMain wiki: ${row.mainWiki}`;
248-
}
246+
if (!row.mainWiki) {
247+
totalMessage += '\nNo main wiki set';
248+
} else {
249+
totalMessage += `\nMain wiki: ${row.mainWiki}`;
250+
}
249251

250-
sql.all(`SELECT * FROM overrides WHERE guildID="${msg.guild.id}"`).then(rows => {
251-
if (rows.length === 0) {
252-
totalMessage += '\nNo channel overrides set';
253-
} else {
254-
totalMessage += '\nChannel overrides:';
255-
for (let i = 0; i < rows.length; i++) {
256-
totalMessage += `\n Wiki ${rows[i].wiki} in channel ${msg.guild.channels.get(rows[i].channelID).name}`;
257-
}
258-
}
252+
sql.all(`SELECT * FROM overrides WHERE guildID="${msg.guild.id}"`).then(rows => {
253+
if (rows.length === 0) {
254+
totalMessage += '\nNo channel overrides set';
255+
} else {
256+
totalMessage += '\nChannel overrides:';
257+
for (let i = 0; i < rows.length; i++) {
258+
totalMessage += `\n Wiki ${rows[i].wiki} in channel ${msg.guild.channels.get(rows[i].channelID).name}`;
259+
}
260+
}
259261

260-
totalMessage += '\n```';
261-
msg.channel.send(totalMessage);
262-
}).catch(console.error);
263-
}).catch(console.error);
264-
}
262+
totalMessage += '\n```';
263+
msg.channel.send(totalMessage);
264+
}).catch(console.error);
265+
}).catch(console.error);
266+
}
265267
}
266268
};
267269

0 commit comments

Comments
 (0)