@nerimity/nerimity.js 1.9.0 → 1.11.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.
Files changed (41) hide show
  1. package/.eslintrc.js +19 -37
  2. package/build/Client.d.ts +58 -1
  3. package/build/Client.d.ts.map +1 -1
  4. package/build/Client.js +80 -2
  5. package/build/Client.js.map +1 -1
  6. package/build/EventNames.d.ts +2 -0
  7. package/build/EventNames.d.ts.map +1 -1
  8. package/build/EventNames.js +1 -0
  9. package/build/EventNames.js.map +1 -1
  10. package/build/RawData.d.ts +37 -0
  11. package/build/RawData.d.ts.map +1 -1
  12. package/build/RawData.js.map +1 -1
  13. package/build/index.d.ts +1 -1
  14. package/build/index.d.ts.map +1 -1
  15. package/build/index.js.map +1 -1
  16. package/build/services/ApplicationService.d.ts +3 -0
  17. package/build/services/ApplicationService.d.ts.map +1 -0
  18. package/build/services/ApplicationService.js +16 -0
  19. package/build/services/ApplicationService.js.map +1 -0
  20. package/build/services/MessageService.d.ts +2 -0
  21. package/build/services/MessageService.d.ts.map +1 -1
  22. package/build/services/MessageService.js +1 -1
  23. package/build/services/MessageService.js.map +1 -1
  24. package/build/services/PostsService.d.ts +26 -0
  25. package/build/services/PostsService.d.ts.map +1 -0
  26. package/build/services/PostsService.js +52 -0
  27. package/build/services/PostsService.js.map +1 -0
  28. package/build/services/serviceEndpoints.d.ts +5 -0
  29. package/build/services/serviceEndpoints.d.ts.map +1 -1
  30. package/build/services/serviceEndpoints.js +15 -4
  31. package/build/services/serviceEndpoints.js.map +1 -1
  32. package/examples/SlashCommands.js +31 -0
  33. package/package.json +3 -2
  34. package/src/Client.ts +127 -1
  35. package/src/EventNames.ts +2 -0
  36. package/src/RawData.ts +29 -3
  37. package/src/index.ts +1 -1
  38. package/src/services/ApplicationService.ts +16 -0
  39. package/src/services/MessageService.ts +3 -1
  40. package/src/services/PostsService.ts +71 -0
  41. package/src/services/serviceEndpoints.ts +33 -13
