This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
402 lines (371 loc) · 11.8 KB
/
index.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// `nodes` contain any nodes you add from the graph (dependencies)
// `root` is a reference to this program's root node
// `state` is an object that persists across program updates. Store data here.
import { nodes, root, state } from "membrane";
import ClientOAuth2 from "client-oauth2";
import { api, oauthRequest, verifyHeaders } from "./utils";
export const Root = {
status() {
if (!state.token || !state.publicKey) {
return `Please follow [these instructions](https://github.com/membrane-io/membrane-driver-discord) to setup your bot`;
}
return "Ready";
},
configure: async ({ clientId, clientSecret, token, publicKey }) => {
state.token = token;
state.publicKey = publicKey;
state.endpointUrl = await nodes.endpoint.$get();
// Get and save the application id for commands endpoint
const req = await api("GET", "oauth2/applications/@me");
const application = await req.json();
state.applicationId = application.id;
// set up oauth2 for join to Discord servers
state.auth = new ClientOAuth2(
{
clientId: clientId,
clientSecret: clientSecret,
accessTokenUri: "https://discord.com/api/oauth2/token",
authorizationUri: "https://discord.com/api/oauth2/authorize",
redirectUri: `${state.endpointUrl}/callback`,
},
oauthRequest
);
},
parse({ name, value }, { self }) {
switch (name) {
case "guild": {
const [id] = value.match(/([0-9]{18})/g);
return [root.guilds.one({ id })];
}
case "channel": {
const url = new URL(value);
const [, , guildId, channelId] = url.pathname.split("/");
return [
root.guilds.one({ id: guildId }).channels.one({ id: channelId }),
];
}
case "user": {
const [id] = value.match(/[0-9]+/g);
return [root.users.one({ id })];
}
case "message": {
const [channelId, messageId] = value.match(/[0-9]+/g);
return [
root.guilds.one.channels
.one({ id: channelId })
.messages.one({ id: messageId }),
];
}
}
return [];
},
guilds: () => ({}),
users: () => ({}),
me: async () => {
const res = await api("GET", "users/@me");
return await res.json();
},
followUpWebhook: async ({ application_id, token, message }) => {
await api(
"POST",
`webhooks/${application_id}/${token}`,
{},
JSON.stringify(message)
);
},
tests: () => ({}),
};
export const Tests = {
ping: async () => {
const res = await root.me.username;
return typeof res === "string";
},
testGetGuilds: async () => {
const res = await root.guilds.items.$query(`{ id }`);
return Array.isArray(res);
},
testGetUsers: async () => {
const id = await root.me.id;
const res = await root.users.one({ id }).$query(`{ id }`);
return typeof res === "object";
},
testGetChannels: async () => {
const [guild]: any = await root.guilds.items.$query(`{ id }`);
const res = await root.guilds
.one({ id: guild.id })
.channels.items.$query(`{ id }`);
return Array.isArray(res);
},
};
export const UserCollection = {
one: async ({ id }) => {
const res = await api("GET", `users/${id}`);
return await res.json();
},
};
export const CommandCollection = {
one: async ({ id }, { self }) => {
const { id: guildId } = self.$argsAt(root.guilds.one);
// Get the commands
const res = await api(
"GET",
`applications/${state.applicationId}/guilds/${guildId}/commands/${id}`
);
return await res.json();
},
items: async (_, { self }) => {
const { id } = self.$argsAt(root.guilds.one);
// Get the commands
const res = await api(
"GET",
`applications/${state.applicationId}/guilds/${id}/commands`
);
return await res.json();
},
};
export const MemberCollection = {
one: async ({ id }, { self }) => {
const { id: guildId } = self.$argsAt(root.guilds.one);
const res = await api("GET", `guilds/${guildId}/members/${id}`);
return await res.json();
},
page: async (args, { self }) => {
const { id } = self.$argsAt(root.guilds.one);
const res = await api("GET", `guilds/${id}/members`, { ...args });
const items = await res.json();
// Get the last user id
const lastId = items[items.length - 1].user.id;
return { items, next: self.page({ limit: args.limit, after: lastId }) };
},
};
export const GuildCollection = {
one: async ({ id }) => {
const res = await api("GET", `guilds/${id}`);
return await res.json();
},
items: async () => {
const res = await api("GET", "users/@me/guilds");
return await res.json();
},
};
export const ChannelCollection = {
one: async ({ id }) => {
const res = await api("GET", `channels/${id}`);
return await res.json();
},
items: async (_, { self }) => {
const { id } = self.$argsAt(root.guilds.one);
const res = await api("GET", `guilds/${id}/channels`);
return await res.json();
},
};
export const MessageCollection = {
one: async ({ id }, { self }) => {
const { id: channelId } = self.$argsAt(root.guilds.one.channels.one);
const res = await api("GET", `channels/${channelId}/messages/${id}`);
return await res.json();
},
items: async (args, { self }) => {
const { id: channelId } = self.$argsAt(root.guilds.one.channels.one);
const res = await api("GET", `channels/${channelId}/messages`, { ...args });
return await res.json();
},
};
export const Guild = {
gref(_, { obj }) {
return root.guilds.one({ id: obj.id });
},
channels: () => ({}),
commands: () => ({}),
members: () => ({}),
createCommand: async (args, { self }) => {
const { id } = self.$argsAt(root.guilds.one);
const res = await api(
"POST",
`applications/${state.applicationId}/guilds/${id}/commands`,
null,
JSON.stringify({
name: args.name,
description: args.description,
options: args.options || [],
type: args.type || 1,
})
);
return await res.json();
},
};
export const Command = {
async gref(_, { self, obj }) {
const { id } = self.$argsAt(root.guilds.one);
return root.guilds.one({ id }).commands.one({ id: obj.id });
},
delete: async (_, { self, obj }) => {
const { id: guildId } = self.$argsAt(root.guilds.one);
const { id } = self.$argsAt(root.guilds.one.commands.one);
const res = await api(
"DELETE",
`applications/${state.applicationId}/guilds/${guildId}/commands/${id}`
);
},
};
export const Channel = {
gref(_, { obj, self }) {
const { id } = self.$argsAt(root.guilds.one);
return root.guilds.one({ id }).channels.one({ id: obj.id });
},
messages: () => ({}),
sendMessage: async (args, { self }) => {
const { id } = self.$argsAt(root.guilds.one.channels.one);
const msg = {
content: args.content,
components: JSON.parse(args.components || "[]"),
embeds: JSON.parse(args.embeds || "[]"),
};
if (args.message_reference) msg["message_reference"] = args.message_reference;
if (args.allowed_mentions) msg["allowed_mentions"] = args.allowed_mentions;
const res = await api(
"POST",
`channels/${id}/messages`,
null,
JSON.stringify(msg)
);
return await res.json();
},
};
export const Message = {
gref(_, { obj, self }) {
const { id: guildId } = self.$argsAt(root.guilds.one);
const { id: channelId } = self.$argsAt(root.guilds.one.channels.one);
return root.guilds
.one({ id: guildId })
.channels.one({ id: channelId })
.messages.one({ id: obj.id });
},
reactionsByEmote: async (args, { self }) => {
const { id: channelId } = self.$argsAt(root.guilds.one.channels.one);
const { id: messageId } = self.$argsAt(root.guilds.one.channels.one.messages.one);
const emoteStr = args.emote_str;
const res = await api(
"GET",
`channels/${channelId}/messages/${messageId}/reactions/${emoteStr}`,
null);
return await res.json();
},
postReaction: async (args, { self }) => {
const { id: channelId } = self.$argsAt(root.guilds.one.channels.one);
const { id: messageId } = self.$argsAt(root.guilds.one.channels.one.messages.one);
const emoteStr = args.emote_str;
const res = await api(
"PUT",
`channels/${channelId}/messages/${messageId}/reactions/${emoteStr}/@me`,
null
);
}
};
export const User = {
gref(_, { obj }) {
return root.users.one({ id: obj.id });
},
};
export const Member = {
gref(_, { obj, self }) {
const { id } = self.$argsAt(root.guilds.one);
return root.guilds.one({ id }).members.one({ id: obj.user.id });
},
};
export const Reaction = {
gref(_, { obj, self}) {
const { id: guildId } = self.$argsAt(root.guilds.one);
const { id: channelId } = self.$argsAt(root.guilds.one.channels.one);
const { id: messageId } = self.$argsAt(root.guilds.one.channels.one.messages.one)
const emojiUri = encodeURIComponent(obj.emoji.name);
return root.guilds
.one({ id: guildId })
.channels.one({ id: channelId })
.messages.one({ id: messageId })
.reactionsByEmote({emote_str: emojiUri});
}
}
export const Emoji = {
gref(_, { obj, self }) {
const { id: guildId } = self.$argsAt(root.guilds.one);
const { id: channelId } = self.$argsAt(root.guilds.one.channels.one);
const { id: messageId } = self.$argsAt(root.guilds.one.channels.one.messages.one)
return root.guilds
.one({ id: guildId })
.channels.one({ id: channelId })
.messages.one({ id: messageId });
}
}
export async function endpoint({ path, query, headers, method, body }) {
switch (path) {
case "/": {
return '<a href="/auth">Add bot to Discord server</a>';
}
case "/auth":
case "/auth/": {
if (!state.auth) {
return "Please invoke `:configure` first";
}
const url = state.auth.code.getUri({
query: { permissions: 8, scope: "bot" },
});
return JSON.stringify({ status: 303, headers: { location: url } });
}
case "/callback": {
state.accessToken = await state.auth.code.getToken(`${path}?${query}`);
if (state.accessToken?.accessToken) {
return 'Bot has been added to server - <a href="/auth">Add bot to another Discord server</a>';
}
return "There was an issue acquiring the access token. Check the logs.";
}
case "/interactions": {
const event = JSON.parse(body);
// verify request signature
const isVerified = verifyHeaders(body, headers);
if (!isVerified) {
return JSON.stringify({
status: 401,
body: "invalid request signature",
});
}
// type 1: Is a ping event from discord to verify the endpoint
// type 2: It's received when someone uses a slash command
// TODO: handle different types of Interaction Types
const PING = 1;
const COMMAND = 2;
switch (event.type) {
case PING: {
return JSON.stringify({
status: 200,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ type: 1 }),
});
}
case COMMAND: {
const { token, member, data, guild_id, application_id } = event;
await root.guilds.one({ id: guild_id }).onSlashCommand.$emit({
options: JSON.stringify(data.options) as any,
user: member.user.username,
token,
application_id,
});
return JSON.stringify({
status: 200,
headers: {
"Content-Type": "application/json",
},
// ACK an interaction and edit a response later, the user sees a loading state.
body: JSON.stringify({
type: 5,
}),
});
}
}
}
default:
console.log("Unknown Endpoint:", path);
}
}