Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 40 additions & 45 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
import { Accessor, Setter, batch, createSignal } from "solid-js";
import type { Accessor, Setter } from "solid-js";
import { batch, createSignal } from "solid-js";

import { AsyncEventEmitter } from "@vladfrangu/async_event_emitter";
import type { DataLogin, RevoltConfig } from "revolt-api";
import { API, Role } from "revolt-api";

import {
Channel,
Emoji,
Message,
Server,
ServerMember,
User,
} from "./classes/index.js";
import { API } from "revolt-api";
import type { DataLogin, RevoltConfig, Role } from "revolt-api";

import type { Channel } from "./classes/Channel.js";
import type { Emoji } from "./classes/Emoji.js";
import type { Message } from "./classes/Message.js";
import type { Server } from "./classes/Server.js";
import type { ServerMember } from "./classes/ServerMember.js";
import type { User } from "./classes/User.js";
import { AccountCollection } from "./collections/AccountCollection.js";
import {
BotCollection,
ChannelCollection,
ChannelUnreadCollection,
ChannelWebhookCollection,
EmojiCollection,
MessageCollection,
ServerCollection,
ServerMemberCollection,
SessionCollection,
UserCollection,
} from "./collections/index.js";
import { BotCollection } from "./collections/BotCollection.js";
import { ChannelCollection } from "./collections/ChannelCollection.js";
import { ChannelUnreadCollection } from "./collections/ChannelUnreadCollection.js";
import { ChannelWebhookCollection } from "./collections/ChannelWebhookCollection.js";
import { EmojiCollection } from "./collections/EmojiCollection.js";
import { MessageCollection } from "./collections/MessageCollection.js";
import { ServerCollection } from "./collections/ServerCollection.js";
import { ServerMemberCollection } from "./collections/ServerMemberCollection.js";
import { SessionCollection } from "./collections/SessionCollection.js";
import { UserCollection } from "./collections/UserCollection.js";
import {
ConnectionState,
EventClient,
EventClientOptions,
handleEventV1,
} from "./events/index.js";
import {
HydratedChannel,
HydratedEmoji,
HydratedMessage,
HydratedServer,
HydratedServerMember,
HydratedUser,
} from "./hydration/index.js";
type EventClientOptions,
} from "./events/EventClient.js";
import { handleEvent } from "./events/v1.js";
import type { HydratedChannel } from "./hydration/channel.js";
import type { HydratedEmoji } from "./hydration/emoji.js";
import type { HydratedMessage } from "./hydration/message.js";
import type { HydratedServer } from "./hydration/server.js";
import type { HydratedServerMember } from "./hydration/serverMember.js";
import type { HydratedUser } from "./hydration/user.js";
import { RE_CHANNELS, RE_MENTIONS, RE_SPOILER } from "./lib/regex.js";

export type Session = { _id: string; token: string; user_id: string } | string;
Expand Down Expand Up @@ -272,21 +267,21 @@ export class Client extends AsyncEventEmitter<Events> {
});

