forked from stoatchat/javascript-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvite.ts
More file actions
97 lines (84 loc) · 2.03 KB
/
Copy pathInvite.ts
File metadata and controls
97 lines (84 loc) · 2.03 KB
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
import type { Invite } from "stoat-api";
import type { Client } from "../Client.js";
import type { Channel } from "./Channel.js";
import type { Server } from "./Server.js";
import type { User } from "./User.js";
/**
* Channel Invite
*/
export abstract class ChannelInvite {
protected client?: Client;
readonly type: Invite["type"] | "None";
/**
* Construct Channel Invite
* @param client Client
* @param type Type
*/
constructor(client?: Client, type: Invite["type"] | "None" = "None") {
this.client = client;
this.type = type;
}
/**
* Create an Invite from an API Invite
* @param client Client
* @param invite Data
* @returns Invite
*/
static from(client: Client, invite: Invite): ChannelInvite {
switch (invite.type) {
case "Server":
return new ServerInvite(client, invite);
default:
return new UnknownInvite(client);
}
}
}
/**
* Invite of unknown type
*/
export class UnknownInvite extends ChannelInvite {}
/**
* Server Invite
*/
export class ServerInvite extends ChannelInvite {
readonly id: string;
readonly creatorId: string;
readonly serverId: string;
readonly channelId: string;
/**
* Construct Server Invite
* @param client Client
* @param invite Invite
*/
constructor(client: Client, invite: Invite & { type: "Server" }) {
super(client, "Server");
this.id = invite._id;
this.creatorId = invite.creator;
this.serverId = invite.server;
this.channelId = invite.channel;
}
/**
* Creator of the invite
*/
get creator(): User | undefined {
return this.client!.users.get(this.creatorId);
}
/**
* Server this invite points to
*/
get server(): Server | undefined {
return this.client!.servers.get(this.serverId);
}
/**
* Channel this invite points to
*/
get channel(): Channel | undefined {
return this.client!.channels.get(this.channelId);
}
/**
* Delete the invite
*/
async delete(): Promise<void> {
await this.client!.api.delete(`/invites/${this.id}`);
}
}