forked from stoatchat/javascript-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.ts
More file actions
152 lines (131 loc) · 3.11 KB
/
Copy pathBot.ts
File metadata and controls
152 lines (131 loc) · 3.11 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
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
import type { DataEditBot } from "stoat-api";
import { decodeTime } from "ulid";
import type { BotCollection } from "../collections/BotCollection.js";
import type { BotFlags } from "../hydration/bot.js";
import { PublicBot } from "./PublicBot.js";
import type { User } from "./User.js";
/**
* Bot Class
*/
export class Bot {
readonly #collection: BotCollection;
readonly id: string;
/**
* Construct Bot
* @param collection Collection
* @param id Id
*/
constructor(collection: BotCollection, id: string) {
this.#collection = collection;
this.id = id;
}
/**
* Convert to string
* @returns String
*/
toString(): string {
return `<@${this.id}>`;
}
/**
* Whether this object exists
*/
get $exists(): boolean {
return !!this.#collection.getUnderlyingObject(this.id).id;
}
/**
* Time when this user created their account
*/
get createdAt(): Date {
return new Date(decodeTime(this.id));
}
/**
* Corresponding user
*/
get user(): User | undefined {
return this.#collection.client.users.get(this.id);
}
/**
* Owner's Id
*/
get ownerId(): string {
return this.#collection.getUnderlyingObject(this.id).ownerId;
}
/**
* Owner
*/
get owner(): User | undefined {
return this.#collection.client.users.get(this.ownerId);
}
/**
* Bot Token
*/
get token(): string {
return this.#collection.getUnderlyingObject(this.id).token;
}
/**
* Whether this bot can be invited by anyone
*/
get public(): boolean {
return this.#collection.getUnderlyingObject(this.id).public;
}
/**
* Whether this bot has analytics enabled
*/
get analytics(): boolean {
return this.#collection.getUnderlyingObject(this.id).analytics;
}
/**
* Whether this bot shows up on Discover
*/
get discoverable(): boolean {
return this.#collection.getUnderlyingObject(this.id).discoverable;
}
/**
* Interactions URL
*/
get interactionsUrl(): string | undefined {
return this.#collection.getUnderlyingObject(this.id).interactionsUrl;
}
/**
* Link to terms of service
*/
get termsOfServiceUrl(): string | undefined {
return this.#collection.getUnderlyingObject(this.id).termsOfServiceUrl;
}
/**
* Link to privacy policy
*/
get privacyPolicyUrl(): string | undefined {
return this.#collection.getUnderlyingObject(this.id).privacyPolicyUrl;
}
/**
* Bot Flags
*/
get flags(): BotFlags {
return this.#collection.getUnderlyingObject(this.id).flags;
}
/**
* Instantiate `PublicBot` class from this bot
*/
get publicBot(): PublicBot {
return new PublicBot(this.#collection.client, {
_id: this.id,
username: this.user?.username ?? "",
avatar: this.user?.avatar?.id,
});
}
/**
* Edit a bot
* @param data Changes
*/
async edit(data: DataEditBot): Promise<void> {
await this.#collection.client.api.patch(`/bots/${this.id as ""}`, data);
}
/**
* Delete a bot
*/
async delete(): Promise<void> {
await this.#collection.client.api.delete(`/bots/${this.id as ""}`);
this.#collection.delete(this.id);
}
}