Skip to content

Commit

Permalink
Major typing changes, introducing Raw & more
Browse files Browse the repository at this point in the history
  • Loading branch information
pakkographic committed Aug 11, 2024
1 parent 583532b commit 0e66843
Show file tree
Hide file tree
Showing 54 changed files with 969 additions and 847 deletions.
3 changes: 2 additions & 1 deletion lib/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const RESTMethods = [
] as const;
export type RESTMethod = typeof RESTMethods[number];

export type UserTypes = "bot" | "user";
export type UserTypes = "app" | "user";
export type RawUserTypes = "bot" | "user";

export * from "guildedapi-types.ts/v1"; // marks api typings as non-external (for docs).

Expand Down
6 changes: 3 additions & 3 deletions lib/gateway/WSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import GatewayError from "./GatewayError";
import { Client } from "../structures/Client";
import { APIBotUser, GatewayOPCodes } from "../Constants";
import { WebsocketEvents, AnyPacket, WelcomePacket } from "../types";
import { GatewayOPCodes } from "../Constants";
import { WebsocketEvents, AnyPacket, WelcomePacket, RawAppUser } from "../types";
import { config as pkgconfig } from "../../pkgconfig";
import { is } from "../util/Util";
import TypedEmitter from "../types/TypedEmitter";
Expand Down Expand Up @@ -261,7 +261,7 @@ export class WSManager extends TypedEmitter<WebsocketEvents> {
this.heartbeat(),
packet.d["heartbeatIntervalMs" as keyof object] as number
);
this.emit("GATEWAY_WELCOME", packet.d["user" as keyof object] as APIBotUser);
this.emit("GATEWAY_WELCOME", packet.d["user" as keyof object] as RawAppUser);
this.emit("GATEWAY_WELCOME_PACKET", packet as WelcomePacket);
this.connected = true;
break;
Expand Down
20 changes: 10 additions & 10 deletions lib/gateway/events/CalendarHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from "../../Constants";
import { CalendarChannel } from "../../structures/CalendarChannel";
import { CalendarReactionInfo } from "../../structures/CalendarReactionInfo";
import { CalendarEventComment } from "../../structures/CalendarEventComment";
import { CalendarComment } from "../../structures/CalendarComment";

