all repos — stealth-developers @ 22bd268c32fa05c64444c6abe2514e95c0ed99cd

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
import config from "@/config.ts";
import { GuildModel, type GuildType } from "@/database/schemas.ts";
import lily from "@/utils/logging.ts";
import {
	ChannelType,
	type ChatInputCommandInteraction,
	type Client,
	PermissionFlagsBits,
	SlashCommandBuilder,
} from "discord.js";

const logger = lily.child("configCommand");

const commandData = new SlashCommandBuilder()
	.setName("config")
	.setDescription("configure bot settings for this server")
	.addSubcommand((subcommand) =>
		subcommand
			.setName("manager-role")
			.setDescription("manage roles that can manage bug reports")
			.addStringOption((option) =>
				option
					.setName("action")
					.setDescription("action to perform")
					.setRequired(true)
					.addChoices(
						{ name: "add", value: "add" },
						{ name: "remove", value: "remove" },
						{ name: "list", value: "list" },
					),
			)
			.addRoleOption((option) =>
				option
					.setName("role")
					.setDescription("role to add/remove")
					.setRequired(false),
			),
	)
	.addSubcommand((subcommand) =>
		subcommand
			.setName("bug-channel")
			.setDescription("set the channel where bug reports are sent")
			.addChannelOption((option) =>
				option
					.setName("channel")
					.setDescription("channel for bug reports")
					.setRequired(true),
			),
	)
	.addSubcommand((subcommand) =>
		subcommand
			.setName("highlight-channel")
			.setDescription("set the channel where highlights are sent")
			.addChannelOption((option) =>
				option
					.setName("channel")
					.setDescription("channel for highlights")
					.setRequired(true),
			),
	);

async function execute(
	_client: Client,
	interaction: ChatInputCommandInteraction,
) {
	if (!interaction.guild) {
		await interaction.reply({
			content: "❌ this command can only be used in a server.",
			flags: ["Ephemeral"],
		});
		return;
	}

	if (
		!interaction.memberPermissions ||
		(!interaction.memberPermissions.has(PermissionFlagsBits.ManageGuild) &&
			interaction.user.id !== config.data.developerId)
	) {
		await interaction.reply({
			content: "❌ you do not have permission to manage this server.",
			flags: ["Ephemeral"],
		});
		return;
	}

	const subcommand = interaction.options.getSubcommand();

	try {
		let guild = await GuildModel.findOne({
			guild_id: interaction.guild.id,
		});

		if (!guild) {
			guild = new GuildModel({
				guild_id: interaction.guild.id,
				manager_roles: [],
			});
			await guild.save();
			logger.info(`created new guild record for ${interaction.guild.id}`);
		}

		if (subcommand === "manager-role") {
			await handleManagerRole(interaction, guild);
		} else if (subcommand === "bug-channel") {
			await handleBugChannel(interaction, guild);
		} else if (subcommand === "highlight-channel") {
			await handleHighlightChannel(interaction, guild);
		} else {
			await interaction.reply({
				content: "❌ unknown subcommand.",
				flags: ["Ephemeral"],
			});
		}
	} catch (error) {
		logger.error("failed to execute config command:", error);
		await interaction.reply({
			content: "❌ failed to update configuration. please try again later.",
			flags: ["Ephemeral"],
		});
	}
}

async function handleManagerRole(
	interaction: ChatInputCommandInteraction,
	guild: GuildType,
) {
	const action = interaction.options.getString("action", true);
	const role = interaction.options.getRole("role");

	if (action === "list") {
		if (guild.manager_roles.length === 0) {
			await interaction.reply({
				content: "📋 no manager roles configured.",
				flags: ["Ephemeral"],
			});
			return;
		}

		const roleList = guild.manager_roles
			.map((roleId: string) => `<@&${roleId}>`)
			.join("\n");

		await interaction.reply({
			content: `📋 **manager roles:**\n${roleList}`,
			flags: ["Ephemeral"],
		});
		return;
	}

	if (!role) {
		await interaction.reply({
			content: "❌ you must specify a role for this action.",
			flags: ["Ephemeral"],
		});
		return;
	}

	if (action === "add") {
		if (guild.manager_roles.includes(role.id)) {
			await interaction.reply({
				content: `❌ ${role} is already a manager role.`,
				flags: ["Ephemeral"],
			});
			return;
		}

		guild.manager_roles.push(role.id);
		await guild.save();

		await interaction.reply({
			content: `✅ added ${role} as a manager role.`,
			flags: ["Ephemeral"],
		});
	} else if (action === "remove") {
		const index = guild.manager_roles.indexOf(role.id);
		if (index === -1) {
			await interaction.reply({
				content: `❌ ${role} is not a manager role.`,
				flags: ["Ephemeral"],
			});
			return;
		}

		guild.manager_roles.splice(index, 1);
		await guild.save();

		await interaction.reply({
			content: `✅ removed ${role} from manager roles.`,
			flags: ["Ephemeral"],
		});
	}
}

async function handleBugChannel(
	interaction: ChatInputCommandInteraction,
	guild: GuildType,
) {
	const channel = interaction.options.getChannel("channel", true);

	if (channel.type !== ChannelType.GuildText) {
		await interaction.reply({
			content: "❌ bug channel must be a text channel.",
			flags: ["Ephemeral"],
		});
		return;
	}

	guild.bug_channel = channel.id;
	await guild.save();

	await interaction.reply({
		content: `✅ set bug reports channel to ${channel}.`,
		flags: ["Ephemeral"],
	});
}

async function handleHighlightChannel(
	interaction: ChatInputCommandInteraction,
	guild: GuildType,
) {
	const channel = interaction.options.getChannel("channel", true);

	const allowedTypes = [ChannelType.GuildText, ChannelType.GuildAnnouncement];
	if (!allowedTypes.includes(channel.type)) {
		await interaction.reply({
			content: "❌ highlight channel must be a text channel.",
			flags: ["Ephemeral"],
		});
		return;
	}

	guild.highlights_channel = channel.id;
	await guild.save();

	await interaction.reply({
		content: `✅ set highlights channel to ${channel}.`,
		flags: ["Ephemeral"],
	});
}

export default {
	data: commandData,
	execute,
};