forked from Dragory/modmailbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.js
More file actions
194 lines (169 loc) · 5.46 KB
/
commands.js
File metadata and controls
194 lines (169 loc) · 5.46 KB
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
const { CommandManager, defaultParameterTypes, TypeConversionError, IParameter, ICommandConfig } = require("knub-command-manager");
const Eris = require("eris");
const config = require("./cfg");
const utils = require("./utils");
const threads = require("./data/threads");
const Thread = require("./data/Thread");
/**
* @callback CommandFn
* @param {Eris.Message} msg
* @param {object} args
*/
/**
* @callback InboxServerCommandHandler
* @param {Eris.Message} msg
* @param {object} args
* @param {Thread} [thread]
*/
/**
* @callback InboxThreadCommandHandler
* @param {Eris.Message} msg
* @param {object} args
* @param {Thread} thread
*/
/**
* @callback AddGlobalCommandFn
* @param {string} trigger
* @param {string} parameters
* @param {CommandFn} handler
* @param {ICommandConfig} commandConfig
*/
/**
* @callback AddInboxServerCommandFn
* @param {string} trigger
* @param {string} parameters
* @param {InboxServerCommandHandler} handler
* @param {ICommandConfig} commandConfig
*/
/**
* @callback AddInboxThreadCommandFn
* Add a command that can only be invoked in a thread on the inbox server
*
* @param {string} trigger
* @param {string} parameters
* @param {InboxThreadCommandHandler} handler
* @param {ICommandConfig} commandConfig
*/
/**
* @callback AddAliasFn
* @param {string} originalCmd
* @param {string} alias
*/
module.exports = {
createCommandManager(bot) {
const manager = new CommandManager({
prefix: config.prefix,
types: Object.assign({}, defaultParameterTypes, {
userId(value) {
const userId = utils.getUserMention(value);
if (! userId) throw new TypeConversionError();
return userId;
},
delay(value) {
const ms = utils.convertDelayStringToMS(value);
if (ms === null) throw new TypeConversionError();
return ms;
}
})
});
const handlers = {};
const aliasMap = new Map();
bot.on("messageCreate", async msg => {
if (msg.author.bot) return;
if (msg.author.id === bot.user.id) return;
if (! msg.content) return;
const matchedCommand = await manager.findMatchingCommand(msg.content, { msg });
if (matchedCommand === null) return;
if (matchedCommand.error !== undefined) {
utils.postError(msg.channel, matchedCommand.error);
return;
}
const allArgs = {};
for (const [name, arg] of Object.entries(matchedCommand.args)) {
allArgs[name] = arg.value;
}
for (const [name, opt] of Object.entries(matchedCommand.opts)) {
allArgs[name] = opt.value;
}
handlers[matchedCommand.id](msg, allArgs);
});
/**
* Add a command that can be invoked anywhere
* @type {AddGlobalCommandFn}
*/
const addGlobalCommand = (trigger, parameters, handler, commandConfig = {}) => {
let aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
if (commandConfig.aliases) aliases.push(...commandConfig.aliases);
const cmd = manager.add(trigger, parameters, { ...commandConfig, aliases });
handlers[cmd.id] = handler;
};
/**
* Add a command that can only be invoked on the inbox server
* @type {AddInboxServerCommandFn}
*/
const addInboxServerCommand = (trigger, parameters, handler, commandConfig = {}) => {
const aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
if (commandConfig.aliases) aliases.push(...commandConfig.aliases);
const cmd = manager.add(trigger, parameters, {
...commandConfig,
aliases,
preFilters: [
async (_, context) => {
if (! await utils.messageIsOnInboxServer(bot, context.msg)) return false;
if (! utils.isStaff(context.msg.member)) return false;
return true;
}
]
});
handlers[cmd.id] = async (msg, args) => {
const thread = await threads.findOpenThreadByChannelId(msg.channel.id);
handler(msg, args, thread);
};
};
/**
* Add a command that can only be invoked in a thread on the inbox server
* @type {AddInboxThreadCommandFn}
*/
const addInboxThreadCommand = (trigger, parameters, handler, commandConfig = {}) => {
const aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
if (commandConfig.aliases) aliases.push(...commandConfig.aliases);
let thread;
const cmd = manager.add(trigger, parameters, {
...commandConfig,
aliases,
preFilters: [
async (_, context) => {
if (! await utils.messageIsOnInboxServer(bot, context.msg)) return false;
if (! utils.isStaff(context.msg.member)) return false;
if (commandConfig.allowSuspended) {
thread = await threads.findByChannelId(context.msg.channel.id);
} else {
thread = await threads.findOpenThreadByChannelId(context.msg.channel.id);
}
if (! thread) return false;
return true;
}
]
});
handlers[cmd.id] = async (msg, args) => {
handler(msg, args, thread);
};
};
/**
* @type {AddAliasFn}
*/
const addAlias = (originalCmd, alias) => {
if (! aliasMap.has(originalCmd)) {
aliasMap.set(originalCmd, new Set());
}
aliasMap.get(originalCmd).add(alias);
};
return {
manager,
addGlobalCommand,
addInboxServerCommand,
addInboxThreadCommand,
addAlias,
};
}
};