|
1 | 1 | import { ChannelType, ForumChannel } from 'discord.js'; |
2 | | -import { GetForumChannelsSchema, CreateForumPostSchema, GetForumPostSchema, ReplyToForumSchema, DeleteForumPostSchema } from '../schemas.js'; |
| 2 | +import { GetForumChannelsSchema, CreateForumPostSchema, GetForumPostSchema, ListForumThreadsSchema, ReplyToForumSchema, DeleteForumPostSchema } from '../schemas.js'; |
3 | 3 | import { ToolHandler } from './types.js'; |
4 | 4 | import { handleDiscordError } from "../errorHandler.js"; |
5 | 5 |
|
@@ -145,6 +145,99 @@ export const getForumPostHandler: ToolHandler = async (args, { client }) => { |
145 | 145 | } |
146 | 146 | }; |
147 | 147 |
|
| 148 | +export const listForumThreadsHandler: ToolHandler = async (args, { client }) => { |
| 149 | + const { forumChannelId, includeArchived, limit } = ListForumThreadsSchema.parse(args); |
| 150 | + |
| 151 | + try { |
| 152 | + if (!client.isReady()) { |
| 153 | + return { |
| 154 | + content: [{ type: "text", text: "Discord client not logged in." }], |
| 155 | + isError: true |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + const channel = await client.channels.fetch(forumChannelId); |
| 160 | + if (!channel || channel.type !== ChannelType.GuildForum) { |
| 161 | + return { |
| 162 | + content: [{ type: "text", text: `Channel ID ${forumChannelId} is not a forum channel.` }], |
| 163 | + isError: true |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + const forumChannel = channel as ForumChannel; |
| 168 | + |
| 169 | + // Fetch active threads |
| 170 | + const activeThreads = await forumChannel.threads.fetchActive(); |
| 171 | + |
| 172 | + // Fetch archived threads if requested |
| 173 | + let archivedThreads: typeof activeThreads | null = null; |
| 174 | + if (includeArchived) { |
| 175 | + archivedThreads = await forumChannel.threads.fetchArchived({ limit: limit }); |
| 176 | + } |
| 177 | + |
| 178 | + // Combine and format thread information |
| 179 | + const threads: Array<{ |
| 180 | + id: string; |
| 181 | + name: string; |
| 182 | + createdAt: Date | null; |
| 183 | + archived: boolean; |
| 184 | + locked: boolean; |
| 185 | + messageCount: number | null; |
| 186 | + ownerId: string | null; |
| 187 | + }> = []; |
| 188 | + |
| 189 | + // Add active threads |
| 190 | + activeThreads.threads.forEach(thread => { |
| 191 | + threads.push({ |
| 192 | + id: thread.id, |
| 193 | + name: thread.name, |
| 194 | + createdAt: thread.createdAt, |
| 195 | + archived: thread.archived || false, |
| 196 | + locked: thread.locked || false, |
| 197 | + messageCount: thread.messageCount, |
| 198 | + ownerId: thread.ownerId |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + // Add archived threads if fetched |
| 203 | + if (archivedThreads) { |
| 204 | + archivedThreads.threads.forEach(thread => { |
| 205 | + // Avoid duplicates |
| 206 | + if (!threads.find(t => t.id === thread.id)) { |
| 207 | + threads.push({ |
| 208 | + id: thread.id, |
| 209 | + name: thread.name, |
| 210 | + createdAt: thread.createdAt, |
| 211 | + archived: thread.archived || false, |
| 212 | + locked: thread.locked || false, |
| 213 | + messageCount: thread.messageCount, |
| 214 | + ownerId: thread.ownerId |
| 215 | + }); |
| 216 | + } |
| 217 | + }); |
| 218 | + } |
| 219 | + |
| 220 | + // Sort by creation date (newest first) |
| 221 | + threads.sort((a, b) => { |
| 222 | + if (!a.createdAt || !b.createdAt) return 0; |
| 223 | + return b.createdAt.getTime() - a.createdAt.getTime(); |
| 224 | + }); |
| 225 | + |
| 226 | + return { |
| 227 | + content: [{ |
| 228 | + type: "text", |
| 229 | + text: JSON.stringify({ |
| 230 | + forumChannelId, |
| 231 | + totalThreads: threads.length, |
| 232 | + threads |
| 233 | + }, null, 2) |
| 234 | + }] |
| 235 | + }; |
| 236 | + } catch (error) { |
| 237 | + return handleDiscordError(error); |
| 238 | + } |
| 239 | +}; |
| 240 | + |
148 | 241 | export const replyToForumHandler: ToolHandler = async (args, { client }) => { |
149 | 242 | const { threadId, message } = ReplyToForumSchema.parse(args); |
150 | 243 |
|
|
0 commit comments