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
160
161
162
163
164
165
|
import { Message } from "discord.js";
import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities";
import { ROLEPLAY_GUILD_ID } from "../constants";
const RACECOURSES_CATEGORY_ID = "1428169113754402836";
const RACE_PLANNER_ROLE_ID = "1428173099270148173";
export const handleVerbalGatesCommand = async (
message: Message,
): Promise<boolean> => {
if (message.author.bot) return false;
const botMentioned = message.mentions.users.has(
message.client.user?.id || "",
);
if (!botMentioned) return false;
if (message.guildId !== ROLEPLAY_GUILD_ID) return false;
const content = message.content.toLowerCase();
const openMatch = content.match(/open\s+<#(\d+)>/);
const closeMatch = content.match(/close\s+<#(\d+)>/);
const toggleMatch = content.match(/toggle\s+<#(\d+)>/);
const action = openMatch
? "open"
: closeMatch
? "close"
: toggleMatch
? "toggle"
: null;
const channelId = openMatch?.[1] || closeMatch?.[1] || toggleMatch?.[1];
if (!action || !channelId) return false;
const application = await message.client.application?.fetch();
const ownerId = application?.owner?.id;
const guildOwnerId = message.guild?.ownerId;
const hasAdminPermission =
message.member?.permissions.has("Administrator") ?? false;
const hasOwnerPermission =
message.member?.permissions.has("CreateInstantInvite") ?? false;
const hasRacePlannerRole =
message.member?.roles.cache.has(RACE_PLANNER_ROLE_ID);
const isBotOwner = message.author.id === ownerId;
const isGuildOwner = message.author.id === guildOwnerId;
if (
!hasAdminPermission &&
!hasOwnerPermission &&
!hasRacePlannerRole &&
!isBotOwner &&
!isGuildOwner
) {
await replyWithCleanup(
message,
"❌ You don't have permission to use this command. Only owners, administrators, or Race Planners can use this command.",
);
return true;
}
const targetChannel = message.client.channels.cache.get(channelId);
if (
!targetChannel ||
!targetChannel.isTextBased() ||
targetChannel.isThread()
) {
await replyWithCleanup(
message,
"❌ Channel not found, is not a text channel, or is a thread.",
);
return true;
}
if (
!("parentId" in targetChannel) ||
targetChannel.parentId !== RACECOURSES_CATEGORY_ID
) {
await replyWithCleanup(
message,
"❌ The specified channel is not a racecourse.",
);
return true;
}
try {
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
const everyoneRoleId = message.guild?.roles.everyone.id!;
const canCurrentlySendMessages =
targetChannel.permissionsFor(everyoneRoleId)?.has("SendMessages") ?? true;
const canCurrentlySendInThreads =
targetChannel
.permissionsFor(everyoneRoleId)
?.has("SendMessagesInThreads") ?? true;
const gatesCurrentlyOpen =
canCurrentlySendMessages && canCurrentlySendInThreads;
let actualAction = action;
if (action === "toggle")
actualAction = gatesCurrentlyOpen ? "close" : "open";
if (action !== "toggle") {
const requestedStateMatches =
(action === "open" && gatesCurrentlyOpen) ||
(action === "close" && !gatesCurrentlyOpen);
if (requestedStateMatches) {
const channelMention = `<#${targetChannel.id}>`;
const currentState = gatesCurrentlyOpen ? "open" : "closed";
await replyWithCleanup(
message,
`ℹ️ The gates for ${channelMention} are already ${currentState}.`,
);
try {
await message.react("✅");
} catch (error) {
logUnexpectedDiscordAPIError(error);
}
return true;
}
}
if (actualAction === "close") {
await targetChannel.permissionOverwrites.edit(everyoneRoleId, {
SendMessages: false,
SendMessagesInThreads: false,
});
} else if (actualAction === "open") {
await targetChannel.permissionOverwrites.edit(everyoneRoleId, {
SendMessages: true,
SendMessagesInThreads: true,
});
}
const channelMention = `<#${targetChannel.id}>`;
await replyWithCleanup(
message,
`✅ I've successfully ${actualAction === "close" ? "closed" : "opened"} the gates for ${channelMention}.`,
);
try {
await message.react("✅");
} catch (error) {
logUnexpectedDiscordAPIError(error);
}
return true;
} catch (error) {
logUnexpectedDiscordAPIError(error);
await replyWithCleanup(
message,
"❌ An error occurred while managing channel permissions.",
);
return true;
}
};
|