/** Internal component, emitting calendar events. */
export class CalendarHandler extends GatewayEventHandler {
Expand Down Expand Up @@ -114,14 +114,14 @@ export class CalendarHandler extends GatewayEventHandler {
);
const CalendarEventComponent =
channel?.scheduledEvents.get(Number(data.calendarEventComment.calendarEventId));
const CalendarComment =
const calendarComment =
CalendarEventComponent?.comments.update(data.calendarEventComment)
?? new CalendarEventComment(
?? new CalendarComment(
data.calendarEventComment,
this.client,
{ guildID: data.serverId }
);
this.client.emit("calendarCommentCreate", CalendarComment);
this.client.emit("calendarCommentCreate", calendarComment);
}

async calendarCommentUpdate(data: GatewayEvent_CalendarEventCommentUpdated): Promise<void> {
Expand All @@ -145,14 +145,14 @@ export class CalendarHandler extends GatewayEventHandler {
channel?.scheduledEvents.get(Number(data.calendarEventComment.calendarEventId));
const CachedComment =
CalendarEventComponent?.comments.get(data.calendarEventComment.id)?.toJSON() ?? null;
const CalendarComment =
const calendarComment =
CalendarEventComponent?.comments.update(data.calendarEventComment)
?? new CalendarEventComment(
?? new CalendarComment(
data.calendarEventComment,
this.client,
{ guildID: data.serverId }
);
this.client.emit("calendarCommentUpdate", CalendarComment, CachedComment);
this.client.emit("calendarCommentUpdate", calendarComment, CachedComment);
}

async calendarCommentDelete(data: GatewayEvent_CalendarEventCommentDeleted): Promise<void> {
Expand All @@ -174,15 +174,15 @@ export class CalendarHandler extends GatewayEventHandler {
);
const CalendarEventComponent =
channel?.scheduledEvents.get(Number(data.calendarEventComment.calendarEventId));
const CalendarComment =
const calendarComment =
CalendarEventComponent?.comments.update(data.calendarEventComment)
?? new CalendarEventComment(
?? new CalendarComment(
data.calendarEventComment,
this.client,
{ guildID: data.serverId }
);
CalendarEventComponent?.comments.delete(data.calendarEventComment.id);
this.client.emit("calendarCommentDelete", CalendarComment);
this.client.emit("calendarCommentDelete", calendarComment);
}

async calendarCommentReactionAdd(data: GatewayEvent_CalendarEventCommentReactionCreated): Promise<void> {
Expand Down
34 changes: 17 additions & 17 deletions lib/gateway/events/GuildHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { GatewayEventHandler } from "./GatewayEventHandler";

import { BannedMember, Guild, GuildRole, Member } from "../../index";
import { BannedMember, Guild, Role, Member } from "../../index";

import { GuildCreateInfo, GuildDeleteInfo } from "../../types";
import {
Expand All @@ -32,8 +32,8 @@ import {
} from "../../Constants";
import { MemberUpdateInfo } from "../../structures/MemberUpdateInfo";
import { MemberRemoveInfo } from "../../structures/MemberRemoveInfo";
import { GuildGroup } from "../../structures/GuildGroup";
import { GuildCategory } from "../../structures/GuildCategory";
import { Group } from "../../structures/Group";
import { Category } from "../../structures/Category";

/** Internal component, emitting guild events. */
export class GuildHandler extends GatewayEventHandler {
Expand Down Expand Up @@ -112,7 +112,7 @@ export class GuildHandler extends GatewayEventHandler {
}

guildGroupCreate(data: GatewayEvent_GroupCreated): void {
const GuildGroupComponent = new GuildGroup(data.group, this.client);
const GuildGroupComponent = new Group(data.group, this.client);
this.client.guilds.get(data.serverId)?.groups.add(GuildGroupComponent);
this.client.emit("guildGroupCreate", GuildGroupComponent);
}
Expand All @@ -121,57 +121,57 @@ export class GuildHandler extends GatewayEventHandler {
const guild = this.client.guilds.get(data.serverId);
const CachedGroup = guild?.groups.get(data.group.id)?.toJSON() ?? null;
const GuildGroupComponent =
guild?.groups.update(new GuildGroup(data.group, this.client))
?? new GuildGroup(data.group, this.client);
guild?.groups.update(new Group(data.group, this.client))
?? new Group(data.group, this.client);
this.client.emit("guildGroupUpdate", GuildGroupComponent, CachedGroup);
}

guildGroupDelete(data: GatewayEvent_GroupDeleted): void {
const guild = this.client.guilds.get(data.serverId);
const GuildGroupComponent =
guild?.groups.update(new GuildGroup(data.group, this.client))
?? new GuildGroup(data.group, this.client);
guild?.groups.update(new Group(data.group, this.client))
?? new Group(data.group, this.client);
this.client.emit("guildGroupDelete", GuildGroupComponent);
}

guildRoleCreate(data: GatewayEvent_RoleCreated): void {
const guild = this.client.guilds.get(data.serverId);
const role =
guild?.roles.add(new GuildRole(data.role, this.client))
?? new GuildRole(data.role, this.client);
guild?.roles.add(new Role(data.role, this.client))
?? new Role(data.role, this.client);
this.client.emit("guildRoleCreate", role);
}

guildRoleUpdate(data: GatewayEvent_RoleUpdated): void {
const guild = this.client.guilds.get(data.serverId);
const cachedRole = guild?.roles.get(data.role.id)?.toJSON() ?? null;
const role =
guild?.roles.update(new GuildRole(data.role, this.client))
?? new GuildRole(data.role, this.client);
guild?.roles.update(new Role(data.role, this.client))
?? new Role(data.role, this.client);
this.client.emit("guildRoleUpdate", role, cachedRole);
}

guildRoleDelete(data: GatewayEvent_RoleDeleted): void {
const guild = this.client.guilds.get(data.serverId);
const role =
guild?.roles.update(new GuildRole(data.role, this.client))
?? new GuildRole(data.role, this.client);
guild?.roles.update(new Role(data.role, this.client))
?? new Role(data.role, this.client);
guild?.roles.delete(data.role.id);
this.client.emit("guildRoleDelete", role);
}

guildCategoryCreate(data: GatewayEvent_CategoryCreated): void {
const category = new GuildCategory(data.category, this.client);
const category = new Category(data.category, this.client);
this.client.emit("guildCategoryCreate", category);
}

guildCategoryUpdate(data: GatewayEvent_CategoryCreated): void {
const category = new GuildCategory(data.category, this.client);
const category = new Category(data.category, this.client);
this.client.emit("guildCategoryUpdate", category);
}

guildCategoryDelete(data: GatewayEvent_CategoryCreated): void {
const category = new GuildCategory(data.category, this.client);
const category = new Category(data.category, this.client);
this.client.emit("guildCategoryDelete", category);
}
}
10 changes: 5 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export * from "./structures/ListItem";
export * from "./structures/Webhook";
export * from "./structures/DocComment";
export * from "./structures/SocialLink";
export * from "./structures/CalendarEventComment";
export * from "./structures/CalendarComment";

export * from "./structures/ReactionInfo";
export * from "./structures/MessageReactionInfo";
Expand All @@ -60,10 +60,10 @@ export * from "./structures/DocReactionInfo";
export * from "./structures/MemberRemoveInfo";
export * from "./structures/MemberUpdateInfo";
export * from "./structures/CalendarReactionInfo";
export * from "./structures/GuildRole";
export * from "./structures/GuildGroup";
export * from "./structures/GuildCategory";
export * from "./structures/GuildSubscription";
export * from "./structures/Role";
export * from "./structures/Group";
export * from "./structures/Category";
export * from "./structures/Subscription";
export * from "./structures/Permission";

export * from "./util/Collection";
Expand Down
29 changes: 15 additions & 14 deletions lib/routes/Channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Message } from "../structures/Message";
import { ForumThreadComment } from "../structures/ForumThreadComment";
import { ListItem } from "../structures/ListItem";
import {
APIListItem,
ChannelReactionTypeBulkDeleteSupported,
ChannelReactionTypes,
ChannelSubcategoryReactionTypes,
Expand Down Expand Up @@ -104,18 +103,20 @@ import {
CreateForumCommentOptions,
EditForumCommentOptions,
EditDocOptions,
CreateDocOptions
CreateDocOptions,
RawMessage,
RawListItem
} from "../types";
import { DocChannel } from "../structures/DocChannel";
import { ForumChannel } from "../structures/ForumChannel";
import { CalendarChannel } from "../structures/CalendarChannel";
import { TextChannel } from "../structures/TextChannel";
import { CalendarEventComment } from "../structures/CalendarEventComment";
import { CalendarComment } from "../structures/CalendarComment";
import { DocComment } from "../structures/DocComment";
import { Announcement } from "../structures/Announcement";
import { AnnouncementComment } from "../structures/AnnouncementComment";
import { Permission } from "../structures/Permission";
import { APIChatMessage, PUTChannelMessageResponse } from "guildedapi-types.ts/v1";
import { PUTChannelMessageResponse } from "guildedapi-types.ts/v1";

export class Channels {
#manager: RESTManager;
Expand Down Expand Up @@ -362,12 +363,12 @@ export class Channels {
* @param eventID ID of an event containing the comment to get.
* @param commentID ID of the comment to get.
*/
async getCalendarEventComment(channelID: string, eventID: number, commentID: number): Promise<CalendarEventComment> {
async getCalendarEventComment(channelID: string, eventID: number, commentID: number): Promise<CalendarComment> {
return this.#manager.authRequest<GETCalendarEventCommentResponse>({
method: "GET",
path: endpoints.CHANNEL_EVENT_COMMENT(channelID, eventID, commentID)
}).then(data =>
new CalendarEventComment(data.calendarEventComment, this.#manager.client)
new CalendarComment(data.calendarEventComment, this.#manager.client)
);
}

Expand All @@ -376,13 +377,13 @@ export class Channels {
* @param channelID ID of a "Calendar" channel.
* @param eventID ID of the event containing comments.
*/
async getCalendarEventComments(channelID: string, eventID: number): Promise<Array<CalendarEventComment>> {
async getCalendarEventComments(channelID: string, eventID: number): Promise<Array<CalendarComment>> {
return this.#manager.authRequest<GETCalendarEventCommentsResponse>({
method: "GET",
path: endpoints.CHANNEL_EVENT_COMMENTS(channelID, eventID)
}).then(data =>
data.calendarEventComments.map(d =>
new CalendarEventComment(d, this.#manager.client)
new CalendarComment(d, this.#manager.client)
)
);
}
Expand Down Expand Up @@ -445,7 +446,7 @@ export class Channels {
path: endpoints.LIST_ITEMS(channelID)
}).then(data =>
data.listItems.map(d =>
new ListItem(d as APIListItem, this.#manager.client)) as never
new ListItem(d as RawListItem, this.#manager.client)) as never
);
}

Expand Down Expand Up @@ -501,7 +502,7 @@ export class Channels {
}
}).then(data =>
this.#manager.client.util.updateMessage(
data.message as APIChatMessage,
data.message as RawMessage,
params
)
);
Expand Down Expand Up @@ -1067,14 +1068,14 @@ export class Channels {
channelID: string,
eventID: number,
options: CreateCalendarCommentOptions
): Promise<CalendarEventComment> {
): Promise<CalendarComment> {
if (typeof options !== "object") throw new Error("comment options should be an object.");
return this.#manager.authRequest<POSTCalendarEventCommentResponse>({
method: "POST",
path: endpoints.CHANNEL_EVENT_COMMENTS(channelID, eventID),
json: options
}).then(data =>
new CalendarEventComment(data.calendarEventComment, this.#manager.client)
new CalendarComment(data.calendarEventComment, this.#manager.client)
);
}

Expand All @@ -1089,14 +1090,14 @@ export class Channels {
eventID: number,
commentID: number,
options: EditCalendarCommentOptions
): Promise<CalendarEventComment> {
): Promise<CalendarComment> {
if (typeof options !== "object") throw new Error("comment options should be an object.");
return this.#manager.authRequest<PATCHCalendarEventCommentResponse>({
method: "PATCH",
path: endpoints.CHANNEL_EVENT_COMMENT(channelID, eventID, commentID),
json: options
}).then(data =>
new CalendarEventComment(data.calendarEventComment, this.#manager.client)
new CalendarComment(data.calendarEventComment, this.#manager.client)
);
}

Expand Down
Loading

0 comments on commit 0e66843

Please sign in to comment.