|
| 1 | +import { Events } from 'discord.js'; |
| 2 | +import { MINUTE } from '../constants/time.js'; |
| 3 | +import { loadMarkdownOptions } from '../util/markdown.js'; |
| 4 | +import { rateLimit } from '../util/rate-limit.js'; |
| 5 | +import { createEvent } from './index.js'; |
| 6 | + |
| 7 | +// Subject patterns (who) |
| 8 | +const reSubject = `(?:(?:any|some|no|every)(?:one|body)|people|folks|peeps|who)`; |
| 9 | + |
| 10 | +// Verb patterns (has/knows/can help/etc) |
| 11 | +const reVerb = `(?:ha[sv]e?|got|knows?|can(?: help)?|tried|used|worked(?: with)?|familiar(?: with)?|experience[ds]?(?: with)?|heard(?: of)?|seen)`; |
| 12 | + |
| 13 | +// Quantifiers (optional intensity) |
| 14 | +const reQuantifier = `(?:any|some|much|good|prior|past|previous)`; |
| 15 | + |
| 16 | +// Question patterns |
| 17 | +const reQuestion = `(?:experience|knowledge|info(?:rmation)?|ideas?|clues?|tips?|advice|help|thoughts?|insights?|suggestions?)`; |
| 18 | + |
| 19 | +// Common connectors |
| 20 | +const reConnector = `(?:with|about|on|regarding|for|of)`; |
| 21 | + |
| 22 | +const askToAskPattern = new RegExp( |
| 23 | + String.raw`\b${reSubject}\s+${reVerb}\s+(?:${reQuantifier}\s+)?(?:${reQuestion}\s+)?(?:${reConnector}\s+)?.+\??`, |
| 24 | + 'i' |
| 25 | +); |
| 26 | + |
| 27 | +const isAskingToAsk = (text: string) => askToAskPattern.test(text); |
| 28 | + |
| 29 | +const [response] = await loadMarkdownOptions<{ name: string }>( |
| 30 | + new URL('../commands/tips/subjects/', import.meta.url), |
| 31 | + 'justask.md' |
| 32 | +); |
| 33 | + |
| 34 | +const { canRun, reset } = rateLimit(10 * MINUTE); |
| 35 | + |
| 36 | +export default createEvent( |
| 37 | + { |
| 38 | + name: Events.MessageCreate, |
| 39 | + }, |
| 40 | + async (message) => { |
| 41 | + if (!canRun()) return; |
| 42 | + if (message.author.bot) return; |
| 43 | + |
| 44 | + if (message.content.split(' ').length > 10) return; |
| 45 | + |
| 46 | + if (isAskingToAsk(message.content)) { |
| 47 | + await message.reply({ |
| 48 | + content: response.content, |
| 49 | + }); |
| 50 | + reset(); |
| 51 | + } |
| 52 | + } |
| 53 | +); |
0 commit comments