Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

feat: add forum topic comments #125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changeset/curvy-jobs-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'guilded.ts': minor
'@guildedts/rest': minor
---

feat: add forum topic comments
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Collector, CollectorOptions } from './Collector';
import { ForumTopicCommentReaction } from '../structures/forum/ForumTopicCommentReaction';
import { ForumTopicCommentReactionManager } from '../managers/forum/ForumTopicCommentReactionManager';

/**
* The collector for message reactions
*/
export class ForumTopicCommentReactionCollector extends Collector<
number,
ForumTopicCommentReaction
> {
/**
* @param reactions The manager for message reactions
* @param options The options for the reaction collector
*/
constructor(
public readonly reactions: ForumTopicCommentReactionManager,
options?: CollectorOptions<ForumTopicCommentReaction>,
) {
super(reactions.client, options);
this.options.dispose =
options?.dispose ?? this.client.options.disposeCollectedMessageReactions;
this.client.on('forumTopicCommentReactionAdd', this.collectReaction.bind(this));
this.client.on('forumTopicCommentReactionRemove', this.disposeReaction.bind(this));
}

private collectReaction(reaction: ForumTopicCommentReaction) {
if (reaction.forumTopicComment.id !== this.reactions.forumTopicComment.id) return;
this.collect(reaction.emote.id, reaction);
}

private disposeReaction(reaction: ForumTopicCommentReaction) {
this.dispose(reaction.emote.id);
}
}
4 changes: 2 additions & 2 deletions packages/guilded.ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export * from './managers/server/ServerRoleManager';
export * from './managers/BaseManager';
export * from './managers/DocManager';
export * from './managers/ListItemManager';
export * from './managers/ForumTopicManager';
export * from './managers/forum/ForumTopicManager';
export * from './managers/UserManager';

// Structures
Expand All @@ -49,7 +49,7 @@ export * from './structures/CacheCollection';
export * from './structures/Client';
export * from './structures/Doc';
export * from './structures/Group';
export * from './structures/ForumTopic';
export * from './structures/forum/ForumTopic';
export * from './structures/User';
export * from './structures/Webhook';

Expand Down
160 changes: 160 additions & 0 deletions packages/guilded.ts/src/managers/forum/ForumTopicCommentManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { BaseManager, FetchManyOptions, FetchOptions } from '../BaseManager';
import { RESTGetForumTopicCommentsResult, APIForumTopicComment } from 'guilded-api-typings';
import { ForumTopicComment } from '../../structures/forum/ForumTopicComment';
import { ForumChannel } from '../../structures/channel/ForumChannel';
import {
APIForumTopic,
APIForumTopicSummary,
RESTPostForumTopicCommentJSONBody,
RESTPatchForumTopicCommentJSONBody,
} from 'guilded-api-typings';
import { Collection } from '@discordjs/collection';

