all repos — stealth-developers @ f848a995c217b0c37ef4591eb802988a808c0e4d

src/interactions/commands/codes.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
import config from "@/config";
import {
	ActionRowBuilder,
	ButtonBuilder,
	type ButtonInteraction,
	ButtonStyle,
	type ChatInputCommandInteraction,
	type Client,
	ContainerBuilder,
	SlashCommandBuilder,
	TextDisplayBuilder,
} from "discord.js";

import { getProjectChoices } from "@/utils/choices";
import { formatTime } from "@/utils/time";

const commandData = new SlashCommandBuilder()
	.setName("codes")
	.setDescription(`get the codes for the associated ${config.data.terminology}`)
	.addStringOption((option) =>
		option
			.setName(config.data.terminology)
			.setDescription("the project to get the codes for")
			.setRequired(true)
			.addChoices(getProjectChoices()),
	)
	.addUserOption((option) =>
		option
			.setName("user")
			.setDescription("the user to send the codes to")
			.setRequired(false),
	);

async function execute(
	_client: Client,
	interaction: ChatInputCommandInteraction,
) {
	const projectKey = interaction.options.getString(
		config.data.terminology,
		true,
	);
	const project = config.data.projects[projectKey];

	if (!project) {
		return interaction.reply({
			content: `couldn't find data for ${config.data.terminology} "${projectKey}"`,
			flags: ["Ephemeral"],
		});
	}

	const codes = project.codes || [];
	const container = new ContainerBuilder();
	const user = interaction.options.getUser("user");
	const p2 = user ? `• <@${user.id}>` : "";

	if (codes.length === 0) {
		const textComponents = [
			new TextDisplayBuilder().setContent(
				`# no codes for ${project.displayName}`,
			),
			new TextDisplayBuilder().setContent(
				`there aren't any known codes for this ${config.data.terminology} at the moment`,
			),
		];

		const footerComponent = new TextDisplayBuilder().setContent(
			`please tell <@${config.data.developerId}> if this is incorrect ${p2}`,
		);

		container.addTextDisplayComponents(textComponents);
		container.addTextDisplayComponents(footerComponent);
		return interaction.reply({
			components: [container],
			flags: ["IsComponentsV2"],
			...(user
				? { allowedMentions: { users: [user?.id] } }
				: { allowedMentions: { users: [] } }),
		});
	}

	{
		const codeButtons = codes.map((code) =>
			new ButtonBuilder()
				.setCustomId(`codes:${projectKey}:${code.code}`)
				.setLabel(code.code)
				.setStyle(code.expired ? ButtonStyle.Danger : ButtonStyle.Success),
		);

		const buttonRows: ActionRowBuilder<ButtonBuilder>[] = [];
		for (let i = 0; i < codeButtons.length; i += 4) {
			const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
				codeButtons.slice(i, i + 4),
			);
			buttonRows.push(row);
		}

		const textComponent = new TextDisplayBuilder().setContent(
			`# codes for ${project.displayName}`,
		);

		const footerComponent = new TextDisplayBuilder().setContent(
			`please tell <@${config.data.developerId}> if any any missing or expired codes ${p2}`,
		);

		container.addTextDisplayComponents(textComponent);
		container.addActionRowComponents(buttonRows);
		container.addTextDisplayComponents(footerComponent);
	}

	return interaction.reply({
		components: [container],
		flags: ["IsComponentsV2"],
		...(user
			? { allowedMentions: { users: [user?.id] } }
			: { allowedMentions: { users: [] } }),
	});
}

async function buttonExecute(client: Client, interaction: ButtonInteraction) {
	const [, project, code] = interaction.customId.split(":");
	if (!project || !code) {
		return interaction.reply({
			content: `invalid code button interaction, missing either ${config.data.terminology} or code`,
			flags: ["Ephemeral"],
		});
	}

	const projectData = config.data.projects[project];
	if (!projectData) {
		return interaction.reply({
			content: `couldn't find data for ${config.data.terminology} "${project}"`,
			flags: ["Ephemeral"],
		});
	}

	const codeData = projectData.codes?.find((c) => c.code === code);
	if (!codeData) {
		return interaction.reply({
			content: `couldn't find code "${code}" for ${config.data.terminology} "${project}"`,
			flags: ["Ephemeral"],
		});
	}

	return interaction.reply({
		content: `\`${codeData.code}\``,
		flags: ["Ephemeral"],
		components: [
			new ActionRowBuilder<ButtonBuilder>().addComponents(
				new ButtonBuilder()
					.setCustomId("disabled:0")
					.setLabel(codeData.expired ? "expired" : "active")
					.setStyle(codeData.expired ? ButtonStyle.Danger : ButtonStyle.Success)
					.setDisabled(true),
				new ButtonBuilder()
					.setCustomId("disabled:1")
					.setLabel(
						codeData.addedAt
							? `added at ${formatTime(codeData.addedAt)}`
							: "added at unknown time",
					)
					.setStyle(ButtonStyle.Secondary)
					.setDisabled(true),
			),
		],
	});
}

export default {
	data: commandData,
	execute,
	buttonExecute,
};