This repository has been archived by the owner on Jan 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantiraid.js
102 lines (89 loc) · 3.25 KB
/
antiraid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const chalk = require('chalk');
const { AntiraidSettings, GuildAntiraidSettingsSchema } = require('./classes');
module.exports = function (Kirbi) {
Kirbi.antiraidGuilds = {};
require('./lib/on-event')(Kirbi);
const GuildAntiraidSettings = Kirbi.Database.model('GuildAntiraidSettings', GuildAntiraidSettingsSchema);
return {
commands: [
'antiraid'
],
antiraid: {
usage: '<parameter> <new value?>',
description: 'Accesses the servers antiraid parameters. Adding a value will update the parameter.',
process: (msg, suffix, isEdit, cb) => {
const parameters = suffix.split(' ');
const parameter = parameters.shift().trim();
const settingTypes = AntiraidSettings.settingTypes();
// There must be at least one parameter passed to the command.
if (parameter === '') {
cb(`Please specify a property of the antiraid settings. \nAvailable properties: ${Object.keys(settingTypes).join(', ')}.`, msg);
return;
}
// Get the antiraid settings for the guild that the command was run from or instantiate it if it doesn't already exist.
let antiraidSettings = Kirbi.antiraidGuilds[msg.guild.id];
if (!antiraidSettings) {
const guild = Kirbi.Discord.guilds.find('id', msg.guild.id);
if (!guild) {
cb('Unable to set your guild settings for antiraid.', msg);
return;
}
const settings = new GuildAntiraidSettings({
guildId: guild.id,
channelId: guild.id
});
Kirbi.antiraidGuilds[msg.guild.id] = new AntiraidSettings(guild, settings);
antiraidSettings = Kirbi.antiraidGuilds[msg.guild.id];
}
// Only some values of the antiraid are allowed to be viewed and updated.
const settingType = settingTypes[parameter];
if (!settingType) {
cb(`That property is not available.`, msg);
return;
}
// If there is only one parameter, we just want to display the current value.
let value = antiraidSettings.settings[parameter];
if (parameters.length === 0) {
if (settingType === 'encodedString') {
value = unescape(value);
}
cb(`That ${parameter} antiraid setting is currently set to '${value}'.`, msg);
return;
}
// Ensure that the value is properly typed for the antraid setting.
value = parameters.join(' ').trim();
let message = `The ${parameter} antiraid setting has been set to '${value}'.`;
switch (settingType) {
case 'int':
value = Math.max(0, Number.parseInt(value, 10));
break;
case 'encodedString':
value = escape(value);
break;
default:
break;
}
// Attempt to set the value of the parameter.
try {
antiraidSettings.settings[parameter] = value;
GuildAntiraidSettings.findOneAndUpdate(
{ guildId: antiraidSettings.settings.guildId },
antiraidSettings.settings,
{ upsert: true },
function (error) {
if (error) {
console.log(chalk.red(`Error: ${error}`));
cb(`Something went wrong saving the ${parameter} value. This change will be lost on the next Kirbi restart.`, msg);
}
}
);
} catch (err) {
console.log(chalk.red(`Error: ${err}`));
message = 'Something went wrong setting the antiraid setting.';
}
// Display a message when done.
cb(message, msg);
}
}
};
};