/**
* The manager for forum topic comments
*/
export class ForumTopicCommentManager extends BaseManager<number, ForumTopicComment> {
/**
* @param forumTopic The forum topic
*/
constructor(
public readonly channel: ForumChannel,
public readonly forumTopic: APIForumTopic | APIForumTopicSummary,
) {
super(channel.client, channel.client.options.maxForumTopicCommentCache);
}
/**
* Fetch the forum topic comment
* @param forumTopicComment The ID of the forum topic comment
* @param options The options to fetch the forum topic comments with
* @returns The fetched forum topic comments
*/
fetch(
forumTopicComment: number | ForumTopicComment,
options?: FetchOptions,
): Promise<ForumTopicComment>;
/**
* Fetch the forum topic comment
* @param forumTopicComment The forum topic comment
* @param options The options to fetch the forum topic comments with
* @returns The fetched forum topic comments
*/
fetch(
options?: ForumTopicCommentFetchManyOptions,
): Promise<Collection<number, ForumTopicComment>>;
fetch(
arg1?: number | ForumTopicComment | ForumTopicCommentFetchManyOptions,
arg2?: FetchOptions,
) {
if (typeof arg1 === 'number' || arg1 instanceof ForumTopicComment)
return this.fetchSingle(arg1, arg2);
return this.fetchMany(arg1);
}

private async fetchSingle(
forumTopicComment: number | ForumTopicComment,
options?: FetchOptions,
) {
const forumTopic = this.forumTopic.id;
forumTopicComment =
forumTopicComment instanceof ForumTopicComment
? forumTopicComment.id
: forumTopicComment;
const cached = this.cache.get(forumTopicComment);
if (cached && !options?.force) return cached;
const raw = await this.client.api.forumTopicComments.fetch(
this.channel.id,
forumTopic,
forumTopicComment,
);
const topic = await this.channel.topics.fetch(forumTopic);
return new ForumTopicComment(
this.channel,
topic,
Array.isArray(raw) ? raw[0] : raw,
options?.cache,
);
}

private async fetchMany(options?: ForumTopicCommentFetchManyOptions) {
const forumTopic = this.forumTopic.id;
const raw = (await this.client.api.forumTopicComments.fetch(
this.channel.id,
forumTopic,
)) as unknown as APIForumTopicComment[];
const topic = await this.channel.topics.fetch(forumTopic);
const forumComments = new Collection<number, ForumTopicComment>();
for (const data of raw) {
const forumTopicComment = new ForumTopicComment(
this.channel,
topic,
data,
options?.cache,
);
forumComments.set(forumTopicComment.id, forumTopicComment);
}
return forumComments;
}

/**
* Create a forum topic comment in the forum topic
* @param options The options to create the forum topic with
* @returns The created forum topic
*/
async create(options: RESTPostForumTopicCommentJSONBody) {
const raw = await this.client.api.forumTopicComments.create(
this.channel.id,
this.forumTopic.id,
options,
);
const topic = await this.channel.topics.fetch(this.forumTopic.id);
return new ForumTopicComment(this.channel, topic, raw);
}

/**
* Update a forum topic comment in the forum topic
* @param forumTopicComment The forum topic comment
* @param options The options to update the forum topic with
* @returns The updated forum topic
*/
async update(
forumTopicComment: number | ForumTopicComment,
options: RESTPatchForumTopicCommentJSONBody,
) {
forumTopicComment =
forumTopicComment instanceof ForumTopicComment
? forumTopicComment.id
: forumTopicComment;
const raw = await this.client.api.forumTopicComments.edit(
this.channel.id,
this.forumTopic.id,
forumTopicComment,
options,
);
const topic = await this.channel.topics.fetch(this.forumTopic.id);
return new ForumTopicComment(this.channel, topic, raw);
}

/**
* Delete a forum topic comment from the channel
* @param forumTopic The forum topic
*/
delete(forumTopicComment: number | ForumTopicComment) {
forumTopicComment =
forumTopicComment instanceof ForumTopicComment
? forumTopicComment.id
: forumTopicComment;
return this.client.api.forumTopicComments.delete(
this.channel.id,
this.forumTopic.id,
forumTopicComment,
);
}
}