this.events.on("event", (event) =>
handleEventV1(this, event, this.#setReady),
handleEvent(this, event, this.#setReady),
);
}

/**
* Current session id
*/
get sessionId() {
get sessionId(): string | undefined {
return typeof this.#session === "string" ? undefined : this.#session?._id;
}

/**
* Get authentication header
*/
get authenticationHeader() {
get authenticationHeader(): [string, string] {
return typeof this.#session === "string"
? ["X-Bot-Token", this.#session]
: ["X-Session-Token", this.#session?.token as string];
Expand All @@ -295,7 +290,7 @@ export class Client extends AsyncEventEmitter<Events> {
/**
* Connect to Revolt
*/
connect() {
connect(): void {
clearTimeout(this.#reconnectTimeout);
this.events.disconnect();
this.#setReady(false);
Expand All @@ -308,7 +303,7 @@ export class Client extends AsyncEventEmitter<Events> {
/**
* Fetches the configuration of the server if it has not been already fetched.
*/
async #fetchConfiguration() {
async #fetchConfiguration(): Promise<void> {
if (!this.configuration) {
this.configuration = await this.api.get("/");
}
Expand All @@ -317,7 +312,7 @@ export class Client extends AsyncEventEmitter<Events> {
/**
* Update API object to use authentication.
*/
#updateHeaders() {
#updateHeaders(): void {
(this.api as API) = new API({
baseURL: this.options.baseURL,
authentication: {
Expand All @@ -331,7 +326,7 @@ export class Client extends AsyncEventEmitter<Events> {
* @param details Login data object
* @returns An on-boarding function if on-boarding is required, undefined otherwise
*/
async login(details: DataLogin) {
async login(details: DataLogin): Promise<void> {
await this.#fetchConfiguration();
const data = await this.api.post("/auth/session/login", details);
if (data.result === "Success") {
Expand All @@ -345,7 +340,7 @@ export class Client extends AsyncEventEmitter<Events> {
/**
* Use an existing session
*/
async useExistingSession(session: Session) {
useExistingSession(session: Session): void {
this.#session = session;
this.#updateHeaders();
}
Expand All @@ -354,7 +349,7 @@ export class Client extends AsyncEventEmitter<Events> {
* Log in as a bot
* @param token Bot token
*/
async loginBot(token: string) {
async loginBot(token: string): Promise<void> {
await this.#fetchConfiguration();
this.#session = token;
this.#updateHeaders();
Expand All @@ -366,7 +361,7 @@ export class Client extends AsyncEventEmitter<Events> {
* @param source Source markdown text
* @returns Modified plain text
*/
markdownToText(source: string) {
markdownToText(source: string): string {
return source
.replace(RE_MENTIONS, (sub: string, id: string) => {
const user = this.users.get(id as string);
Expand Down
8 changes: 5 additions & 3 deletions src/classes/BannedUser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BannedUser as ApiBannedUser } from "revolt-api";
import type { BannedUser as APIBannedUser } from "revolt-api";

import { Client, File } from "../index.js";
import type { Client } from "../Client.js";

import { File } from "./File.js";

/**
* Banned User
Expand All @@ -16,7 +18,7 @@ export class BannedUser {
* @param client Client
* @param data Data
*/
constructor(client: Client, data: ApiBannedUser) {
constructor(client: Client, data: APIBannedUser) {
this.id = data._id;
this.avatar = data.avatar ? new File(client, data.avatar) : undefined;
this.username = data.username;
Expand Down
39 changes: 21 additions & 18 deletions src/classes/Bot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { DataEditBot } from "revolt-api";
import type { DataEditBot } from "revolt-api";
import { decodeTime } from "ulid";

import { BotCollection } from "../collections/index.js";
import type { BotCollection } from "../collections/BotCollection.js";
import type { BotFlags } from "../hydration/bot.js";

import type { User } from "./User.js";

/**
* Bot Class
Expand All @@ -24,113 +27,113 @@ export class Bot {
* Convert to string
* @returns String
*/
toString() {
toString(): string {
return `<@${this.id}>`;
}

/**
* Whether this object exists
*/
get $exists() {
get $exists(): boolean {
return !!this.#collection.getUnderlyingObject(this.id).id;
}

/**
* Time when this user created their account
*/
get createdAt() {
get createdAt(): Date {
return new Date(decodeTime(this.id));
}

/**
* Corresponding user
*/
get user() {
get user(): User | undefined {
return this.#collection.client.users.get(this.id);
}

/**
* Owner's Id
*/
get ownerId() {
get ownerId(): string {
return this.#collection.getUnderlyingObject(this.id).ownerId;
}

/**
* Owner
*/
get owner() {
get owner(): User | undefined {
return this.#collection.client.users.get(this.ownerId);
}

/**
* Bot Token
*/
get token() {
get token(): string {
return this.#collection.getUnderlyingObject(this.id).token;
}

/**
* Whether this bot can be invited by anyone
*/
get public() {
get public(): boolean {
return this.#collection.getUnderlyingObject(this.id).public;
}

/**
* Whether this bot has analytics enabled
*/
get analytics() {
get analytics(): boolean {
return this.#collection.getUnderlyingObject(this.id).analytics;
}

/**
* Whether this bot shows up on Discover
*/
get discoverable() {
get discoverable(): boolean {
return this.#collection.getUnderlyingObject(this.id).discoverable;
}

/**
* Interactions URL
*/
get interactionsUrl() {
get interactionsUrl(): string | undefined {
return this.#collection.getUnderlyingObject(this.id).interactionsUrl;
}

/**
* Link to terms of service
*/
get termsOfServiceUrl() {
get termsOfServiceUrl(): string | undefined {
return this.#collection.getUnderlyingObject(this.id).termsOfServiceUrl;
}

/**
* Link to privacy policy
*/
get privacyPolicyUrl() {
get privacyPolicyUrl(): string | undefined {
return this.#collection.getUnderlyingObject(this.id).privacyPolicyUrl;
}

/**
* Bot Flags
*/
get flags() {
get flags(): BotFlags {
return this.#collection.getUnderlyingObject(this.id).flags;
}

/**
* Edit a bot
* @param data Changes
*/
async edit(data: DataEditBot) {
async edit(data: DataEditBot): Promise<void> {
await this.#collection.client.api.patch(`/bots/${this.id as ""}`, data);
}

/**
* Delete a bot
*/
async delete() {
async delete(): Promise<void> {
await this.#collection.client.api.delete(`/bots/${this.id as ""}`);
this.#collection.delete(this.id);
}
Expand Down
Loading
Loading