This repository has been archived by the owner on Apr 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createMessageMatcher.ts
67 lines (60 loc) · 2.08 KB
/
createMessageMatcher.ts
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
import Discord from "discord.js"
import { EventList } from "../struct/Module"
import Action from "../struct/action/Action"
import { PunishmentProperties } from "../struct/PunishmentProvider"
import DeleteAction from "../struct/action/DeleteAction"
type MessageMatcherProperties = {
matcher: (config: any, message: Discord.Message) => Promise<boolean> | boolean
module: string
reason: string
}
export default function createMessageMatcher(
properties: MessageMatcherProperties
): EventList {
return {
message: createMessageHandler("message", properties),
messageUpdate: createMessageHandler("messageUpdate", properties)
}
}
function createMessageHandler(
event: "message" | "messageUpdate",
properties: MessageMatcherProperties
) {
return async function messageHandler(
config: {
delete: boolean
punishment: PunishmentProperties[]
[key: string]: any
},
oldMessage: Discord.Message,
newMessage?: Discord.Message
) {
const message = event === "message" ? oldMessage : newMessage
if (event === "messageUpdate" && oldMessage.content === newMessage.content) return
if (!message.content) return
const actions: Action[] = []
const matcher: typeof properties.matcher = properties.matcher.bind(this)
const matches = await matcher(config, message)
if (matches) {
if (config.delete) {
actions.push(
new DeleteAction(this, {
module: properties.module,
target: message,
reason: properties.reason
})
)
}
if (config.punishment) {
actions.push(
this.punishment.processPunishment(config.punishment, {
module: properties.module,
target: message.member,
reason: properties.reason
})
)
}
}
return actions
}
}