forked from stoatchat/javascript-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotCollection.ts
More file actions
81 lines (72 loc) · 2.02 KB
/
Copy pathBotCollection.ts
File metadata and controls
81 lines (72 loc) · 2.02 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
import { batch } from "solid-js";
import type { Bot as APIBot, OwnedBotsResponse } from "stoat-api";
import { Bot } from "../classes/Bot.js";
import { PublicBot } from "../classes/PublicBot.js";
import type { HydratedBot } from "../hydration/bot.js";
import { ClassCollection } from "./Collection.js";
/**
* Collection of Bots
*/
export class BotCollection extends ClassCollection<Bot, HydratedBot> {
/**
* Fetch bot by ID
* @param id Id
* @returns Bot
*/
async fetch(id: string): Promise<Bot> {
const bot = this.get(id);
if (bot) return bot;
const data = await this.client.api.get(`/bots/${id as ""}`);
this.client.users.getOrCreate(data.user._id, data.user);
return this.getOrCreate(data.bot._id, data.bot);
}
/**
* Fetch owned bots
* @returns List of bots
*/
async fetchOwned(): Promise<Bot[]> {
const data = (await this.client.api.get("/bots/@me")) as OwnedBotsResponse;
return batch(() => {
data.users.forEach((user) =>
this.client.users.getOrCreate(user._id, user),
);
return data.bots.map((bot) => this.getOrCreate(bot._id, bot));
});
}
/**
* Fetch public bot by ID
* @param id Id
* @returns Public Bot
*/
async fetchPublic(id: string): Promise<PublicBot> {
const data = await this.client.api.get(`/bots/${id as ""}/invite`);
return new PublicBot(this.client, data);
}
/**
* Get or create
* @param id Id
* @param data Data
* @returns Bot
*/
getOrCreate(id: string, data: APIBot): Bot {
if (this.has(id)) {
return this.get(id)!;
} else {
const instance = new Bot(this, id);
this.create(id, "bot", instance, this.client, data);
return instance;
}
}
/**
* Create a bot
* @param name Bot name
* @returns The newly-created bot
*/
async createBot(name: string): Promise<Bot> {
const { user, ...bot } = await this.client.api.post(`/bots/create`, {
name,
});
this.client.users.getOrCreate(user._id, user);
return this.getOrCreate(bot._id, bot);
}
}