@frogbotai/piece-discord 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/actions.js CHANGED
@@ -4,13 +4,33 @@ import { channelId, guildId, roleId } from './config.js';
4
4
  import { defineAction } from './define.js';
5
5
  import { discordAttachment, loadDiscordAttachment } from './files.js';
6
6
  import { channelOptions, guildOptions, roleOptions } from './options.js';
7
- const success = z.object({ success: z.boolean() });
8
- const createdChannel = success.extend({ channel: z.object({ id: z.string(), name: z.string() }) });
9
- const createdRole = success.extend({ role: z.object({ id: z.string(), name: z.string() }) });
10
- const optionsOutput = z.array(z.object({ label: z.string(), value: z.string() }));
11
- const memberInput = z.object({ guildId, userId: z.string().min(1) });
12
- const memberRoleInput = memberInput.extend({ roleId });
13
- function statusAction({ slug, description, input, method, path, options, idempotent = true, }) {
7
+ const success = z.object({
8
+ success: z.boolean()
9
+ });
10
+ const createdChannel = success.extend({
11
+ channel: z.object({
12
+ id: z.string(),
13
+ name: z.string()
14
+ })
15
+ });
16
+ const createdRole = success.extend({
17
+ role: z.object({
18
+ id: z.string(),
19
+ name: z.string()
20
+ })
21
+ });
22
+ const optionsOutput = z.array(z.object({
23
+ label: z.string(),
24
+ value: z.string()
25
+ }));
26
+ const memberInput = z.object({
27
+ guildId,
28
+ userId: z.string().min(1)
29
+ });
30
+ const memberRoleInput = memberInput.extend({
31
+ roleId
32
+ });
33
+ function statusAction({ slug, description, input, method, path, options, idempotent = true }) {
14
34
  return defineAction({
15
35
  slug,
16
36
  description,
@@ -18,20 +38,23 @@ function statusAction({ slug, description, input, method, path, options, idempot
18
38
  output: success,
19
39
  idempotent,
20
40
  options,
21
- async run({ client, input: value }) {
22
- await client.request({ method, path: path(value) });
23
- return { success: true };
24
- },
41
+ async run ({ client, input: value }) {
42
+ await client.request({
43
+ method,
44
+ path: path(value)
45
+ });
46
+ return {
47
+ success: true
48
+ };
49
+ }
25
50
  });
26
51
  }
27
- const sendMessageInput = z
28
- .object({
52
+ const sendMessageInput = z.object({
29
53
  channelId,
30
54
  message: z.string().min(1).optional(),
31
- attachments: z.array(discordAttachment).default([]),
32
- })
33
- .refine(({ message, attachments }) => message !== undefined || attachments.length > 0, {
34
- message: 'A message or attachment is required.',
55
+ attachments: z.array(discordAttachment).default([])
56
+ }).refine(({ message, attachments })=>message !== undefined || attachments.length > 0, {
57
+ message: 'A message or attachment is required.'
35
58
  });
36
59
  export const sendMessage = defineAction({
37
60
  slug: 'sendMessage',
@@ -39,28 +62,33 @@ export const sendMessage = defineAction({
39
62
  input: sendMessageInput,
40
63
  output: discordObject,
41
64
  idempotent: false,
42
- options: { channelId: channelOptions },
43
- async run({ client, input, req }) {
65
+ options: {
66
+ channelId: channelOptions
67
+ },
68
+ async run ({ client, input, req }) {
44
69
  const form = new FormData();
45
- if (input.message !== undefined)
46
- form.set('content', input.message);
47
- const attachments = await Promise.all(input.attachments.map((attachment) => loadDiscordAttachment(req, attachment)));
48
- attachments.forEach((attachment, index) => {
49
- form.append(`files[${index}]`, new Blob([attachment.data], { type: attachment.type }), attachment.name);
70
+ if (input.message !== undefined) form.set('content', input.message);
71
+ const attachments = await Promise.all(input.attachments.map((attachment)=>loadDiscordAttachment(req, attachment)));
72
+ attachments.forEach((attachment, index)=>{
73
+ form.append(`files[${index}]`, new Blob([
74
+ attachment.data
75
+ ], {
76
+ type: attachment.type
77
+ }), attachment.name);
50
78
  });
51
79
  const response = await client.request({
52
80
  method: 'POST',
53
81
  path: `/channels/${encodeURIComponent(input.channelId)}/messages`,
54
- body: form,
82
+ body: form
55
83
  });
56
84
  return discordObject.parse(response.body);
57
- },
85
+ }
58
86
  });
59
87
  const requestApprovalInput = z.object({
60
88
  channelId,
61
89
  message: z.string().min(1),
62
90
  reviewUrl: z.url(),
63
- buttonLabel: z.string().min(1).default('Review request'),
91
+ buttonLabel: z.string().min(1).default('Review request')
64
92
  });
65
93
  export const requestApproval = defineAction({
66
94
  slug: 'requestApproval',
@@ -68,8 +96,10 @@ export const requestApproval = defineAction({
68
96
  input: requestApprovalInput,
69
97
  output: discordObject,
70
98
  idempotent: false,
71
- options: { channelId: channelOptions },
72
- async run({ client, input }) {
99
+ options: {
100
+ channelId: channelOptions
101
+ },
102
+ async run ({ client, input }) {
73
103
  const response = await client.request({
74
104
  method: 'POST',
75
105
  path: `/channels/${encodeURIComponent(input.channelId)}/messages`,
@@ -78,29 +108,36 @@ export const requestApproval = defineAction({
78
108
  components: [
79
109
  {
80
110
  type: 1,
81
- components: [{ type: 2, style: 5, label: input.buttonLabel, url: input.reviewUrl }],
82
- },
83
- ],
84
- },
111
+ components: [
112
+ {
113
+ type: 2,
114
+ style: 5,
115
+ label: input.buttonLabel,
116
+ url: input.reviewUrl
117
+ }
118
+ ]
119
+ }
120
+ ]
121
+ }
85
122
  });
86
123
  return discordObject.parse(response.body);
87
- },
124
+ }
88
125
  });
89
126
  const webhookInput = z.object({
90
- webhookUrl: z.url().refine((value) => {
127
+ webhookUrl: z.url().refine((value)=>{
91
128
  const url = new URL(value);
92
- return (['discord.com', 'discordapp.com'].includes(url.hostname) &&
93
- /^\/api\/webhooks\/\d+\/[^/]+$/.test(url.pathname) &&
94
- !url.username &&
95
- !url.password);
129
+ return [
130
+ 'discord.com',
131
+ 'discordapp.com'
132
+ ].includes(url.hostname) && /^\/api\/webhooks\/\d+\/[^/]+$/.test(url.pathname) && !url.username && !url.password;
96
133
  }, {
97
- message: 'Webhook URL must be a Discord incoming webhook URL.',
134
+ message: 'Webhook URL must be a Discord incoming webhook URL.'
98
135
  }),
99
136
  username: z.string().optional(),
100
137
  content: z.string(),
101
138
  avatarUrl: z.url().optional(),
102
139
  embeds: z.array(discordObject).default([]),
103
- tts: z.boolean().default(false),
140
+ tts: z.boolean().default(false)
104
141
  });
105
142
  export const sendWebhookMessage = defineAction({
106
143
  slug: 'sendWebhookMessage',
@@ -108,7 +145,7 @@ export const sendWebhookMessage = defineAction({
108
145
  input: webhookInput,
109
146
  output: success,
110
147
  idempotent: false,
111
- async run({ client, input }) {
148
+ async run ({ client, input }) {
112
149
  await client.request({
113
150
  method: 'POST',
114
151
  path: input.webhookUrl,
@@ -118,40 +155,50 @@ export const sendWebhookMessage = defineAction({
118
155
  content: input.content,
119
156
  avatar_url: input.avatarUrl,
120
157
  embeds: input.embeds,
121
- tts: input.tts,
122
- },
158
+ tts: input.tts
159
+ }
123
160
  });
124
- return { success: true };
125
- },
161
+ return {
162
+ success: true
163
+ };
164
+ }
126
165
  });
127
166
  export const addRoleToMember = statusAction({
128
167
  slug: 'addRoleToMember',
129
168
  description: 'Add a role to a guild member.',
130
169
  input: memberRoleInput,
131
170
  method: 'PUT',
132
- path: ({ guildId, userId, roleId }) => `/guilds/${encodeURIComponent(guildId)}/members/${encodeURIComponent(userId)}/roles/${encodeURIComponent(roleId)}`,
133
- options: { guildId: guildOptions, roleId: roleOptions },
171
+ path: ({ guildId, userId, roleId })=>`/guilds/${encodeURIComponent(guildId)}/members/${encodeURIComponent(userId)}/roles/${encodeURIComponent(roleId)}`,
172
+ options: {
173
+ guildId: guildOptions,
174
+ roleId: roleOptions
175
+ }
134
176
  });
135
177
  export const removeRoleFromMember = statusAction({
136
178
  slug: 'removeRoleFromMember',
137
179
  description: 'Remove a role from a guild member.',
138
180
  input: memberRoleInput,
139
181
  method: 'DELETE',
140
- path: ({ guildId, userId, roleId }) => `/guilds/${encodeURIComponent(guildId)}/members/${encodeURIComponent(userId)}/roles/${encodeURIComponent(roleId)}`,
141
- options: { guildId: guildOptions, roleId: roleOptions },
182
+ path: ({ guildId, userId, roleId })=>`/guilds/${encodeURIComponent(guildId)}/members/${encodeURIComponent(userId)}/roles/${encodeURIComponent(roleId)}`,
183
+ options: {
184
+ guildId: guildOptions,
185
+ roleId: roleOptions
186
+ }
142
187
  });
143
188
  export const removeMember = statusAction({
144
189
  slug: 'removeMember',
145
190
  description: 'Remove a member from a guild.',
146
191
  input: memberInput,
147
192
  method: 'DELETE',
148
- path: ({ guildId, userId }) => `/guilds/${encodeURIComponent(guildId)}/members/${encodeURIComponent(userId)}`,
149
- options: { guildId: guildOptions },
193
+ path: ({ guildId, userId })=>`/guilds/${encodeURIComponent(guildId)}/members/${encodeURIComponent(userId)}`,
194
+ options: {
195
+ guildId: guildOptions
196
+ }
150
197
  });
151
198
  const listMembersInput = z.object({
152
199
  guildId,
153
200
  search: z.string(),
154
- limit: z.number().int().min(1).max(1000).default(1000),
201
+ limit: z.number().int().min(1).max(1000).default(1000)
155
202
  });
156
203
  export const listMembers = defineAction({
157
204
  slug: 'listMembers',
@@ -159,60 +206,67 @@ export const listMembers = defineAction({
159
206
  input: listMembersInput,
160
207
  output: optionsOutput,
161
208
  idempotent: true,
162
- options: { guildId: guildOptions },
163
- async run({ client, input }) {
209
+ options: {
210
+ guildId: guildOptions
211
+ },
212
+ async run ({ client, input }) {
164
213
  const members = [];
165
214
  let after;
166
215
  do {
167
- const query = new URLSearchParams({ limit: String(input.limit) });
168
- if (after)
169
- query.set('after', after);
216
+ const query = new URLSearchParams({
217
+ limit: String(input.limit)
218
+ });
219
+ if (after) query.set('after', after);
170
220
  const response = await client.request({
171
- path: `/guilds/${encodeURIComponent(input.guildId)}/members?${query}`,
221
+ path: `/guilds/${encodeURIComponent(input.guildId)}/members?${query}`
172
222
  });
173
223
  const page = Array.isArray(response.body) ? response.body : [];
174
224
  members.push(...page);
175
- after =
176
- page.length === input.limit
177
- ? String(page.at(-1)?.user?.id ?? '') ||
178
- undefined
179
- : undefined;
180
- } while (after);
181
- return members.flatMap((member) => {
182
- if (!member || typeof member !== 'object' || !('user' in member))
183
- return [];
225
+ after = page.length === input.limit ? String(page.at(-1)?.user?.id ?? '') || undefined : undefined;
226
+ }while (after)
227
+ return members.flatMap((member)=>{
228
+ if (!member || typeof member !== 'object' || !('user' in member)) return [];
184
229
  const user = member.user;
185
- if (!user || typeof user !== 'object' || !('id' in user) || !('username' in user))
186
- return [];
187
- if (typeof user.id !== 'string' || typeof user.username !== 'string')
188
- return [];
189
- if (!user.username.toLowerCase().includes(input.search.toLowerCase()))
190
- return [];
191
- return [{ label: user.username, value: user.id }];
230
+ if (!user || typeof user !== 'object' || !('id' in user) || !('username' in user)) return [];
231
+ if (typeof user.id !== 'string' || typeof user.username !== 'string') return [];
232
+ if (!user.username.toLowerCase().includes(input.search.toLowerCase())) return [];
233
+ return [
234
+ {
235
+ label: user.username,
236
+ value: user.id
237
+ }
238
+ ];
192
239
  });
193
- },
240
+ }
241
+ });
242
+ const renameChannelInput = z.object({
243
+ channelId,
244
+ name: z.string().min(1)
194
245
  });
195
- const renameChannelInput = z.object({ channelId, name: z.string().min(1) });
196
246
  export const renameChannel = defineAction({
197
247
  slug: 'renameChannel',
198
248
  description: 'Rename a Discord channel.',
199
249
  input: renameChannelInput,
200
250
  output: discordObject,
201
251
  idempotent: true,
202
- options: { channelId: channelOptions },
203
- async run({ client, input }) {
252
+ options: {
253
+ channelId: channelOptions
254
+ },
255
+ async run ({ client, input }) {
204
256
  const response = await client.request({
205
257
  method: 'PATCH',
206
258
  path: `/channels/${encodeURIComponent(input.channelId)}`,
207
- body: { name: input.name },
259
+ body: {
260
+ name: input.name
261
+ }
208
262
  });
209
263
  return discordObject.parse(response.body);
210
- },
264
+ }
211
265
  });
212
266
  const createChannelInput = z.object({
213
267
  guildId,
214
268
  name: z.string().min(1),
215
- topic: z.string().optional(),
269
+ topic: z.string().optional()
216
270
  });
217
271
  export const createChannel = defineAction({
218
272
  slug: 'createChannel',
@@ -220,55 +274,81 @@ export const createChannel = defineAction({
220
274
  input: createChannelInput,
221
275
  output: createdChannel,
222
276
  idempotent: false,
223
- options: { guildId: guildOptions },
224
- async run({ client, input }) {
277
+ options: {
278
+ guildId: guildOptions
279
+ },
280
+ async run ({ client, input }) {
225
281
  const response = await client.request({
226
282
  method: 'POST',
227
283
  path: `/guilds/${encodeURIComponent(input.guildId)}/channels`,
228
- body: { name: input.name, topic: input.topic },
284
+ body: {
285
+ name: input.name,
286
+ topic: input.topic
287
+ }
229
288
  });
230
- const channel = z.object({ id: z.string(), name: z.string() }).parse(response.body);
231
- return { success: true, channel };
232
- },
289
+ const channel = z.object({
290
+ id: z.string(),
291
+ name: z.string()
292
+ }).parse(response.body);
293
+ return {
294
+ success: true,
295
+ channel
296
+ };
297
+ }
298
+ });
299
+ const deleteChannelInput = z.object({
300
+ channelId
233
301
  });
234
- const deleteChannelInput = z.object({ channelId });
235
302
  export const deleteChannel = defineAction({
236
303
  slug: 'deleteChannel',
237
304
  description: 'Permanently delete a Discord channel.',
238
305
  input: deleteChannelInput,
239
306
  output: discordObject,
240
307
  idempotent: true,
241
- options: { channelId: channelOptions },
242
- async run({ client, input }) {
308
+ options: {
309
+ channelId: channelOptions
310
+ },
311
+ async run ({ client, input }) {
243
312
  const response = await client.request({
244
313
  method: 'DELETE',
245
- path: `/channels/${encodeURIComponent(input.channelId)}`,
314
+ path: `/channels/${encodeURIComponent(input.channelId)}`
246
315
  });
247
316
  return discordObject.parse(response.body);
248
- },
317
+ }
318
+ });
319
+ const findChannelInput = z.object({
320
+ guildId,
321
+ name: z.string().min(1)
322
+ });
323
+ const findChannelOutput = z.object({
324
+ success: z.boolean(),
325
+ channelId: z.string().optional()
249
326
  });
250
- const findChannelInput = z.object({ guildId, name: z.string().min(1) });
251
- const findChannelOutput = z.object({ success: z.boolean(), channelId: z.string().optional() });
252
327
  export const findChannel = defineAction({
253
328
  slug: 'findChannel',
254
329
  description: 'Find a guild channel by exact name.',
255
330
  input: findChannelInput,
256
331
  output: findChannelOutput,
257
332
  idempotent: true,
258
- options: { guildId: guildOptions },
259
- async run({ client, input }) {
333
+ options: {
334
+ guildId: guildOptions
335
+ },
336
+ async run ({ client, input }) {
260
337
  const response = await client.request({
261
- path: `/guilds/${encodeURIComponent(input.guildId)}/channels`,
338
+ path: `/guilds/${encodeURIComponent(input.guildId)}/channels`
262
339
  });
263
340
  const channels = Array.isArray(response.body) ? response.body : [];
264
- const channel = channels.find((value) => value && typeof value === 'object' && value.name === input.name);
265
- const id = channel && typeof channel === 'object' && typeof channel.id === 'string'
266
- ? channel.id
267
- : undefined;
268
- return { success: id !== undefined, channelId: id };
269
- },
341
+ const channel = channels.find((value)=>value && typeof value === 'object' && value.name === input.name);
342
+ const id = channel && typeof channel === 'object' && typeof channel.id === 'string' ? channel.id : undefined;
343
+ return {
344
+ success: id !== undefined,
345
+ channelId: id
346
+ };
347
+ }
348
+ });
349
+ const moderationInput = memberInput.extend({
350
+ reason: z.string().optional()
270
351
  });
271
- const moderationInput = memberInput.extend({ reason: z.string().optional() });
272
352
  function moderationAction(method, slug, resource) {
273
353
  return defineAction({
274
354
  slug,
@@ -276,15 +356,21 @@ function moderationAction(method, slug, resource) {
276
356
  input: moderationInput,
277
357
  output: success,
278
358
  idempotent: true,
279
- options: { guildId: guildOptions },
280
- async run({ client, input }) {
359
+ options: {
360
+ guildId: guildOptions
361
+ },
362
+ async run ({ client, input }) {
281
363
  await client.request({
282
364
  method,
283
365
  path: `/guilds/${encodeURIComponent(input.guildId)}/${resource}/${encodeURIComponent(input.userId)}`,
284
- headers: input.reason ? { 'X-Audit-Log-Reason': input.reason } : undefined,
366
+ headers: input.reason ? {
367
+ 'X-Audit-Log-Reason': input.reason
368
+ } : undefined
285
369
  });
286
- return { success: true };
287
- },
370
+ return {
371
+ success: true
372
+ };
373
+ }
288
374
  });
289
375
  }
290
376
  export const unbanMember = moderationAction('DELETE', 'unbanMember', 'bans');
@@ -295,7 +381,7 @@ const createRoleInput = z.object({
295
381
  color: z.coerce.number().int().min(0).max(0xffffff).optional(),
296
382
  displaySeparately: z.boolean().optional(),
297
383
  mentionable: z.boolean().optional(),
298
- reason: z.string().optional(),
384
+ reason: z.string().optional()
299
385
  });
300
386
  export const createRole = defineAction({
301
387
  slug: 'createRole',
@@ -303,46 +389,77 @@ export const createRole = defineAction({
303
389
  input: createRoleInput,
304
390
  output: createdRole,
305
391
  idempotent: false,
306
- options: { guildId: guildOptions },
307
- async run({ client, input }) {
392
+ options: {
393
+ guildId: guildOptions
394
+ },
395
+ async run ({ client, input }) {
308
396
  const response = await client.request({
309
397
  method: 'POST',
310
398
  path: `/guilds/${encodeURIComponent(input.guildId)}/roles`,
311
- headers: input.reason ? { 'X-Audit-Log-Reason': input.reason } : undefined,
399
+ headers: input.reason ? {
400
+ 'X-Audit-Log-Reason': input.reason
401
+ } : undefined,
312
402
  body: {
313
403
  name: input.name,
314
404
  color: input.color,
315
405
  hoist: input.displaySeparately,
316
- mentionable: input.mentionable,
317
- },
406
+ mentionable: input.mentionable
407
+ }
318
408
  });
319
- const role = z.object({ id: z.string(), name: z.string() }).parse(response.body);
320
- return { success: true, role };
321
- },
409
+ const role = z.object({
410
+ id: z.string(),
411
+ name: z.string()
412
+ }).parse(response.body);
413
+ return {
414
+ success: true,
415
+ role
416
+ };
417
+ }
418
+ });
419
+ const deleteRoleInput = z.object({
420
+ guildId,
421
+ roleId,
422
+ reason: z.string().optional()
322
423
  });
323
- const deleteRoleInput = z.object({ guildId, roleId, reason: z.string().optional() });
324
424
  export const deleteRole = defineAction({
325
425
  slug: 'deleteRole',
326
426
  description: 'Delete a role from a guild.',
327
427
  input: deleteRoleInput,
328
428
  output: success,
329
429
  idempotent: true,
330
- options: { guildId: guildOptions, roleId: roleOptions },
331
- async run({ client, input }) {
430
+ options: {
431
+ guildId: guildOptions,
432
+ roleId: roleOptions
433
+ },
434
+ async run ({ client, input }) {
332
435
  await client.request({
333
436
  method: 'DELETE',
334
437
  path: `/guilds/${encodeURIComponent(input.guildId)}/roles/${encodeURIComponent(input.roleId)}`,
335
- headers: input.reason ? { 'X-Audit-Log-Reason': input.reason } : undefined,
438
+ headers: input.reason ? {
439
+ 'X-Audit-Log-Reason': input.reason
440
+ } : undefined
336
441
  });
337
- return { success: true };
338
- },
442
+ return {
443
+ success: true
444
+ };
445
+ }
339
446
  });
340
447
  const customApiInput = z.object({
341
448
  path: z.string().regex(/^\/(?!\/)/, 'Path must be relative to the Discord API.'),
342
- method: z.enum(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']),
449
+ method: z.enum([
450
+ 'GET',
451
+ 'POST',
452
+ 'PUT',
453
+ 'PATCH',
454
+ 'DELETE'
455
+ ]),
343
456
  headers: z.record(z.string(), z.string()).default({}),
344
- queryParams: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).default({}),
345
- body: z.unknown().optional(),
457
+ queryParams: z.record(z.string(), z.union([
458
+ z.string(),
459
+ z.number(),
460
+ z.boolean()
461
+ ])).default({}),
462
+ body: z.unknown().optional()
346
463
  });
347
464
  export const sendApiRequest = defineAction({
348
465
  slug: 'sendApiRequest',
@@ -350,17 +467,18 @@ export const sendApiRequest = defineAction({
350
467
  input: customApiInput,
351
468
  output: discordResponse,
352
469
  idempotent: false,
353
- async run({ client, input }) {
470
+ async run ({ client, input }) {
354
471
  const url = new URL(`https://discord.com${input.path}`);
355
- for (const [key, value] of Object.entries(input.queryParams)) {
472
+ for (const [key, value] of Object.entries(input.queryParams)){
356
473
  url.searchParams.set(key, String(value));
357
474
  }
358
475
  return client.request({
359
476
  method: input.method,
360
477
  path: `${url.pathname}${url.search}`,
361
478
  headers: input.headers,
362
- body: input.body,
479
+ body: input.body
363
480
  });
364
- },
481
+ }
365
482
  });
483
+
366
484
  //# sourceMappingURL=actions.js.map