-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.messageCreate.native.ts
162 lines (129 loc) · 4.15 KB
/
command.messageCreate.native.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// system file, please don't modify it
import * as app from "#app"
import config from "#config"
import env from "#env"
import yargsParser from "yargs-parser"
export default new app.Listener({
event: "messageCreate",
description: "Handle the messages for commands",
async run(message) {
if (config.ignoreBots && message.author.bot) return
if (!app.isAnyMessage(message)) return
const prefix = config.getPrefix
? await config.getPrefix(message)
: env.BOT_PREFIX
if (new RegExp(`^<@!?${message.client.user.id}>$`).test(message.content))
return message.channel
.send(
await app.getSystemMessage("default", `My prefix is \`${prefix}\``),
)
.catch()
message.usedAsDefault = false
message.isFromBotOwner = message.author.id === app.env.BOT_OWNER
app.emitMessage(message.channel, message)
app.emitMessage(message.author, message)
if (app.isGuildMessage(message)) {
message.isFromGuildOwner =
message.isFromBotOwner || message.guild.ownerId === message.author.id
app.emitMessage(message.guild, message)
app.emitMessage(message.member, message)
}
let dynamicContent = message.content
const cut = function (key: string) {
dynamicContent = dynamicContent.slice(key.length).trim()
}
const mentionRegex = new RegExp(`^(<@!?${message.client.user.id}>) ?`)
if (dynamicContent.startsWith(prefix)) {
message.usedPrefix = prefix
cut(prefix)
} else if (mentionRegex.test(dynamicContent)) {
const [match, used] = mentionRegex.exec(dynamicContent) as RegExpExecArray
message.usedPrefix = `${used} `
cut(match)
} else if (app.isDirectMessage(message)) {
message.usedPrefix = ""
} else return
let key = dynamicContent.split(/\s+/)[0]
// turn ON/OFF
if (
key !== "turn" &&
!app.cache.ensure<boolean>("turn", true) &&
message.author.id !== app.env.BOT_OWNER
)
return
let cmd = app.commands.resolve(key)
if (!cmd) {
if (app.defaultCommand) {
key = ""
cmd = app.defaultCommand
message.usedAsDefault = true
} else return null
}
// check sub commands
{
let cursor = 0
let depth = 0
while (cmd.options.subs && cursor < cmd.options.subs.length) {
const subKey = dynamicContent.split(/\s+/)[depth + 1]
for (const sub of cmd.options.subs) {
if (sub.options.name === subKey) {
key += ` ${subKey}`
cursor = 0
cmd = sub
depth++
break
} else if (sub.options.aliases) {
let found = false
for (const alias of sub.options.aliases) {
if (alias === subKey) {
key += ` ${subKey}`
cursor = 0
cmd = sub
depth++
found = true
break
}
}
if (found) break
}
cursor++
}
}
}
cut(key.trim())
const baseContent = dynamicContent
// parse CommandMessage arguments
const parsedArgs = yargsParser(dynamicContent)
const restPositional = (parsedArgs._?.slice() ?? []).map(String)
message.args = restPositional.map((positional) => {
if (/^(?:".+"|'.+')$/.test(positional))
return positional.slice(1, positional.length - 1)
return positional
})
// handle help argument
if (parsedArgs.help || parsedArgs.h)
return app.sendCommandDetails(message, cmd)
// prepare command
const prepared = await app.prepareCommand(message, cmd, {
restPositional,
baseContent,
parsedArgs,
key,
})
if (typeof prepared !== "boolean")
return message.channel
.send({ ...prepared, allowedMentions: { parse: [] } })
.catch()
if (!prepared) return
try {
await cmd.options.run.bind(cmd)(message)
} catch (error: any) {
app.error(error, cmd.filepath!, true)
message.channel
.send(await app.getSystemMessage("error", error))
.catch((error) => {
app.error(error, cmd!.filepath!, true)
})
}
},
})