@@ -0,0 +1,26 @@
1
+ import { Client } from '../Client';
2
+ import { RawPost } from '../RawData';
3
+ interface PostPostOpts {
4
+ client: Client;
5
+ postId?: string;
6
+ content: string;
7
+ nerimityCdnFileId?: string;
8
+ poll?: {
9
+ choices: string[];
10
+ };
11
+ }
12
+ interface EditPostOpts {
13
+ client: Client;
14
+ postId: string;
15
+ content: string;
16
+ }
17
+ interface DeletePostOpts {
18
+ client: Client;
19
+ postId: string;
20
+ }
21
+ export declare function getPosts(client: Client): Promise<RawPost[]>;
22
+ export declare function postPost(opts: PostPostOpts): Promise<RawPost>;
23
+ export declare function editPost(opts: EditPostOpts): Promise<RawPost>;
24
+ export declare function deletePost(opts: DeletePostOpts): Promise<unknown>;
25
+ export {};
26
+ //# sourceMappingURL=PostsService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostsService.d.ts","sourceRoot":"","sources":["../../src/services/PostsService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAIrC,UAAU,YAAY;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,IAAI,CAAC,EAAE;QACH,OAAO,EAAE,MAAM,EAAE,CAAC;KACrB,CAAC;CACL;AAED,UAAU,YAAY;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACnB;AAED,UAAU,cAAc;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAClB;AAED,wBAAsB,QAAQ,CAAC,MAAM,EAAE,MAAM,sBAS5C;AAED,wBAAsB,QAAQ,CAAC,IAAI,EAAE,YAAY,oBAUhD;AAED,wBAAsB,QAAQ,CAAC,IAAI,EAAE,YAAY,oBAUhD;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,cAAc,oBASpD"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deletePost = exports.editPost = exports.postPost = exports.getPosts = void 0;
4
+ const MessageService_1 = require("./MessageService");
5
+ const serviceEndpoints_1 = require("./serviceEndpoints");
6
+ async function getPosts(client) {
7
+ return await (0, MessageService_1.request)({
8
+ client: client,
9
+ url: serviceEndpoints_1.ServiceEndpoints.GetPosts(),
10
+ method: 'GET',
11
+ useToken: true,
12
+ }).catch(err => {
13
+ throw new Error(`Failed to get posts. ${err.message}`);
14
+ });
15
+ }
16
+ exports.getPosts = getPosts;
17
+ async function postPost(opts) {
18
+ return await (0, MessageService_1.request)({
19
+ client: opts.client,
20
+ url: serviceEndpoints_1.ServiceEndpoints.PostPost(),
21
+ method: 'POST',
22
+ body: { content: opts.content, nerimityCdnFileId: opts.nerimityCdnFileId, poll: opts.poll },
23
+ useToken: true,
24
+ }).catch(err => {
25
+ throw new Error(`Failed to send post. ${err.message}`);
26
+ });
27
+ }
28
+ exports.postPost = postPost;
29
+ async function editPost(opts) {
30
+ return await (0, MessageService_1.request)({
31
+ client: opts.client,
32
+ url: serviceEndpoints_1.ServiceEndpoints.EditPost(opts.postId),
33
+ method: 'PATCH',
34
+ body: { content: opts.content },
35
+ useToken: true,
36
+ }).catch(err => {
37
+ throw new Error(`Failed to edit post. ${err.message}`);
38
+ });
39
+ }
40
+ exports.editPost = editPost;
41
+ async function deletePost(opts) {
42
+ return await (0, MessageService_1.request)({
43
+ client: opts.client,
44
+ url: serviceEndpoints_1.ServiceEndpoints.DeletePost(opts.postId),
45
+ method: 'DELETE',
46
+ useToken: true,
47
+ }).catch(err => {
48
+ throw new Error(`Failed to delete post. ${err.message}`);
49
+ });
50
+ }
51
+ exports.deletePost = deletePost;
52
+ //# sourceMappingURL=PostsService.js.map
@@ -0,0 +1 @@
1
+ {"version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OAAO,EAAC;QAC7B,QAAQ,EAAE,IAAI;KACjB,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;QACX,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAA;AACN,CAAC;AAVD,4BAUC;AAEM,KAAK,UAAU,UAAU,CAAC,IAAoB;IACjD,OAAO,MAAM,IAAA,wBAAO,EAAC;QACjB,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,GAAG,EAAE,mCAAgB,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC;QAC7C,MAAM,EAAE,QAAQ;QAChB,QAAQ,EAAE,IAAI;KACjB,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;QACX,MAAM,IAAI,KAAK,CAAC,0BAA0B,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;AACP,CAAC;AATD,gCASC"}
@@ -4,6 +4,11 @@ export declare const ServiceEndpoints: {
4
4
  GetMessages: (channelId: string) => string;
5
5
  PostMessage: (channelId: string) => string;
6
6
  EditMessage: (channelId: string, messageId: string) => string;
7
+ GetPosts: () => string;
8
+ PostPost: () => string;
9
+ EditPost: (postId: string) => string;
10
+ DeletePost: (postId: string) => string;
11
+ BotCommands: () => string;
7
12
  ButtonClickCallback: (channelId: string, messageId: string, buttonId: string) => string;
8
13
  };
9
14
  //# sourceMappingURL=serviceEndpoints.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"serviceEndpoints.d.ts","sourceRoot":"","sources":["../../src/services/serviceEndpoints.ts"],"names":[],"mappings":"AACA,eAAO,IAAI,IAAI,QAAyB,CAAC;AAGzC,eAAO,MAAM,UAAU,YAAa,MAAM,SAGzC,CAAC;AAQF,eAAO,MAAM,gBAAgB;6BAPG,MAAM;6BACN,MAAM;6BACN,MAAM,aAAa,MAAM;qCAGjB,MAAM,aAAa,MAAM,YAAY,MAAM;CAOlF,CAAC"}
