Skip to content

Commit 2e4d68f

Browse files
committed
fix: check if user is allowed to use /reopen and /close
1 parent 65d073e commit 2e4d68f

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/commands/util/close.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { config } from "@lib/config.js";
22

33
import {
4-
canUserInteractWithThread,
4+
canMemberInteractWithThread,
55
getChannelFromInteraction,
66
isHelpPost,
77
} from "@lib/channels.js";
@@ -15,7 +15,7 @@ import {
1515

1616
// TODO: find a better way to do this
1717
const getStateWord = (close) => (close ? "closed" : "reopened");
18-
const getStateVerb = (close) => (close ? "close" : "reopene");
18+
const getStateVerb = (close) => (close ? "close" : "reopen");
1919

2020
export async function handleIssueState(
2121
interaction: ChatInputCommandInteraction,
@@ -85,11 +85,10 @@ export async function handleIssueStateCommand(
8585

8686
// Check if thread is a help post and if user can interact
8787
if (await isHelpPost(interactionChannel)) {
88+
const member = await interaction.guild.members.fetch(interaction.user.id);
89+
8890
if (
89-
canUserInteractWithThread(
90-
interaction.channel as ThreadChannel,
91-
interaction.user,
92-
)
91+
await canMemberInteractWithThread(interaction.channel as ThreadChannel, member)
9392
) {
9493
return handleIssueState(interaction, close, lock);
9594
} else {

src/lib/channels.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import {
44
type ChatInputCommandInteraction,
55
ChannelType,
66
type ThreadChannel,
7-
type User,
87
type GuildTextBasedChannel,
8+
PermissionsBitField,
9+
type GuildMember,
910
} from "discord.js";
1011

1112
export async function getChannelFromInteraction(
@@ -36,14 +37,18 @@ export async function isHelpPost(channel: GuildTextBasedChannel) {
3637
);
3738
}
3839

39-
export async function canUserInteractWithThread(
40+
export async function canMemberInteractWithThread(
4041
channel: ThreadChannel,
41-
user: User,
42+
member: GuildMember,
4243
) {
43-
/*const threadChannel: ThreadChannel<true> = await interaction.client.channels.fetch(interaction.channelId) as ThreadChannel;
44-
45-
const firstMessage = await threadChannel.fetchStarterMessage();*/
46-
47-
// TODO: actually check
48-
return true;
44+
if (member.permissions.has(PermissionsBitField.Flags.ManageChannels)) {
45+
return true;
46+
} else {
47+
// Sometimes fetchOwner() will fail, so this is just a failsafe
48+
const owner =
49+
(await channel.fetchOwner())?.guildMember ??
50+
(await channel.fetchStarterMessage()).member;
51+
52+
return member.id === owner.id;
53+
}
4954
}

0 commit comments

Comments
 (0)