-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjust-ask.ts
More file actions
77 lines (65 loc) · 2.22 KB
/
just-ask.ts
File metadata and controls
77 lines (65 loc) · 2.22 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
import { Events, type Message, PermissionFlagsBits } from 'discord.js';
import { MINUTE } from '../constants/time.js';
import { config } from '../env.js';
import { createEvent } from '../util/events.js';
import { loadMarkdownOptions } from '../util/markdown.js';
import { rateLimit } from '../util/rate-limit.js';
// Subject patterns (who)
const reSubject = `(?:(?:any|some|no|every)(?:one|body)|people|folks|peeps|who)`;
// Verb patterns (has/knows/can help/etc)
const reVerb = `(?:ha[sv]e?|got|knows?|can(?: help)?|tried|used|worked(?: with)?|familiar(?: with)?|experience[ds]?(?: with)?|heard(?: of)?|seen)`;
// Quantifiers (optional intensity)
const reQuantifier = `(?:any|some|much|good|prior|past|previous)`;
// Question patterns
const reQuestion = `(?:experience|knowledge|info(?:rmation)?|ideas?|clues?|tips?|advice|help|thoughts?|insights?|suggestions?)`;
// Common connectors
const reConnector = `(?:with|about|on|regarding|for|of)`;
const askToAskPattern = new RegExp(
String.raw`\b${reSubject}\s+${reVerb}\s+(?:${reQuantifier}\s+)?(?:${reQuestion}\s+)?(?:${reConnector}\s+)?.+\?`,
'i'
);
const isAskingToAsk = (text: string) => askToAskPattern.test(text);
const [response] = await loadMarkdownOptions<{ name: string }>(
new URL('../commands/tips/subjects/', import.meta.url),
'justask.md'
);
const { canRun, reset } = rateLimit(10 * MINUTE);
const shouldCheck = (message: Message) => {
// check rate limit
if (!canRun()) {
return false;
}
// check author
if (message.author.bot || message.author.system) {
return false;
}
// check roles/permissions
if (
message.member !== null &&
(message.member.roles.highest.comparePositionTo(config.roleIds.repel) >= 0 ||
message.member.permissions.has(PermissionFlagsBits.ModerateMembers))
) {
return false;
}
return true;
};
export const justAskEvent = createEvent(
{
name: Events.MessageCreate,
},
async (message) => {
if (!shouldCheck(message)) {
return;
}
// Ignore long messages, likely user provided more context
if (message.content.split(' ').length > 10) {
return;
}
if (isAskingToAsk(message.content)) {
await message.reply({
content: response.content,
});
reset();
}
}
);