1
+ {"version":3,"file":"serviceEndpoints.d.ts","sourceRoot":"","sources":["../../src/services/serviceEndpoints.ts"],"names":[],"mappings":"AACA,eAAO,IAAI,IAAI,QAAyB,CAAC;AAGzC,eAAO,MAAM,UAAU,YAAa,MAAM,SAGzC,CAAC;AAuBF,eAAO,MAAM,gBAAgB;6BAtBG,MAAM;6BAEN,MAAM;6BAEN,MAAM,aAAa,MAAM;;;uBAM/B,MAAM;yBACJ,MAAM;;qCAKrB,MAAM,aACN,MAAM,YACP,MAAM;CAcjB,CAAC"}
@@ -2,21 +2,32 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ServiceEndpoints = exports.updatePath = exports.path = void 0;
4
4
  // export const path = 'http://localhost:8080';
5
- exports.path = 'https://nerimity.com';
6
- let BaseUrl = exports.path + '/api';
5
+ exports.path = "https://nerimity.com";
6
+ let BaseUrl = exports.path + "/api";
7
7
  const updatePath = (newPath) => {
8
8
  exports.path = newPath;
9
- BaseUrl = exports.path + '/api';
9
+ BaseUrl = exports.path + "/api";
10
10
  };
11
11
  exports.updatePath = updatePath;
12
12
  const GetMessages = (channelId) => `${BaseUrl}/channels/${channelId}/messages`;
13
13
  const PostMessage = (channelId) => `${BaseUrl}/channels/${channelId}/messages`;
14
14
  const EditMessage = (channelId, messageId) => `${BaseUrl}/channels/${channelId}/messages/${messageId}`;
15
+ // Posts
16
+ const GetPosts = () => `${BaseUrl}/posts`;
17
+ const PostPost = () => `${BaseUrl}/posts`;
18
+ const EditPost = (postId) => `${BaseUrl}/posts/${postId}`;
19
+ const DeletePost = (postId) => `${BaseUrl}/posts/${postId}`;
20
+ const BotCommands = () => `${BaseUrl}/applications/bot/commands`;
15
21
  const ButtonClickCallback = (channelId, messageId, buttonId) => `${BaseUrl}/channels/${channelId}/messages/${messageId}/buttons/${buttonId}/callback`;
16
22
  exports.ServiceEndpoints = {
17
23
  GetMessages,
18
24
  PostMessage,
19
25
  EditMessage,
20
- ButtonClickCallback
26
+ GetPosts,
27
+ PostPost,
28
+ EditPost,
29
+ DeletePost,
30
+ BotCommands,
31
+ ButtonClickCallback,
21
32
  };
22
33
  //# sourceMappingURL=serviceEndpoints.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"serviceEndpoints.js","sourceRoot":"","sources":["../../src/services/serviceEndpoints.ts"],"names":[],"mappings":";;;AAAA,+CAA+C;AACpC,QAAA,IAAI,GAAG,sBAAsB,CAAC;AACzC,IAAI,OAAO,GAAG,YAAI,GAAG,MAAM,CAAC;AAErB,MAAM,UAAU,GAAG,CAAC,OAAe,EAAE,EAAE;IAC1C,YAAI,GAAG,OAAO,CAAC;IACf,OAAO,GAAG,YAAI,GAAG,MAAM,CAAC;AAC5B,CAAC,CAAC;AAHW,QAAA,UAAU,cAGrB;AACF,MAAM,WAAW,GAAG,CAAC,SAAiB,EAAE,EAAE,CAAC,GAAG,OAAO,aAAa,SAAS,WAAW,CAAC;AACvF,MAAM,WAAW,GAAG,CAAC,SAAiB,EAAE,EAAE,CAAC,GAAG,OAAO,aAAa,SAAS,WAAW,CAAC;AACvF,MAAM,WAAW,GAAG,CAAC,SAAiB,EAAE,SAAiB,EAAE,EAAE,CAAC,GAAG,OAAO,aAAa,SAAS,aAAa,SAAS,EAAE,CAAC;AAGvH,MAAM,mBAAmB,GAAG,CAAC,SAAiB,EAAE,SAAiB,EAAE,QAAgB,EAAE,EAAE,CAAC,GAAG,OAAO,aAAa,SAAS,aAAa,SAAS,YAAY,QAAQ,WAAW,CAAC;AAEjK,QAAA,gBAAgB,GAAG;IAC5B,WAAW;IACX,WAAW;IACX,WAAW;IACX,mBAAmB;CACtB,CAAC"}
