all repos — stealth-developers @ 837b1ab337220d1cdaeacd59eb4b730ef8c92472

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

const logger = new Logger("config-command");

const commandData = new SlashCommandBuilder()
	.setName("config")
	.setDescription("configure bot settings for this server")
	.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild)
	.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),
			),
	);

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;
	}

	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);
		}
	} 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"],
	});
}

export default {
	data: commandData,
	execute,
};