-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ForumManager.ts
159 lines (145 loc) · 4.96 KB
/
ForumManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Collection } from "@discordjs/collection";
import type { ForumsService } from "@guildedjs/api";
import { ForumTopic, PartialForumTopic } from "../../structures/Forum";
import type { OptionBody, OptionQuery } from "../../typings";
import { CacheableStructManager } from "./CacheableStructManager";
/**
* Manager for interacting with forum topics at a global level.
*/
export class GlobalForumTopicManager extends CacheableStructManager<number, ForumTopic> {
get shouldCacheForumTopic(): boolean {
return this.client.options.cache?.cacheForumTopics !== false;
}
/**
* Create a topic in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param options The options for creating the forum topic.
* @returns A Promise that resolves with the payload of the created forum topic.
*/
async create(channelId: string, options: OptionBody<ForumsService["forumTopicCreate"]>): Promise<ForumTopic> {
const data = await this.client.rest.router.forums.forumTopicCreate({
channelId,
requestBody: options,
});
return new ForumTopic(this.client, data.forumTopic);
}
/**
* Get all topics in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param options The options for filtering the forum topics.
* @returns A Promise that resolves to a Collection of ForumTopics.
*/
async fetchMany(channelId: string, options: Omit<OptionQuery<ForumsService["forumTopicReadMany"]>, "channelId">): Promise<Collection<number, PartialForumTopic>> {
const data = await this.client.rest.router.forums.forumTopicReadMany({
channelId,
...options,
});
const topics = new Collection<number, PartialForumTopic>();
for (const forumTopic of data.forumTopics) {
const newTopic = new PartialForumTopic(this.client, forumTopic);
topics.set(newTopic.id, newTopic);
}
return topics;
}
/**
* Get a topic in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param forumTopicId The ID of the forum topic.
* @returns a Promise that resolves to a ForumTopic.
*/
async fetch(channelId: string, forumTopicId: number): Promise<ForumTopic> {
const data = await this.client.rest.router.forums.forumTopicRead({
channelId,
forumTopicId,
});
return new ForumTopic(this.client, data.forumTopic);
}
/**
* Update a topic in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param forumTopicId The ID of the forum topic.
* @param options The options for updating the forum topic.
* @returns A Promise that resolves to the updated ForumTopic. If cached locally, it will modify that object.
*/
async update(channelId: string, forumTopicId: number, options: OptionBody<ForumsService["forumTopicUpdate"]>): Promise<ForumTopic> {
const data = await this.client.rest.router.forums.forumTopicUpdate({
channelId,
forumTopicId,
requestBody: options,
});
const existingTopic = this.client.topics.cache.get(data.forumTopic.id);
if (existingTopic) return existingTopic._update(data.forumTopic);
const newTopic = new ForumTopic(this.client, data.forumTopic);
this.client.topics.cache.set(newTopic.id, newTopic);
return newTopic;
}
/**
* Delete a topic in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param forumTopicId The ID of the forum topic.
* @returns A Promise that resolves to nothing.
*/
async delete(channelId: string, forumTopicId: number): Promise<void> {
await this.client.rest.router.forums.forumTopicDelete({
channelId,
forumTopicId,
});
}
/**
* Pin a topic in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param forumTopicId The ID of the forum topic.
* @returns A Promise that resolves to nothing.
*/
async pin(channelId: string, forumTopicId: number): Promise<void> {
await this.client.rest.router.forums.forumTopicPin({
channelId,
forumTopicId,
});
}
/**
* Unpin a topic in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param forumTopicId The ID of the forum topic.
* @returns A Promise that resolves to nothing.
*/
async unpin(channelId: string, forumTopicId: number): Promise<void> {
await this.client.rest.router.forums.forumTopicUnpin({
channelId,
forumTopicId,
});
}
/**
* Lock a topic in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param forumTopicId The ID of the forum topic.
* @returns A Promise that resolves to nothing.
*/
async lock(channelId: string, forumTopicId: number): Promise<void> {
await this.client.rest.router.forums.forumTopicLock({
channelId,
forumTopicId,
});
}
/**
* Unlock a topic in a forum.
*
* @param channelId The ID of the channel containing the forum.
* @param forumTopicId The ID
* @returns A Promise that resolves to nothing.
*/
async unlock(channelId: string, forumTopicId: number): Promise<void> {
await this.client.rest.router.forums.forumTopicUnlock({
channelId,
forumTopicId,
});
}
}