1
+ {"version":3,"file":"serviceEndpoints.js","sourceRoot":"","sources":["../../src/services/serviceEndpoints.ts"],"names":[],"mappings":";;;AAAA,+CAA+C;AACpC,QAAA,IAAI,GAAG,sBAAsB,CAAC;AACzC,IAAI,OAAO,GAAG,YAAI,GAAG,MAAM,CAAC;AAErB,MAAM,UAAU,GA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new Client();
8
+
9
+ client.on(Events.Ready, () => {
10
+ console.log(`Connected as ${client.user?.username}!`);
11
+ });
12
+
13
+
14
+ // this function only to be run when updating command list. dont run every time the bot is running.
15
+ client.updateCommands("token", [
16
+ {name: "help", description: "Shows the help list", args: "<page number>"},
17
+ ]).then(console.log).catch(console.error);
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+ client.on(Events.MessageCreate, async (message) => {
26
+ if (message.command.name === "help") {
27
+ message.reply("Help page " + (message.command.args[0] || "1"));
28
+ }
29
+
30
+ })
31
+ client.login("token")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nerimity/nerimity.js",
3
- "version": "1.9.0",
3
+ "version": "1.11.0",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "keywords": [],
@@ -31,6 +31,7 @@
31
31
  },
32
32
  "homepage": "https://github.com/Nerimity/nerimity.js#readme",
33
33
  "scripts": {
34
- "build": "tsc"
34
+ "build": "tsc",
35
+ "dev": "tsc && node tests/test.js"
35
36
  }
36
37
  }
