mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-19 17:13:31 -04:00
a672fea424
* fix: hydrate Bot and User when using edit() closes #146 Signed-off-by: Amy <amy+git@amogus.cloud> * chore: run formatter Signed-off-by: Amy <amy+git@amogus.cloud> --------- Signed-off-by: Amy <amy+git@amogus.cloud>
162 lines
3.3 KiB
TypeScript
162 lines
3.3 KiB
TypeScript
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 { hydrate } from "../hydration/index.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> {
|
|
this.#collection.updateUnderlyingObject(
|
|
this.id,
|
|
hydrate(
|
|
"bot",
|
|
await this.#collection.client.api.patch(`/bots/${this.id as ""}`, data),
|
|
this.#collection.client,
|
|
false,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Delete a bot
|
|
*/
|
|
async delete(): Promise<void> {
|
|
await this.#collection.client.api.delete(`/bots/${this.id as ""}`);
|
|
this.#collection.delete(this.id);
|
|
}
|
|
}
|