src/interactions/commands/config.ts (view raw)
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
import { SlashCommandBuilder } from "discord.js";
import type {
AutocompleteInteraction,
ChatInputCommandInteraction,
Client,
GuildMember,
} from "discord.js";
import { ChannelType, type GuildChannel, type Role } from "discord.js";
import cfg from "@/config";
import { db, guild as guildTable, manager as managerTable } from "@/database";
import { getGuild } from "@/database/queries";
import { hasManagerPermissions } from "@/utils/discord/permissions";
import { loggers } from "@/utils/logging";
import { eq } from "drizzle-orm";
const logger = loggers.interactions.child({ name: "configCommand" });
const CONFIG_KEYS = [
"suggestion_forum_id",
"bug_channel_id",
"highlights_channel_id",
"commands_channel_id",
"ticket_channel_id",
"ticket_category_id",
"ticket_message",
] as const;
function isChannelKey(key: string) {
return key.endsWith("_id");
}
const commandData = new SlashCommandBuilder()
.setName("config")
.setDescription("configure bot settings for this server")
.addSubcommand((sub) =>
sub
.setName("manager-role")
.setDescription("manage roles that can manage reports/tickets")
.addStringOption((opt) =>
opt
.setName("action")
.setDescription("action to perform")
.setRequired(true)
.addChoices(
{ name: "add", value: "add" },
{ name: "remove", value: "remove" },
{ name: "list", value: "list" },
),
)
.addRoleOption((opt) => opt.setName("role").setDescription("role to add/remove")),
)
.addSubcommand((sub) =>
sub
.setName("set")
.setDescription("set a guild configuration key")
.addStringOption((opt) =>
opt
.setName("key")
.setDescription("configuration key to set")
.setRequired(true)
.setAutocomplete(true),
)
.addChannelOption((opt) =>
opt
.setName("channel")
.setDescription("channel to set (for channel keys)")
.setRequired(false),
)
.addStringOption((opt) =>
opt
.setName("value")
.setDescription("value to set (for non-channel keys)")
.setRequired(false),
),
)
.addSubcommand((sub) => sub.setName("view").setDescription("view current server configuration"));
async function autocomplete(interaction: AutocompleteInteraction) {
const focused = interaction.options.getFocused(true);
if (focused.name === "key") {
const input = String(focused.value).toLowerCase();
const suggestions = CONFIG_KEYS.filter((k) => k.toLowerCase().includes(input)).slice(0, 25);
await interaction.respond(suggestions.map((s) => ({ name: s, value: s })));
}
}
async function execute(_client: Client, interaction: ChatInputCommandInteraction) {
if (!interaction.guild || !interaction.member) {
await interaction.reply({
content: "❌ this command can only be used in a server.",
flags: ["Ephemeral"],
});
return;
}
const isDev = interaction.user.id === cfg.developerId;
const isManager = hasManagerPermissions(interaction.member as GuildMember);
if (!(isDev || isManager)) {
await interaction.reply({
content: "❌ You do not have permission to manage this server.",
flags: ["Ephemeral"],
});
return;
}
const sub = interaction.options.getSubcommand();
try {
if (sub === "manager-role") {
await handleManagerRole(interaction);
} else if (sub === "set") {
await handleSet(interaction);
} else if (sub === "view") {
await handleView(interaction);
} else {
await interaction.reply({
content: "❌ Unknown subcommand.",
flags: ["Ephemeral"],
});
}
} catch (err) {
logger.error(err, "Failed to execute config command");
await interaction.reply({
content: "❌ Failed to update configuration, please try again later.",
flags: ["Ephemeral"],
});
}
}
async function handleManagerRole(interaction: ChatInputCommandInteraction) {
if (!interaction.guild || !interaction.member) {
await interaction.reply({
content: "❌ This command can only be used in a server.",
flags: ["Ephemeral"],
});
return;
}
const action = interaction.options.getString("action", true);
const role = interaction.options.getRole("role") as Role | null;
const guildId = interaction.guild.id;
const isDeveloper = interaction.user.id === cfg.developerId;
const hasPerms = await hasManagerPermissions(interaction.member as GuildMember);
if (!isDeveloper && !hasPerms) {
await interaction.reply({
content: "❌ You do not have manager permissions to run this action.",
flags: ["Ephemeral"],
});
return;
}
if (action === "list") {
const rows = await db
.select({ roleId: managerTable.roleId })
.from(managerTable)
.where(eq(managerTable.guildId, guildId))
.execute();
if (!rows || rows.length === 0) {
await interaction.reply({
content: "📋 There are no manager roles configured for this server.",
flags: ["Ephemeral"],
});
return;
}
const mentions = rows.map((r) => `\* <@&${r.roleId}>`).join("\n");
await interaction.reply({
content: `**📋 Manager roles**\n${mentions}`,
flags: ["Ephemeral"],
});
return;
}
if (!role) {
await interaction.reply({
content: "❌ You must specify a role for this action.",
flags: ["Ephemeral"],
});
return;
}
if (action === "add") {
const existing = await db
.select()
.from(managerTable)
.where(eq(managerTable.roleId, role.id))
.execute();
if (existing && existing.length > 0) {
await interaction.reply({
content: `❌ ${role} is already configured as a manager role.`,
flags: ["Ephemeral"],
});
return;
}
await db
.insert(managerTable)
.values({
roleId: role.id,
guildId,
})
.execute();
await interaction.reply({
content: `✅ Added ${role} as a manager role.`,
flags: ["Ephemeral"],
});
return;
}
if (action === "remove") {
await db.delete(managerTable).where(eq(managerTable.roleId, role.id)).execute();
await interaction.reply({
content: `✅ Removed ${role} from manager roles.`,
flags: ["Ephemeral"],
});
return;
}
await interaction.reply({
content: "❌ unknown action.",
flags: ["Ephemeral"],
});
}
async function handleSet(interaction: ChatInputCommandInteraction) {
if (!interaction.guild) {
await interaction.reply({
content: "❌ This command can only be used in a server.",
flags: ["Ephemeral"],
});
return;
}
const key = interaction.options.getString("key", true);
// @ts-expect-error shush
if (!CONFIG_KEYS.includes(key)) {
await interaction.reply({
content: "❌ Unknown configuration key.",
flags: ["Ephemeral"],
});
return;
}
const guildId = interaction.guild.id;
await getGuild(guildId);
if (isChannelKey(key)) {
const channel = interaction.options.getChannel("channel", false) as GuildChannel | null;
if (!channel) {
await interaction.reply({
content: "❌ this key expects a channel - please provide a channel.",
flags: ["Ephemeral"],
});
return;
}
const allowedTypes = [
ChannelType.GuildText,
ChannelType.GuildAnnouncement,
ChannelType.GuildCategory,
];
if (!allowedTypes.includes(channel.type)) {
await interaction.reply({
content: "❌ Channel must be a text channel or category.",
flags: ["Ephemeral"],
});
return;
}
await db
.update(guildTable)
.set({ [key]: channel.id })
.where(eq(guildTable.guildId, guildId))
.execute();
await interaction.reply({
content: `✅ Set \`${key}\` to <#${channel.id}>.`,
flags: ["Ephemeral"],
});
return;
}
const value = interaction.options.getString("value", true);
await db
.update(guildTable)
.set({ [key]: value })
.where(eq(guildTable.guildId, guildId))
.execute();
await interaction.reply({
content: `✅ set ${key} to \`${value}\`.`,
flags: ["Ephemeral"],
});
}
async function handleView(interaction: ChatInputCommandInteraction) {
if (!interaction.guild) {
await interaction.reply({
content: "❌ This command can only be used in a server.",
flags: ["Ephemeral"],
});
return;
}
const guildId = interaction.guild.id;
const result = await db
.select()
.from(guildTable)
.where(eq(guildTable.guildId, guildId))
.execute();
if (!result || result.length === 0) {
await interaction.reply({
content: "📋 No configuration found for this server.",
flags: ["Ephemeral"],
});
return;
}
const row = result[0] as Record<string, string>;
const lines = CONFIG_KEYS.map((k) => {
const val = row[k];
if (!val) return `- \`${k}\` - unset`;
if (isChannelKey(k)) return `- \`${k}\` <#${val}>`;
return `- \`${k}\` ${val}`;
});
await interaction.reply({
content: `# 📋 Server Configuration\n${lines.join("\n")}`,
flags: ["Ephemeral"],
});
}
export default {
data: commandData,
execute,
autocomplete,
};
|