Skip to content

Commit

Permalink
fix: typing issues since revolt-api bump
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed Jun 8, 2024
1 parent e913c80 commit a8f13ff
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 35 deletions.
25 changes: 25 additions & 0 deletions src/classes/BannedUser.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 3 additions & 3 deletions src/classes/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -573,7 +573,7 @@ export class Channel {
* @requires `SavedMessages`, `DirectMessage`, `Group`, `TextChannel`
* @returns Messages
*/
async search(params: Omit<OptionsMessageSearch, "include_users">) {
async search(params: Omit<DataMessageSearch, "include_users">) {
const messages = (await this.#collection.client.api.post(
`/channels/${this.id as ""}/search`,
params
Expand All @@ -592,7 +592,7 @@ export class Channel {
* @requires `SavedMessages`, `DirectMessage`, `Group`, `TextChannel`
* @returns Object including messages and users
*/
async searchWithUsers(params: Omit<OptionsMessageSearch, "include_users">) {
async searchWithUsers(params: Omit<DataMessageSearch, "include_users">) {
const data = (await this.#collection.client.api.post(
`/channels/${this.id as ""}/search`,
{
Expand Down
9 changes: 6 additions & 3 deletions src/classes/ChannelWebhook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DataEditWebhook } from "revolt-api";

import { ChannelWebhookCollection } from "../collections/index.js";
import { hydrate } from "../hydration/index.js";

Expand Down Expand Up @@ -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}`
);

Expand Down
28 changes: 17 additions & 11 deletions src/classes/PublicInvite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
}
15 changes: 9 additions & 6 deletions src/classes/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { batch } from "solid-js";

import type {
AllMemberResponse,
BannedUser,
Category,
DataBanCreate,
DataCreateEmoji,
Expand Down Expand Up @@ -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<string, BannedUser>
);

return bans.map((ban) => new ServerBan(this.#collection.client, ban));
return bans.map(
(ban) =>
new ServerBan(this.#collection.client, ban, userDict[ban._id.user])
);
}

/**
Expand Down Expand Up @@ -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}`);
}
}
19 changes: 9 additions & 10 deletions src/classes/ServerBan.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/classes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./BannedUser.js";
export * from "./Bot.js";
export * from "./Channel.js";
export * from "./ChannelUnread.js";
Expand All @@ -15,4 +16,3 @@ export * from "./ServerMember.js";
export * from "./Session.js";
export * from "./SystemMessage.js";
export * from "./User.js";

4 changes: 4 additions & 0 deletions src/collections/ChannelWebhookCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export class ChannelWebhookCollection extends ClassCollection<
async fetch(id: string): Promise<ChannelWebhook> {
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);
}

Expand All @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/collections/ServerCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class ServerCollection extends ClassCollection<Server, HydratedServer> {
}
}

// @ts-expect-error TODO
return this.getOrCreate(data._id, data);
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/collections/UserCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class UserCollection extends ClassCollection<User, HydratedUser> {
_id: SYSTEM_ID,
username: "Revolt",
discriminator: "0000",
online: true,
relationship: "None",
});
}

Expand Down

0 comments on commit a8f13ff

Please sign in to comment.