-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (51 loc) · 1.66 KB
/
index.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
const utils = require('@basedakp48/plugin-utils');
const random = require('./src/util/random');
const getFlags = require('./src/util/getFlags');
const plugin = new utils.Plugin({ dir: __dirname });
const prefix = '.'; // Pre-CommandProcessor
const commandData = {
description: 'Pick a random word/item.',
usage: '{prefix}{command} item1 item2 item3',
aliases: ['choose', 'pick'],
flags: {
split: ' ',
},
};
let commandProcessorExists = false;
plugin.presenceSystem();
plugin.messageSystem().on('message/command', (msg) => {
commandProcessorExists = true;
sendMessage(msg, handleCommand(msg.data))
});
function sendMessage(msg, text) {
if (!text) return;
// Maybe?
plugin.messageSystem().sendText(text, msg);
}
function handleCommand({ command = '', text = '', flags = {} }) {
if (!commandData.aliases.includes(command)) return '';
const array = text.split(getOptions(flags).split);
return array[random(array.length)].trim();
}
function getOptions(overrides) {
return Object.assign({}, commandData.flags, overrides)
}
// Pre-CommandProcessor
plugin.messageSystem().on('message-in', (msg) => {
if (commandProcessorExists || msg.type !== 'text' || !(msg.text.startsWith(prefix) || msg.data.isPM)) return;
const {message, flags} = getFlags(msg.text);
const args = message.split(' ');
// Trim space between prefix and command
if (args[0] === prefix) args.shift();
sendMessage(msg, handleCommand({
flags,
command: getCommand(args.shift()).toLowerCase().trim(),
text: args.join(' ').trim(),
}));
});
function getCommand(text = '') {
if (text.startsWith(prefix)) {
return text.substring(prefix.length);
}
return text;
}