forked from Anti-Raid/templating-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.luau
200 lines (178 loc) · 6.81 KB
/
init.luau
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
--local once = require "@antiraid-ext/once"
local promise = require "@antiraid/promise"
local luau = require "@antiraid/luau"
local executor = require "@antiraid-ext/executor"
local Primitives = require "@antiraid-core/primitives"
local serde = require "@lune/serde"
local discordApiTypes = require "@discord-types/apiTypes"
local discordRestTypes = require "@discord-types/restTypes"
local Message = require "@antiraid-ext/events/discord/Message"
local InteractionCreate = require "@antiraid-ext/events/discord/InteractionCreate"
local toArray = require "@antiraid-ext/array".toArray
local InteractionType = require "@discord-types/interaction".InteractionType
local InteractionCallbackType = require "@discord-types/interaction".InteractionCallbackType
local evt: Primitives.Event, ctx: Primitives.TemplateContext = ...
local ge = executor.NewGlobalExecutor(ctx)
Message(evt, function(message)
if message.content ~= "eval:makeCommand" then
return
end
local channelId: string;
if message.channel_id then
channelId = message.channel_id
else
return
end
local commandBuilder = require "@discord-types/builders/interaction/interaction"
local commandOptionBuilder = require "@discord-types/builders/interaction/option"
local ApplicationCommandOptionType = require "@discord-types/interaction".ApplicationCommandOptionType
local command = commandBuilder.new({
name = "vm-eval",
})
:addIntegrationType("GuildInstall")
:setType("ChatInput")
:addContext("Guild")
:setDescription("Evaluates Luau code in a VM")
:option(
function(opt)
return commandOptionBuilder.new()
:setType(ApplicationCommandOptionType.String)
:setName("code")
:setDescription("The code to evaluate")
:build()
end
)
promise.yield(ge.discord:create_message({
channel_id = channelId,
data = {
content = (serde.encode("json", command:build())) :: string?
}
}))
promise.yield(ge.discord:create_guild_command({
data = command:build() :: discordRestTypes.CreateGuildApplicationCommandRequest,
}))
end)
local function evaluateExpression(interaction: discordApiTypes.InteractionObject, content: string)
local status: "running" | "deferred" | "done" = "running"
local deferInteractionThread = task.delay(3, function()
if status == "running" then
promise.yield(ge.discord:create_interaction_response({
interaction_id = interaction.id,
interaction_token = interaction.token,
data = {
type = InteractionCallbackType.DeferredChannelMessageWithSource,
data = {}
} :: discordRestTypes.CreateInteractionRequest
}))
status = "deferred"
end
end)
-- Do some work here
local chunk = luau.load(ctx, content)
chunk.optimization_level = 2
chunk.chunk_name = "__vm_eval_chunk"
local ok, result = pcall(promise.yield, chunk:call_async({}))
-- Print result
if status == "deferred" then
promise.yield(ge.discord:create_followup_message({
interaction_token = interaction.token,
data = {
embeds = toArray({
{
title = "Result",
description = "```\n" .. tostring(result) .. "\n```",
fields = toArray({
{
name = "OK",
value = tostring(ok),
inline = false,
},
}),
color = if ok then 0x00FF00 else 0xFF0000,
},
})
} :: discordRestTypes.CreateFollowupMessageRequest
}))
else
promise.yield(ge.discord:create_interaction_response({
interaction_id = interaction.id,
interaction_token = interaction.token,
data = {
type = InteractionCallbackType.ChannelMessageWithSource,
data = {
embeds = toArray({
{
title = "Result",
description = "```\n" .. tostring(result) .. "\n```",
fields = toArray({
{
name = "OK",
value = tostring(ok),
inline = false,
},
}),
color = if ok then 0x00FF00 else 0xFF0000,
}
}),
}
} :: discordRestTypes.CreateInteractionRequest
}))
end
-- Done!
task.cancel(deferInteractionThread)
status = "done"
end
InteractionCreate(evt, function(interaction)
-- Keep store of the interaction object
local interactionObject: discordApiTypes.InteractionObject = interaction
if interaction.type ~= InteractionType.ApplicationCommand then
return
end
if not interaction.data then
return
end
if interaction.data.name ~= "vm-eval" then
return
end
--- Get the code value from the interaction
local options = interaction.data.options
if not options or #options == 0 then
promise.yield(ge.discord:create_interaction_response({
interaction_id = interaction.id,
interaction_token = interaction.token,
data = {
type = InteractionCallbackType.ChannelMessageWithSource,
data = {
embeds = toArray({
{
title = "Result",
description = "Modal support is TODO",
}
})
}
} :: discordRestTypes.CreateInteractionRequest
}))
return
end
local code = tostring(options[1].value)
if not code or code == "" then
promise.yield(ge.discord:create_interaction_response({
interaction_id = interaction.id,
interaction_token = interaction.token,
data = {
type = InteractionCallbackType.ChannelMessageWithSource,
data = {
embeds = toArray({
{
title = "Result",
description = "No code provided",
}
})
}
} :: discordRestTypes.CreateInteractionRequest
}))
return
end
-- Evaluate the code
evaluateExpression(interactionObject, code)
end)