-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtriggers.js
More file actions
137 lines (119 loc) · 3.82 KB
/
Copy pathtriggers.js
File metadata and controls
137 lines (119 loc) · 3.82 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
import * as regex from '@aquarius-bot/regex';
import { isBot } from '@aquarius-bot/users';
/** @typedef {import('discord.js').Message} Message */
/**
* Get Aquarius's mention regex
* @returns {RegExp} Aquarius's mention regex
*/
export function botMention(id) {
return new RegExp(`<@!?${id}>`);
}
/**
* Check to see if a message starts with an Aquarius mention
* @param {Message} message - the message to check
* @returns {?RegExpMatchArray} the regex match array or null if no match
*/
export function botMentionTrigger(message) {
return message.content
.trim()
.match(new RegExp(`^${botMention(message.client.user.id).source}`));
}
function startsWithTrigger(message, character) {
return message.content.trim().startsWith(character);
}
/**
* Check to see if a message starts with `.`
* @param {Message} message - the message to check
* @returns {boolean} Whether the message starts with `.`
*/
export function dotTrigger(message) {
return startsWithTrigger(message, '.');
}
/**
* Check to see if a message starts with `;`
* @param {Message} message - the message to check
* @returns {boolean} Whether the message starts with `;`
*/
export function semicolonTrigger(message) {
return startsWithTrigger(message, ';');
}
/**
* Check to see if a message starts with `?`
* @param {Message} message - the message to check
* @returns {boolean} Whether the message starts with `?`
*/
export function questionTrigger(message) {
return startsWithTrigger(message, '?');
}
/**
* Check to see if a message starts with `!`
* @param {Message} message - the message to check
* @returns {boolean} Whether the message starts with `!`
*/
export function exclamationTrigger(message) {
return startsWithTrigger(message, '!');
}
/**
* Checks to see if a non-bot message matches a trigger
* @param {Message} message - the message to check
* @param {RegExp} trigger - the custom trigger to check against
* @returns {boolean|?RegExpMatchArray} False if a bot sends the message or
* Array/Null regex match group
*/
export function customTrigger(message, trigger) {
if (isBot(message.author)) {
return false;
}
return message.content.trim().match(trigger);
}
/**
* Get a list of card-style matches in a non-bot message
* (the [[Double Brackets]] syntax)
* @param {Message} message - the message to check
* @returns {boolean|?Array} False if the message is from a bot or
* an Array of string matches or null if none exist
* @todo TODO: Update return type to the regex matchAll type when typescript adds it
*/
export function bracketTrigger(message) {
if (isBot(message.author)) {
return false;
}
const matchList = Array.from(
message.content.trim().matchAll(new RegExp(regex.BRACKET, 'g'))
);
if (matchList.length > 0) {
return matchList;
}
return false;
}
/**
* Looks to see if a non-bot message should trigger against a Regex by
* examining various patterns to key off of.
* @param {Message} message - the message to check
* @param {RegExp} trigger - the custom trigger to check against
* @returns {boolean|?RegExpMatchArray} False if no match or a bot sends the
* message or an Array or null from a RegExp match group
*/
export function messageTriggered(message, trigger) {
// We don't respond to other bots. The "Shaun Boley" check
if (isBot(message.author)) {
return false;
}
// @aquarius trigger [message]
if (botMentionTrigger(message)) {
return message.content
.trim()
.replace(new RegExp(`^${regex.MENTION_USER.source} `), '')
.match(trigger);
}
// .trigger [message] OR !trigger [message] OR ;trigger [message]
if (
dotTrigger(message, trigger) ||
exclamationTrigger(message, trigger) ||
questionTrigger(message, trigger) ||
semicolonTrigger(message, trigger)
) {
return message.content.trim().substr(1).match(trigger);
}
return false;
}