package/src/Client.ts CHANGED
@@ -11,9 +11,11 @@ import {
11
11
  ChannelType,
12
12
  MessageButtonClickPayload,
13
13
  MessageType,
14
+ RawBotCommand,
14
15
  RawChannel,
15
16
  RawMessage,
16
17
  RawMessageButton,
18
+ RawPost,
17
19
  RawServer,
18
20
  RawServerMember,
19
21
  RawUser,
@@ -25,6 +27,13 @@ import {
25
27
  postMessage,
26
28
  } from "./services/MessageService";
27
29
  import { path, updatePath } from "./services/serviceEndpoints";
30
+ import {
31
+ deletePost,
32
+ editPost,
33
+ getPosts,
34
+ postPost,
35
+ } from "./services/PostsService";
36
+ import { updateCommands as postUpdateCommands } from "./services/ApplicationService";
28
37
 
29
38
  export const Events = ClientEvents;
30
39
 
@@ -35,6 +44,7 @@ export class Client extends EventEmitter<ClientEventMap> {
35
44
  users: Users;
36
45
  channels: Channels;
37
46
  servers: Servers;
47
+ posts: Posts;
38
48
 
39
49
  constructor(opts?: { urlOverride?: string }) {
40
50
  super();
@@ -48,9 +58,13 @@ export class Client extends EventEmitter<ClientEventMap> {
48
58
  this.channels = new Channels(this);
49
59
  this.users = new Users(this);
50
60
  this.servers = new Servers(this);
61
+ this.posts = new Posts(this);
51
62
  new EventHandlers(this);
52
63
  }
53
64
 
65
+ updateCommands(token: string, commands: Omit<RawBotCommand, "botUserId">[]) {
66
+ return postUpdateCommands(token, commands);
67
+ }
54
68
  public login(token: string) {
55
69
  this.token = token;
56
70
  this.socket.connect();
@@ -333,6 +347,15 @@ export interface MessageOpts {
333
347
  nerimityCdnFileId?: string;
334
348
  buttons?: RawMessageButton[];
335
349
  silent?: boolean;
350
+ replyToMessageIds?: string[];
351
+ mentionReplies?: boolean;
352
+ }
353
+
354
+ export interface PostOpts {
355
+ nerimityCdnFileId?: string;
356
+ poll?: {
357
+ choices: string[];
358
+ };
336
359
  }
337
360
 
338
361
  export class Channel {
@@ -342,12 +365,15 @@ export class Channel {
342
365
  type: ChannelType;
343
366
  createdAt?: number;
344
367
  lastMessagedAt?: number;
368
+ server?: Server;
345
369
  constructor(client: Client, channel: RawChannel) {
346
370
  this.client = client;
347
371
  this.id = channel.id;
348
372
  this.type = channel.type;
349
373
  this.createdAt = channel.createdAt;
350
374
  this.lastMessagedAt = channel.lastMessagedAt;
375
+ if (channel.serverId)
376
+ this.server = this.client.servers.cache.get(channel.serverId)!;
351
377
  }
352
378
 
353
379
  async send(content: string, opts?: MessageOpts) {
@@ -359,6 +385,8 @@ export class Channel {
359
385
  nerimityCdnFileId: opts?.nerimityCdnFileId,
360
386
  htmlEmbed: opts?.htmlEmbed,
361
387
  buttons: opts?.buttons,
388
+ replyToMessageIds: opts?.replyToMessageIds,
389
+ mentionReplies: opts?.mentionReplies,
362
390
  });
363
391
  const message = new Message(this.client, RawMessage);
364
392
  return message;
@@ -398,6 +426,7 @@ export class ServerChannel extends Channel {
398
426
  }
399
427
 
400
428
  const UserMentionRegex = /\[@:([0-9]+)\]/gm;
429
+ const CommandRegex = /^(\/[^:\s]*):(\d+)( .*)?$/m;
401
430
 
402
431
  export class Message {
403
432
  client: Client;
@@ -409,6 +438,7 @@ export class Message {
409
438
  channel: AllChannel;
410
439
  user: User;
411
440
  mentions: User[] = [];
441
+ command?: { name: string; args: string[] };
412
442
  constructor(client: Client, message: RawMessage) {
413
443
  this.client = client;
414
444
 
@@ -420,6 +450,14 @@ export class Message {
420
450
  this.createdAt = message.createdAt;
421
451
  this.user = this.client.users.cache.get(message.createdBy.id)!;
422
452
 
453
+ const cmd = message.content?.match(CommandRegex);
454
+ if (cmd?.[2] === this.client.user?.id) {
455
+ this.command = {
456
+ name: cmd?.[1]!.substring(1)!,
457
+ args: message.content!.split(" ").slice(1),
458
+ };
459
+ }
460
+
423
461
  if (!this.user) {
424
462
  this.user = this.client.users.setCache(message.createdBy);
425
463
  }
@@ -441,7 +479,9 @@ export class Message {
441
479
  }
442
480
  }
443
481
  reply(content: string, opts?: MessageOpts) {
444
- return this.channel.send(`${this.user} ${content}`, opts);
482
+ let fOpts: MessageOpts = opts || {};
483
+ fOpts.replyToMessageIds = [this.id];
484
+ return this.channel.send(content, opts);
445
485
  }
446
486
  async edit(content: string) {
447
487
  const RawMessage = await editMessage({
@@ -465,6 +505,92 @@ export class Message {
465
505
  }
466
506
  }
467
507
 
508
+ class Posts {
509
+ client: Client;
510
+ constructor(client: Client) {
511
+ this.client = client;
512
+ }
513
+
514
+ async get(id?: string) {
515
+ const RawPosts = await getPosts(this.client);
516
+ const posts = RawPosts.map((post) => new Post(this.client, post));
517
+ return id ? posts.find((p) => p.id === id) : posts;
518
+ }
519
+
520
+ async create(content: string, opts?: PostOpts) {
521
+ const RawPost = await postPost({
522
+ client: this.client,
523
+ content: content,
524
+ nerimityCdnFileId: opts?.nerimityCdnFileId,
525
+ poll: opts?.poll,
526
+ });
527
+
528
+ const post = new Post(this.client, RawPost);
529
+
530
+ return post;
531
+ }
532
+ }
533
+
534
+ export class Post {
535
+ client: Client;
536
+ id: string;
537
+ content?: string;
538
+ attachments?: Array<any>;
539
+ deleted: boolean;
540
+ block?: boolean;
541
+ commentToId: string;
542
+ commentTo?: RawPost;
543
+ createdBy: RawUser;
544
+ createdAt: number;
545
+ editedAt: number;
546
+ likedBy: { id: string }[]; // if you liked this post, array will not be empty
547
+ reposts: { id: string; createdBy: { id: string; username: string } }[];
548
+ repost?: RawPost;
549
+ _count: { likedBy: number; comments: number; reposts: number };
550
+ views: number;
551
+ announcement: any;
552
+ poll?: any;
553
+
554
+ constructor(client: Client, post: RawPost) {
555
+ this.client = client;
556
+ this.id = post.id;
557
+ this.content = post.content;
558
+ this.attachments = post.attachments;
559
+ this.deleted = post.deleted;
560
+ this.block = post.block;
561
+ this.commentToId = post.commentToId;
562
+ this.commentTo = post.commentTo;
563
+ this.createdBy = post.createdBy;
564
+ this.createdAt = post.createdAt;
565
+ this.editedAt = post.editedAt;
566
+ this.likedBy = post.likedBy;
567
+ this.reposts = post.reposts;
568
+ this.repost = post.repost;
569
+ this._count = post._count;
570
+ this.views = post.views;
571
+ this.announcement = post.announcement;
572
+ this.poll = post.poll;
573
+ }
574
+
575
+ async edit(content: string) {
576
+ const RawPost = await editPost({
577
+ client: this.client,
578
+ content: content,
579
+ postId: this.id,
580
+ });
581
+
582
+ const post = new Post(this.client, RawPost);
583
+ return post;
584
+ }
585
+
586
+ async delete() {
587
+ await deletePost({
588
+ client: this.client,
589
+ postId: this.id,
590
+ });
591
+ }
592
+ }
593
+
468
594
  class User {
469
595
  client: Client;
470
596
  id: string;
package/src/EventNames.ts CHANGED
@@ -3,6 +3,7 @@ import { Button, Message, Server, ServerChannel, ServerMember } from './Client';
3
3
  export const ClientEvents = {
4
4
  Ready: 'ready',
5
5
  MessageCreate: 'messageCreate',
6
+ MessageUpdate: 'messageUpdate',
6
7
  ServerMemberLeft: 'serverMemberLeft',
7
8
  ServerMemberJoined: 'serverMemberJoined',
8
9
  ServerJoined: 'serverJoined',
@@ -17,6 +18,7 @@ export const ClientEvents = {
17
18
  export type ClientEventMap = {
18
19
  'ready': () => void;
19
20
  'messageCreate': (message: Message) => void;
21
+ 'messageUpdate': (message: Message) => void;
20
22
  'serverMemberLeft': (member: ServerMember) => void;
21
23
  'serverMemberJoined': (member: ServerMember) => void;
22
24
  'serverJoined': (server: Server) => void;
package/src/RawData.ts CHANGED
@@ -12,6 +12,12 @@ export interface AuthenticatedPayload {
12
12
  lastSeenServerChannelIds: Record<string, number>; // { [channelId]: timestamp }
13
13
  }
14
14
 
15
+ export interface RawBotCommand {
16
+ name: string;
17
+ description: string;
18
+ args: string;
19
+ botUserId: string;
20
+ }
15
21
 
16
22
  export interface MessageButtonClickPayload {
17
23
  messageId: string;
@@ -69,10 +75,9 @@ export interface RawMessage {
69
75
  editedAt?: number;
70
76
  mentions?: Array<RawUser>;
71
77
  attachments?: Array<any>;
72
- buttons?: RawMessageButton[]
78
+ buttons?: RawMessageButton[];
73
79
  }
74
80
 
75
-
76
81
  export interface RawMessageButton {
77
82
  id: string;
78
83
  label: string;
@@ -109,4 +114,25 @@ export interface RawChannel {
109
114
 
110
115
  export interface RawCDNUpload {
111
116
  fileId: string;
112
- }
117
+ }
118
+
119
+ export interface RawPost {
120
+ id: string;
121
+ content?: string;
122
+ attachments?: Array<any>;
123
+ deleted: boolean;
124
+ block?: boolean;
125
+ commentToId: string;
126
+ commentTo?: RawPost;
127
+ createdBy: RawUser;
128
+ createdAt: number;
129
+ editedAt: number;
130
+ likedBy: { id: string }[]; // if you liked this post, array will not be empty
131
+ reposts: { id: string; createdBy: { id: string; username: string } }[];
132
+ repost?: RawPost;
133
+ _count: { likedBy: number; comments: number; reposts: number };
134
+ views: number;
135
+ announcement: any;
136
+
137
+ poll?: any;
138
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { Client, Events } from "./Client";
2
- export { AttachmentBuilder } from './Attachment'
2
+ export { AttachmentBuilder } from "./Attachment";
3
3
  export { RPCClient, EmitPayload } from "./RPCClient";
@@ -0,0 +1,16 @@
1
+ import { RawBotCommand } from "../RawData";
2
+ import { ServiceEndpoints } from "./serviceEndpoints";
3
+
4
+ export async function updateCommands(
5
+ token: string,
6
+ commands: Omit<RawBotCommand, "botUserId">[]
7
+ ) {
8
+ return await fetch(ServiceEndpoints.BotCommands(), {
9
+ method: "POST",
10
+ headers: {
11
+ "Content-Type": "application/json",
12
+ Authorization: token,
13
+ },
14
+ body: JSON.stringify({ commands }),
15
+ }).then((res) => res.json());
16
+ }
@@ -10,6 +10,8 @@ interface PostMessageOpts {
10
10
  nerimityCdnFileId?: string;
11
11
  htmlEmbed?: string;
12
12
  buttons?: RawMessageButton[]
13
+ replyToMessageIds?: string[];
14
+ mentionReplies?: boolean;
13
15
  silent? : boolean
14
16
  }
15
17
 
@@ -18,7 +20,7 @@ export async function postMessage(opts: PostMessageOpts) {
18
20
  client: opts.client,
19
21
  url: ServiceEndpoints.PostMessage(opts.channelId),
20
22
  method: 'POST',
21
- body: {content: opts.content, nerimityCdnFileId: opts.nerimityCdnFileId, htmlEmbed: opts.htmlEmbed, buttons: opts.buttons, silent: opts.silent},
23
+ body: {content: opts.content, nerimityCdnFileId: opts.nerimityCdnFileId, htmlEmbed: opts.htmlEmbed, buttons: opts.buttons, silent: opts.silent, mentionReplies: opts.mentionReplies, replyToMessageIds: opts.replyToMessageIds},
22
24
  useToken: true,
23
25
  }).catch(err => {
24
26
  throw new Error(`Failed to send message. ${err.message}`);
@@ -0,0 +1,71 @@
1
+ import { Client } from '../Client';
2
+ import { RawPost } from '../RawData';
3
+ import { request } from './MessageService'
4
+ import { ServiceEndpoints } from './serviceEndpoints';
5
+
6
+ interface PostPostOpts {
7
+ client: Client;
8
+ postId?: string;
9
+ content: string;
10
+ nerimityCdnFileId?: string;
11
+ poll?: {
12
+ choices: string[];
13
+ };
14
+ }
15
+
16
+ interface EditPostOpts {
17
+ client: Client;
18
+ postId: string;
19
+ content: string;
20
+ }
21
+
22
+ interface DeletePostOpts {
23
+ client: Client;
24
+ postId: string;
25
+ }
26
+
27
+ export async function getPosts(client: Client) {
28
+ return await request<RawPost[]>({
29
+ client: client,
30
+ url: ServiceEndpoints.GetPosts(),
31
+ method: 'GET',
32
+ useToken: true,
33
+ }).catch(err => {
34
+ throw new Error(`Failed to get posts. ${err.message}`);
35
+ });
36
+ }
37
+
38
+ export async function postPost(opts: PostPostOpts) {
39
+ return await request<RawPost>({
40
+ client: opts.client,
41
+ url: ServiceEndpoints.PostPost(),
42
+ method: 'POST',
43
+ body: {content: opts.content, nerimityCdnFileId: opts.nerimityCdnFileId, poll: opts.poll},
44
+ useToken: true,
45
+ }).catch(err => {
46
+ throw new Error(`Failed to send post. ${err.message}`);
47
+ });
48
+ }
49
+
50
+ export async function editPost(opts: EditPostOpts) {
51
+ return await request<RawPost>({
52
+ client: opts.client,
53
+ url: ServiceEndpoints.EditPost(opts.postId),
54
+ method: 'PATCH',
55
+ body: {content: opts.content},
56
+ useToken: true,
57
+ }).catch(err => {
58
+ throw new Error(`Failed to edit post. ${err.message}`);
59
+ })
60
+ }
61
+
62
+ export async function deletePost(opts: DeletePostOpts) {
63
+ return await request({
64
+ client: opts.client,
65
+ url: ServiceEndpoints.DeletePost(opts.postId),
66
+ method: 'DELETE',
67
+ useToken: true,
68
+ }).catch(err => {
69
+ throw new Error(`Failed to delete post. ${err.message}`);
70
+ });
71
+ }
@@ -1,21 +1,41 @@
1
1
  // export const path = 'http://localhost:8080';
2
- export let path = 'https://nerimity.com';
3
- let BaseUrl = path + '/api';
2
+ export let path = "https://nerimity.com";
3
+ let BaseUrl = path + "/api";
4
4
 
5
5
  export const updatePath = (newPath: string) => {
6
- path = newPath;
7
- BaseUrl = path + '/api';
6
+ path = newPath;
7
+ BaseUrl = path + "/api";
8
8
  };
9
- const GetMessages = (channelId: string) => `${BaseUrl}/channels/${channelId}/messages`;
10
- const PostMessage = (channelId: string) => `${BaseUrl}/channels/${channelId}/messages`;
11
- const EditMessage = (channelId: string, messageId: string) => `${BaseUrl}/channels/${channelId}/messages/${messageId}`;
9
+ const GetMessages = (channelId: string) =>
10
+ `${BaseUrl}/channels/${channelId}/messages`;
11
+ const PostMessage = (channelId: string) =>
12
+ `${BaseUrl}/channels/${channelId}/messages`;
13
+ const EditMessage = (channelId: string, messageId: string) =>
14
+ `${BaseUrl}/channels/${channelId}/messages/${messageId}`;
12
15
 
16
+ // Posts
17
+ const GetPosts = () => `${BaseUrl}/posts`;
18
+ const PostPost = () => `${BaseUrl}/posts`;
19
+ const EditPost = (postId: string) => `${BaseUrl}/posts/${postId}`;
20
+ const DeletePost = (postId: string) => `${BaseUrl}/posts/${postId}`;
13
21
 
14
- const ButtonClickCallback = (channelId: string, messageId: string, buttonId: string) => `${BaseUrl}/channels/${channelId}/messages/${messageId}/buttons/${buttonId}/callback`;
22
+ const BotCommands = () => `${BaseUrl}/applications/bot/commands`;
23
+
24
+ const ButtonClickCallback = (
25
+ channelId: string,
26
+ messageId: string,
27
+ buttonId: string
28
+ ) =>
29
+ `${BaseUrl}/channels/${channelId}/messages/${messageId}/buttons/${buttonId}/callback`;
15
30
 
16
31
  export const ServiceEndpoints = {
17
- GetMessages,
18
- PostMessage,
19
- EditMessage,
20
- ButtonClickCallback
21
- };
32
+ GetMessages,
33
+ PostMessage,
34
+ EditMessage,
35
+ GetPosts,
36
+ PostPost,
37
+ EditPost,
38
+ DeletePost,
39
+ BotCommands,
40
+ ButtonClickCallback,
41
+ };