-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval.native.ts
148 lines (130 loc) · 3.75 KB
/
eval.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
// native file, if you want edit it, remove the "native" suffix from the filename
import cp from "child_process"
import util from "util"
import * as discordEval from "discord-eval.ts"
import * as app from "#app"
import discord from "discord.js"
const exec = util.promisify(cp.exec)
const alreadyInstalled = (pack: string): boolean =>
!!app.packageJSON.dependencies?.hasOwnProperty(pack) ||
!!app.packageJSON.devDependencies?.hasOwnProperty(pack)
export default new app.Command({
name: "eval",
description: "JS code evaluator",
channelType: "all",
botOwnerOnly: true,
aliases: ["js", "code", "run", "="],
rest: {
name: "code",
description: "The evaluated code",
required: true,
},
options: [
app.option({
name: "use",
type: "array",
description: "NPM packages I want to includes in my code",
validate: (packages) => {
return packages.length > 0
},
default: [],
}),
],
flags: [
{
name: "muted",
flag: "m",
description: "Disable message feedback",
aliases: ["mute"],
},
{
name: "verbose",
flag: "v",
aliases: ["info", "information"],
description: "Information about output",
},
],
async run(message) {
const installed = new Set<string>()
const code = message.args.code
const use = message.args.use
if (use.length > 0) {
const given = new Set<string>(use.filter((p: string) => p))
for (const pack of given) {
if (alreadyInstalled(pack)) {
await message.channel.send(
`${app.getSystemEmoji("success")} **${pack}** - installed`,
)
installed.add(pack)
} else {
let log
try {
log = await message.channel.send(
`${app.getSystemEmoji("loading")} **${pack}** - install...`,
)
await exec(`npm i ${pack}@latest`)
await log.edit(
`${app.getSystemEmoji("success")} **${pack}** - installed`,
)
installed.add(pack)
} catch {
if (log)
await log.edit(
`${app.getSystemEmoji("error")} **${pack}** - error`,
)
else
await message.channel.send(
`${app.getSystemEmoji("error")} **${pack}** - error`,
)
}
}
}
}
const req = Object.fromEntries(
await Promise.all(
[...installed].map(async (pack) => [pack, await import(pack)]),
),
)
const embed = await discordEval.evaluate(
code,
{
ctx: { message, app, req },
muted: message.args.muted,
verbose: message.args.verbose,
},
{
success: new discord.EmbedBuilder().setColor(app.systemColors.success),
error: new discord.EmbedBuilder().setColor(app.systemColors.error),
},
)
await message.channel.send({
embeds: [embed],
})
let somePackagesRemoved = false
for (const pack of installed) {
if (alreadyInstalled(pack)) continue
somePackagesRemoved = true
let log
try {
log = await message.channel.send(
`${app.getSystemEmoji("loading")} **${pack}** - uninstall...`,
)
await exec(`npm remove --purge ${pack}`)
await log.edit(
`${app.getSystemEmoji("success")} **${pack}** - uninstalled`,
)
} catch {
if (log)
await log.edit(`${app.getSystemEmoji("error")} **${pack}** - error`)
else
await message.channel.send(
`${app.getSystemEmoji("error")} **${pack}** - error`,
)
}
}
if (somePackagesRemoved)
return message.channel.send(
`${app.getSystemEmoji("success")} process completed`,
)
},
})