/**
* The options for fetching forum topics
*/
export interface ForumTopicCommentFetchManyOptions
extends FetchManyOptions,
RESTGetForumTopicCommentsResult {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { BaseManager, FetchManyOptions } from '../BaseManager';
import { RESTGetForumTopicCommentsResult } from 'guilded-api-typings';
import { ForumTopicComment } from '../../structures/forum/ForumTopicComment';
import { ForumTopicCommentReaction } from '../../structures/forum/ForumTopicCommentReaction';
import { ForumTopicCommentReactionCollector } from '../../collectors/ForumTopicCommentReactionCollector';
import { CollectorOptions } from '../../collectors/Collector';
import { Collection } from '@discordjs/collection';

/**
* The manager for forum topic comment reactions
*/
export class ForumTopicCommentReactionManager extends BaseManager<
number,
ForumTopicCommentReaction
> {
/**
* @param forumTopicComment The forum topic comment
*/
constructor(public readonly forumTopicComment: ForumTopicComment) {
super(forumTopicComment.client, forumTopicComment.client.options.maxMessageReactionCache);
}

/**
* Add a reaction to the forum topic comment
* @param emojiId The ID of the emoji
*/
add(emojiId: number) {
return this.client.api.forumTopicCommentReactions.create(
this.forumTopicComment.channel.id,
this.forumTopicComment.forumTopic.id,
this.forumTopicComment.id,
emojiId,
);
}

/**
* Remove a reaction from the forum topic comment
* @param emojiId The ID of the emoji
*/
remove(emojiId: number) {
return this.client.api.forumTopicCommentReactions.delete(
this.forumTopicComment.channel.id,
this.forumTopicComment.forumTopic.id,
this.forumTopicComment.id,
emojiId,
);
}

/**
* Create a reaction collector for the forum topic comment
* @param options The options for the forum topic comment reaction collector
* @returns The created forum topic comment reaction collector
* @example
* const collector = comment.reactions.createCollector({ time: 15 * 1000 });
*
* collector.on('end', (reactions) => console.log(`Collected ${reactions.size} reactions!`));
*/
createCollector(options?: CollectorOptions<ForumTopicCommentReaction>) {
return new ForumTopicCommentReactionCollector(this, options);
}

/**
* Similar to {@link createCollector} but in promise form
* @param options The options for the forum topic comment reaction collector
* @returns The collected forum topic comment reactions
* @example
* const reactions = await comment.reactions.awaitReactions({ time: 15 * 1000 });
*
* console.log(`Collected ${reactions.size} reactions!`);
*/
awaitReactions(options?: CollectorOptions<ForumTopicCommentReaction>) {
return new Promise<Collection<number, ForumTopicCommentReaction>>((resolve) =>
this.createCollector(options).once('end', resolve),
);
}
}

/**
* The options for fetching forum topics
*/
export interface ForumTopicCommentFetchManyOptions
extends FetchManyOptions,
RESTGetForumTopicCommentsResult {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseManager, FetchManyOptions, FetchOptions } from './BaseManager';
import { ForumChannel } from '../structures/channel/ForumChannel';
import { ForumTopic } from '../structures/ForumTopic';
import { BaseManager, FetchManyOptions, FetchOptions } from '../BaseManager';
import { ForumChannel } from '../../structures/channel/ForumChannel';
import { ForumTopic } from '../../structures/forum/ForumTopic';
import {
RESTPatchForumTopicJSONBody,
RESTGetForumTopicsQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class MessageReactionManager extends BaseManager<number, MessageReaction>
* @param options The options for the message reaction collector
* @returns The collected message reactions
* @example
* const reactions = await message.reactions.await({ time: 15 * 1000 });
* const reactions = await message.reactions.awaitReactions({ time: 15 * 1000 });
*
* console.log(`Collected ${reactions.size} reactions!`);
*/
Expand Down
51 changes: 50 additions & 1 deletion packages/guilded.ts/src/structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import { MessageReaction } from './message/MessageReaction';
import { Channel } from './channel/Channel';
import { Collection } from '@discordjs/collection';
import { CalendarEventRsvp } from './calendarEvent/CalendarEventRsvp';
import { ForumTopic } from './ForumTopic';
import { ForumTopic } from './forum/ForumTopic';
import { ForumTopicComment } from './forum/ForumTopicComment';
import { ForumTopicCommentReaction } from './forum/ForumTopicCommentReaction';

/**
* The main hub for interacting with the Guilded API
Expand Down Expand Up @@ -334,6 +336,29 @@ export interface ClientEvents {
* Emitted whenever a forum topic is unlocked
*/
forumTopicUnlock: [forumTopic: ForumTopic];
/**
* Emitted whenever a forum topic comment is created
*/
forumTopicCommentCreate: [forumTopicComment: ForumTopicComment];
/**
* Emitted whenever a forum topic comment is edited
*/
forumTopicCommentEdit: [
newForumTopicComment: ForumTopicComment,
oldForumTopicComment?: ForumTopicComment,
];
/**
* Emitted whenever a forum topic comment is deleted
*/
forumTopicCommentDelete: [forumTopicComment: ForumTopicComment];
/**
* Emitted whenever a forum topic comment reaction is added
*/
forumTopicCommentReactionAdd: [forumTopicCommentReaction: ForumTopicCommentReaction];
/**
* Emitted whenever a forum topic comment reaction is removed
*/
forumTopicCommentReactionRemove: [forumTopicCommentReaction: ForumTopicCommentReaction];
/**
* Emitted whenever a calendar event RSVP is edited
*/
Expand Down Expand Up @@ -510,6 +535,30 @@ export interface ClientOptions {
* Whether to dispose cached forum topics
*/
disposeCachedForumTopics?: boolean;
/**
* Whether to cache forum topic comments
*/
cacheForumTopicComments?: boolean;
/**
* The max cache size for forum topic comments
*/
maxForumTopicCommentCache?: number;
/**
* Whether to dispose cached forum topic comments
*/
disposeCachedForumTopicComments?: boolean;
/**
* Whether to cache forum topic comment reactions
*/
cacheForumTopicCommentReactions?: boolean;
/**
* The max cache size for forum topic reactions
*/
maxForumTopicCommentReactionsCache?: number;
/**
* Whether to dispose cached forum topic reactions
*/
disposeCachedForumTopicCommentReactions?: boolean;
/**
* Whether to cache list items
*/
Expand Down
Loading