Skip to content

Commit 87b4e21

Browse files
fix: Add permission check for managing private threads
1 parent 2858ff3 commit 87b4e21

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/utils/channel-helper.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { type ThreadManager, Collection, type GuildBasedChannel, type TextBasedChannel, ChannelType } from "discord.js";
1+
import {
2+
type ThreadManager,
3+
Collection,
4+
type GuildBasedChannel,
5+
type TextBasedChannel,
6+
ChannelType,
7+
PermissionsBitField,
8+
} from "discord.js";
29
import logger from "../logger";
310

411
export async function getAllTextChannels(
@@ -50,8 +57,16 @@ async function fetchAndStoreThreads(
5057
textChannels.set(thread.id, thread);
5158
});
5259

53-
const privateArchivedChannelThreads = await threadManager.fetchArchived({ fetchAll: true, type: "private" }, false);
54-
privateArchivedChannelThreads.threads.forEach((thread) => {
55-
textChannels.set(thread.id, thread);
56-
});
60+
const botPermissionInChannel = threadManager.channel.permissionsFor(threadManager.client.user!);
61+
if (botPermissionInChannel !== null && botPermissionInChannel.has(PermissionsBitField.Flags.ManageThreads)) {
62+
const privateArchivedChannelThreads = await threadManager.fetchArchived({ fetchAll: true, type: "private" }, false);
63+
privateArchivedChannelThreads.threads.forEach((thread) => {
64+
textChannels.set(thread.id, thread);
65+
});
66+
} else {
67+
logger.info(
68+
"Bot does not have permission to manage threads in channel %o. Private threads in this channel are ignored.",
69+
threadManager.channel.name
70+
);
71+
}
5772
}

0 commit comments

Comments
 (0)