forked from BlaCoiso/Casper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.js
204 lines (200 loc) · 9.29 KB
/
custom.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Module Docs___________________________
// | Name: Custom
// | Type: CASPER_MODULE
// | Function: Custom Command Module
// |_____________________________________
module.exports = {
handles(event) {
return event == "message";
},
fullName: "Custom Module",
commands: [
{
name: "addcommand",
description: "Adds a custom command.",
usage: "<command> [contents]",
aliases: ["addcmd", "addcom"],
perms: "mod",
noPermsMessage: true,
run(msg, args) {
if (args.params.length == 0) {
return { text: `\`${args.prefix + args.command}\`: ${this.description}\n**Available special options for custom command response**:\n \`{user}\`: Mentions the user\n \`{user.name}\`: Shows the user's current name\n \`{user.id}\`: Shows the user's ID\n \`{server}\`: Shows the server's name\n \`{server.id}\`: Shows the server's ID\n You can also use \`{0}\`, \`{1}\`, etc for arguments.` };
} else {
let commandName = args.params[0].toLowerCase();
let customCmds = args.moduleConf.customCommands;
let customAliases = args.moduleConf.customAliases;
if (args.moduleHandler.findCommand(commandName)) {
return { text: "The command with the name you entered already exists!" };
} else if (findCustomCommand(commandName, customCmds)) {
return { text: "A custom command with this name already exists! If you meant to edit it, use `editcommand` instead." };
} else if (findCustomCommand(commandName, customAliases)) {
return { text: "A custom alias with this name already exists!" };
} else {
let command = {
name: commandName,
perms: [],
editPerms: [],
aliases: [],
out: ""
};
if (args.params[1] && args.params[1] != "") {
command.out = args.params.slice(1).join(" ");
customCmds.push(command);
args.setModuleConfig(args.config, "Custom", "customCommands", customCmds);
return { text: `A custom command with the name \`${commandName}\` has successfully been created! Use \`editcommand\` to modify it.` };
} else {
let filter = m => m.author.id == msg.author.id;
args.channel.awaitMessages(filter, { maxMatches: 1, time: 30000, errors: ["time"] })
.then(c => {
let cmdContent = c.first().content;
command.out = cmdContent;
customCmds.push(command);
args.setModuleConfig(args.config, "Custom", "customCommands", customCmds);
args.asyncCallback({ text: "Custom command successfully created." });
})
.catch(c => args.asyncCallback({ text: "User took too long to reply, command creation canceled." }));
return { text: `Creating command \`${commandName}\`... Please enter the command's content.` };
}
}
}
}
},
{
name: "editcommand",
perms: "mod",
description: "Edits a custom command.",
aliases: ["editcom", "editcmd"],
noPermsMessage: true,
run(msg, args) {
if (args.params.length == 0) {
return { text: `\`${args.prefix + args.command}\`: ${this.description}` };
} else {
let customCmds = args.moduleConf.customCommands;
let name = args.params[0].toLowerCase();
let command = findCustomCommand(name, customCmds);
if (command) {
let newOut = args.params.slice(1).join(" ");
if (newOut.split(" ").join("").length != 0) {
command.out = newOut;
args.setModuleConfig(args.config, "Custom", "customCommands", customCmds);
return { text: `Updated \`${name}\` custom command.` };
} else {
return { text: "Command response wasn't specified." };
}
} else {
return { text: "Custom command wasn't found." };
}
}
}
},
{
name: "removecommand",
aliases: ["removecmd", "remcommand", "remcmd", "delcommand", "deletecommand", "delcmd", "deletecmd"],
perms: "mod",
description: "Deletes a custom command.",
usage: "<command>",
noPermsMessage: true,
requiresArgs: true,
run(msg, args) {
let customCmds = args.moduleConf.customCommands;
let name = args.params[0].toLowerCase();
let command = findCustomCommand(name, customCmds);
if (command) {
customCmds.splice(customCmds.indexOf(command), 1);
args.setModuleConfig(args.config, "Custom", "customCommands", customCmds);
return { text: "Successfully deleted custom command." };
} else {
return { text: "Can't find custom command to delete." };
}
}
},
{
name: "listcommands",
aliases: ["listcmds", "listcmd", "cmdlist"],
description: "Lists custom commands.",
run(msg, args) {
let customCmds = args.moduleConf.customCommands;
let cmdList = customCmds.map(c => c.name).join(", ");
return { text: "Available Custom Commands: " + (customCmds.length === 0 ? "None" : cmdList) };
}
}
],
needsConfig: true,
confV: 2,
generateConfig(oldConf, guild) {
if (oldConf.version == 1 && oldConf.customCommands) {
let names = [];
let newCmdList = [];
for (let cmd of oldConf.customCommands) {
if (cmd && names.indexOf(cmd.name.toLowerCase()) !== 1) {
cmd.name = cmd.name.toLowerCase();
names.push(cmd.name);
newCmdList.push(cmd);
}
}
oldConf.customCommands = newCmdList;
}
oldConf.customCommands = oldConf.customCommands || [];
oldConf.customAliases = oldConf.customAliases || [];
oldConf.version = this.confV;
return oldConf;
},
checkConfUpdates(oldConfig) {
return oldConfig.version != this.confV;
},
overridesMessage: true,
handle(eventType, args, Discord, Client, Config) {
if (eventType == "message") {
let msg = args.message;
if (!args.moduleConfig) {
return;
}
let customCmds = args.moduleConfig.customCommands;
let customAliases = args.moduleConfig.customAliases;
let cmd = findCustomCommand(args.command, customCmds);
let alias = findCustomCommand(args.command, customAliases);
if (cmd) {
let cmdOut = cmd.out;
let cmdRegTest1 = /{(\D[\w\.]*)}/g;
let cmdRegTest2 = /{(\d*)}/g;
//TODO: Add escape stuff
cmdOut = cmdOut.replace(cmdRegTest1, (total, match) => {
if (match == "user") {
return msg.member.toString();
} else if (match == "server") {
return msg.guild.name;
} else if (match == "user.name") {
return msg.member.displayName;
} else if (match == "user.tag") {
return msg.author.username + "#" + msg.author.discriminator;
} else if (match == "server.members") {
return msg.guild.members.size;
} else if (match == "user.id") {
return msg.author.id;
} else if (match == "server.id") {
return msg.guild.id;
} else if (match == "args") {
return args.paramsJoined;
} else {
return total;
}
});
cmdOut = cmdOut.replace(cmdRegTest2, (total, match) => args.params[parseInt(match)] || " ");
args.asyncCallback({ text: cmdOut });
}
// Else check custom aliases
}
}
};
function findCustomCommand(cmd, list) {
for (let command of list) {
if (command.name == cmd) return command;
}
return null;
}
function findCustomAliasFor(name, list) {
for (let alias of list) {
if (alias.target == name) return alias;
}
return null;
} //Useless?