-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (67 loc) · 1.83 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const utils = require('@basedakp48/plugin-utils');
const getFlags = require('./src/util/getFlags');
const plugin = new utils.Plugin({ dir: __dirname, name: 'Say' });
const PREFIX = '.';
const commandData = {
description: 'Have the bot text',
usage: '{prefix}{command} text',
aliases: ['say'],
flags: {
mention: false,
},
};
let commandProcessorExists = false;
plugin.presenceSystem();
plugin.messageSystem().on('message/command', (msg) => {
if (commandProcessorExists) { // The first message will always be processed by the message-in system.
sendMessage(msg, handleCommand(msg));
}
commandProcessorExists = true;
});
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(/\s+/g);
// Trim space between prefix and command
if (args[0] === PREFIX) args.shift();
const command = getCommand(args.shift()).trim();
sendMessage(msg, handleCommand({
uid: msg.uid,
data: {
flags,
command: command.toLowerCase(),
text: message.substring(message.indexOf(command) + command.length + 1).trim(),
},
}));
});
function handleCommand({
uid,
data: {
command = '',
text = '',
flags = {}
}
}) {
if (!commandData.aliases.includes(command) || !text) return '';
if (getOptions(flags).mention) {
text = `<@!${uid}>: ${text}`; // Just assumes it's discord
}
return text;
}
function sendMessage(msg, text) {
if (text) {
plugin.messageSystem().sendText(text, msg)
}
}
function getOptions(overrides = {}) {
return {
...commandData.flags,
...overrides,
};
}
function getCommand(text = '') {
if (text.startsWith(PREFIX)) {
return text.substring(PREFIX.length);
}
return text;
}