From a8f13ff27cf2bb37579fddfaa1240907d363adbf Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sat, 8 Jun 2024 11:33:21 +0100 Subject: [PATCH] fix: typing issues since revolt-api bump --- src/classes/BannedUser.ts | 25 ++++++++++++++++++ src/classes/Channel.ts | 6 ++--- src/classes/ChannelWebhook.ts | 9 ++++--- src/classes/PublicInvite.ts | 28 +++++++++++++-------- src/classes/Server.ts | 15 ++++++----- src/classes/ServerBan.ts | 19 +++++++------- src/classes/index.ts | 2 +- src/collections/ChannelWebhookCollection.ts | 4 +++ src/collections/ServerCollection.ts | 1 - src/collections/UserCollection.ts | 2 ++ 10 files changed, 76 insertions(+), 35 deletions(-) create mode 100644 src/classes/BannedUser.ts diff --git a/src/classes/BannedUser.ts b/src/classes/BannedUser.ts new file mode 100644 index 00000000..ce96c9d6 --- /dev/null +++ b/src/classes/BannedUser.ts @@ -0,0 +1,25 @@ +import { BannedUser as ApiBannedUser } from "revolt-api"; + +import { Client, File } from "../index.js"; + +/** + * Banned User + */ +export class BannedUser { + readonly id: string; + readonly avatar?: File; + readonly username: string; + readonly discriminator: string; + + /** + * Construct Banned User + * @param client Client + * @param data Data + */ + constructor(client: Client, data: ApiBannedUser) { + this.id = data._id; + this.avatar = data.avatar ? new File(client, data.avatar) : undefined; + this.username = data.username; + this.discriminator = data.discriminator; + } +} diff --git a/src/classes/Channel.ts b/src/classes/Channel.ts index d1ec4ed7..b9a8fb5c 100644 --- a/src/classes/Channel.ts +++ b/src/classes/Channel.ts @@ -5,8 +5,8 @@ import type { Message as ApiMessage, User as ApiUser, DataEditChannel, + DataMessageSearch, DataMessageSend, - OptionsMessageSearch, Override, } from "revolt-api"; import { APIRoutes } from "revolt-api/dist/routes"; @@ -573,7 +573,7 @@ export class Channel { * @requires `SavedMessages`, `DirectMessage`, `Group`, `TextChannel` * @returns Messages */ - async search(params: Omit) { + async search(params: Omit) { const messages = (await this.#collection.client.api.post( `/channels/${this.id as ""}/search`, params @@ -592,7 +592,7 @@ export class Channel { * @requires `SavedMessages`, `DirectMessage`, `Group`, `TextChannel` * @returns Object including messages and users */ - async searchWithUsers(params: Omit) { + async searchWithUsers(params: Omit) { const data = (await this.#collection.client.api.post( `/channels/${this.id as ""}/search`, { diff --git a/src/classes/ChannelWebhook.ts b/src/classes/ChannelWebhook.ts index 4082b380..ab823956 100644 --- a/src/classes/ChannelWebhook.ts +++ b/src/classes/ChannelWebhook.ts @@ -1,5 +1,3 @@ -import { DataEditWebhook } from "revolt-api"; - import { ChannelWebhookCollection } from "../collections/index.js"; import { hydrate } from "../hydration/index.js"; @@ -75,24 +73,29 @@ export class ChannelWebhook { /** * Edit this webhook + * TODO: not in production */ - async edit(data: DataEditWebhook) { + async edit(data: any /*: DataEditWebhook*/) { const webhook = await this.#collection.client.api.patch( + // @ts-expect-error not in prod `/webhooks/${this.id as ""}/${this.token as ""}`, data ); this.#collection.updateUnderlyingObject( this.id, + // @ts-expect-error not in prod hydrate("channelWebhook", webhook, this.#collection.client) ); } /** * Delete this webhook + * TODO: not in production */ async delete() { await this.#collection.client.api.delete( + // @ts-expect-error not in prod `/webhooks/${this.id}/${this.token}` ); diff --git a/src/classes/PublicInvite.ts b/src/classes/PublicInvite.ts index d446dc79..68444d87 100644 --- a/src/classes/PublicInvite.ts +++ b/src/classes/PublicInvite.ts @@ -97,16 +97,22 @@ export class ServerPublicInvite extends PublicChannelInvite { const existingServer = this.client!.servers.get(this.serverId); if (existingServer) return existingServer; - const { server, channels } = await this.client!.api.post( - `/invites/${this.code as ""}` - ); - - return batch(() => { - for (const channel of channels) { - this.client!.channels.getOrCreate(channel._id, channel); - } - - return this.client!.servers.getOrCreate(server._id, server, true); - }); + const invite = await this.client!.api.post(`/invites/${this.code as ""}`); + + if (invite.type === "Server") { + return batch(() => { + for (const channel of invite.channels) { + this.client!.channels.getOrCreate(channel._id, channel); + } + + return this.client!.servers.getOrCreate( + invite.server._id, + invite.server, + true + ); + }); + } else { + throw "unreachable"; + } } } diff --git a/src/classes/Server.ts b/src/classes/Server.ts index 5b6c2936..73bd56fc 100644 --- a/src/classes/Server.ts +++ b/src/classes/Server.ts @@ -2,6 +2,7 @@ import { batch } from "solid-js"; import type { AllMemberResponse, + BannedUser, Category, DataBanCreate, DataCreateEmoji, @@ -514,11 +515,15 @@ export class Server { `/servers/${this.id as ""}/bans` ); - users.forEach((user) => - this.#collection.client.users.getOrCreate(user._id, user) + const userDict = users.reduce( + (d, c) => ({ ...d, [c._id]: c }), + {} as Record ); - return bans.map((ban) => new ServerBan(this.#collection.client, ban)); + return bans.map( + (ban) => + new ServerBan(this.#collection.client, ban, userDict[ban._id.user]) + ); } /** @@ -720,8 +725,6 @@ export class Server { * @param emojiId Emoji ID */ async deleteEmoji(emojiId: string) { - return await this.#collection.client.api.delete( - `/custom/emoji/${emojiId}` - ); + return await this.#collection.client.api.delete(`/custom/emoji/${emojiId}`); } } diff --git a/src/classes/ServerBan.ts b/src/classes/ServerBan.ts index f3a6697f..2d20307b 100644 --- a/src/classes/ServerBan.ts +++ b/src/classes/ServerBan.ts @@ -1,6 +1,10 @@ -import { MemberCompositeKey } from "revolt-api"; +import { + BannedUser as ApiBannedUser, + ServerBan as ApiServerBan, + MemberCompositeKey, +} from "revolt-api"; -import { API, Client } from "../index.js"; +import { BannedUser, Client } from "../index.js"; /** * Server Ban @@ -9,23 +13,18 @@ export class ServerBan { protected client: Client; readonly id: MemberCompositeKey; readonly reason?: string; + readonly user?: BannedUser; /** * Construct Server Ban * @param client Client * @param data Data */ - constructor(client: Client, data: API.ServerBan) { + constructor(client: Client, data: ApiServerBan, user?: ApiBannedUser) { this.client = client; this.id = data._id; this.reason = data.reason!; - } - - /** - * User - */ - get user() { - return this.client.users.get(this.id.user); + this.user = user ? new BannedUser(client, user) : undefined; } /** diff --git a/src/classes/index.ts b/src/classes/index.ts index ed8ac5fe..7575c6f9 100644 --- a/src/classes/index.ts +++ b/src/classes/index.ts @@ -1,3 +1,4 @@ +export * from "./BannedUser.js"; export * from "./Bot.js"; export * from "./Channel.js"; export * from "./ChannelUnread.js"; @@ -15,4 +16,3 @@ export * from "./ServerMember.js"; export * from "./Session.js"; export * from "./SystemMessage.js"; export * from "./User.js"; - diff --git a/src/collections/ChannelWebhookCollection.ts b/src/collections/ChannelWebhookCollection.ts index 466af1cf..2c49ee7f 100644 --- a/src/collections/ChannelWebhookCollection.ts +++ b/src/collections/ChannelWebhookCollection.ts @@ -19,7 +19,9 @@ export class ChannelWebhookCollection extends ClassCollection< async fetch(id: string): Promise { const webhook = this.get(id); if (webhook) return webhook; + // @ts-expect-error not in prod const data = await this.client.api.get(`/webhooks/${id as ""}`); + // @ts-expect-error not in prod return this.getOrCreate(data.id, data as API.Webhook); } @@ -33,8 +35,10 @@ export class ChannelWebhookCollection extends ClassCollection< const webhook = this.get(id); if (webhook) return webhook; const data = await this.client.api.get( + // @ts-expect-error not in prod `/webhooks/${id as ""}/${token as ""}` ); + // @ts-expect-error not in prod return this.getOrCreate(data.id, data); } diff --git a/src/collections/ServerCollection.ts b/src/collections/ServerCollection.ts index de4ed7f1..1496902c 100644 --- a/src/collections/ServerCollection.ts +++ b/src/collections/ServerCollection.ts @@ -32,7 +32,6 @@ export class ServerCollection extends ClassCollection { } } - // @ts-expect-error TODO return this.getOrCreate(data._id, data); }); } diff --git a/src/collections/UserCollection.ts b/src/collections/UserCollection.ts index 6331921b..a052eab2 100644 --- a/src/collections/UserCollection.ts +++ b/src/collections/UserCollection.ts @@ -18,6 +18,8 @@ export class UserCollection extends ClassCollection { _id: SYSTEM_ID, username: "Revolt", discriminator: "0000", + online: true, + relationship: "None", }); }