all repos — stealth-developers @ e3649499a4f2c6f95902a43a0f171e9df0d40ad1

apps/bot/src/feats/bugs/shared.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
import type { Actor, Bug } from "@stealth-developers/db";

import {
	ActionRowBuilder,
	AttachmentBuilder,
	ButtonBuilder,
	ButtonStyle,
	ContainerBuilder,
	FileUploadBuilder,
	LabelBuilder,
	MediaGalleryBuilder,
	MediaGalleryItemBuilder,
	Message,
	ModalBuilder,
	SectionBuilder,
	StringSelectMenuBuilder,
	TextDisplayBuilder,
	TextInputBuilder,
	TextInputStyle,
	ThumbnailBuilder,
} from "discord.js";

import { bugStatusButton, editButton } from "./buttons";
import { bugModal, editBugModal } from "./modals";

export const Projects = {
	gw: {
		universe: "6583326485",
		name: "ground war",
		displayName: "Ground War",
		iconURL:
			"https://tr.rbxcdn.com/180DAY-08f5294f7b48367cccbe389e705a4060/256/256/Image/Webp/noFilter",
	},
	wft: {
		universe: "21449357",
		name: "warfare tycoon",
		displayName: "Warfare Tycoon",
		iconURL:
			"https://tr.rbxcdn.com/180DAY-3fd1cc01fe04259c77a635662f096c9e/256/256/Image/Webp/noFilter",
		codes: [
			{
				code: "FIRSTCODE",
				expired: true,
				addedAt: "2022-09-03T12:12:00Z",
			},
			{
				code: "40KLIKES",
				expired: false,
				addedAt: "2022-09-29T18:31:00Z",
			},
			{
				code: "HAPPYHOLIDAYS",
				expired: true,
				addedAt: "2022-12-24T21:27:00Z",
			},
			{
				code: "50KLIKES",
				expired: false,
				addedAt: "2022-12-24T21:34:00Z",
			},
			{
				code: "BLACKHAWKISHERE",
				expired: false,
				addedAt: "2022-12-24T21:32:00Z",
			},
			{
				code: "60KLIKES",
				expired: false,
				addedAt: "2023-03-09T21:19:00Z",
			},
			{
				code: "70KLIKES",
				expired: false,
				addedAt: "2023-07-07T20:43:00Z",
			},
		],
	},
	ab: {
		universe: "5611522097",
		name: "airsoft battles",
		displayName: "Airsoft Battles",
		iconURL:
			"https://tr.rbxcdn.com/180DAY-704ac49e2129f50ca709751588cdd45b/256/256/Image/Webp/noFilter",
	},
} as const;

type ProjectChoice<T extends "name" | "label" = "name"> = T extends "name"
	? { name: string; value: string }
	: { label: string; value: string; default: boolean };

export function getProjectChoices<T extends "name" | "label" = "name">(
	label: T = "name" as T,
	defaultProjectKey?: string,
): ProjectChoice<T>[] {
	return Object.entries(Projects).map(([key, project]) =>
		label === "name"
			? { name: project.displayName, value: key }
			: {
					label: project.displayName,
					value: key,
					default: key === defaultProjectKey,
				},
	) as ProjectChoice<T>[];
}

export function buildReportModal(bug?: Bug) {
	const projectOptions = getProjectChoices("label");

	const gameLabel = new LabelBuilder()
		.setLabel("Affected Game")
		.setDescription(`The game affected by this bug`)
		.setStringSelectMenuComponent(
			new StringSelectMenuBuilder()
				.setMaxValues(1)
				.setMinValues(1)
				.setCustomId("affected")
				.setPlaceholder(`Select a game`)
				.addOptions(projectOptions),
		);

	const titleLabel = new LabelBuilder()
		.setLabel("Bug Title")
		.setDescription("A short summary of the bug")
		.setTextInputComponent(
			(() => {
				const input = new TextInputBuilder()
					.setCustomId("title")
					.setStyle(TextInputStyle.Short)
					.setValue(bug?.title ?? "")
					.setRequired(true)
					.setMaxLength(128);

				return input;
			})(),
		);

	const descriptionLabel = new LabelBuilder()
		.setLabel("Bug Description")
		.setDescription("Describe the bug in detail")
		.setTextInputComponent(
			(() => {
				const input = new TextInputBuilder()
					.setCustomId("description")
					.setStyle(TextInputStyle.Paragraph)
					.setValue(bug?.description ?? "")
					.setRequired(true)
					.setMaxLength(1024);

				return input;
			})(),
		);

	const modalId = bug ? editBugModal.id({ id: bug.id }) : bugModal.id();
	const modal = new ModalBuilder().setCustomId(modalId).setTitle("Report a Bug");

	modal.setLabelComponents(gameLabel, titleLabel, descriptionLabel);

	if (!bug) {
		const mediaLabel = new LabelBuilder()
			.setLabel("Media")
			.setDescription("Provide any screenshots or videos that demonstrate the bug")
			.setFileUploadComponent(new FileUploadBuilder().setCustomId("media").setRequired(false));
		modal.addLabelComponents(mediaLabel);
	}

	return modal;
}

export async function constructButtons(bug: Bug) {
	const row = new ActionRowBuilder<ButtonBuilder>();

	const isOpen = bug.status === "open";
	const statusLabel = isOpen ? "Close" : "Reopen";

	const _statusToggle = new ButtonBuilder()
		.setCustomId(bugStatusButton.id({ id: bug.id, status: bug.status || "open" }))
		.setLabel(statusLabel)
		.setStyle(isOpen ? ButtonStyle.Danger : ButtonStyle.Success);
	const _editButton = new ButtonBuilder()
		.setCustomId(editButton.id({ id: bug.id }))
		.setLabel("Edit")
		.setStyle(ButtonStyle.Secondary);

	row.addComponents(_statusToggle, _editButton);
	return row;
}

export function text(content: string) {
	return new TextDisplayBuilder().setContent(content);
}
export function thumbnail(url?: string) {
	return url ? new ThumbnailBuilder().setURL(url) : undefined;
}

export async function constructBugContainer(
	bug: Bug,
	actor: Actor,
	file?: AttachmentBuilder,
	existingMessage?: Message,
): Promise<ContainerBuilder | undefined> {
	const affected = bug.affectedProjects[0];
	if (!affected) return undefined;

	// @ts-expect-error: shudup
	const project = Projects[affected];
	if (!project) return undefined;

	const { title, description, bugNumber } = bug;
	const bodyText = text(`### ${title}\n${description}`);
	const footerText = text(
		[`-# #${bugNumber}`, project.displayName, `Reported by <@${actor.discordId!}>`].join(" • "),
	);

	const icon = thumbnail(project.iconURL);

	const section = new SectionBuilder().addTextDisplayComponents(bodyText);
	if (icon) section.setThumbnailAccessory(icon);

	const buttons = await constructButtons(bug);

	let gallery: MediaGalleryBuilder | undefined = undefined;

	if (file) {
		gallery = new MediaGalleryBuilder().addItems(
			new MediaGalleryItemBuilder().setURL(`attachment://${file.name}`),
		);
	} else if (existingMessage) {
		const oldContainer = existingMessage.components[0] as any;
		if (oldContainer) {
			const subComponents = oldContainer.components || oldContainer.data?.components || [];
			const oldGallery = subComponents.find(
				(c: any) =>
					c.items !== undefined ||
					c.type === 11 ||
					(c.constructor && c.constructor.name.includes("MediaGallery")),
			);

			if (oldGallery) {
				const itemsToLoop =
					oldGallery.items || oldGallery.components || oldGallery.data?.items || [];
				const extractedUrls: string[] = [];

				for (const item of itemsToLoop) {
					const url = item.url ?? item.data?.url ?? item.media?.url ?? item.data?.media?.url;
					if (url) extractedUrls.push(url);
				}

				if (extractedUrls.length > 0) {
					gallery = new MediaGalleryBuilder();
					for (const url of extractedUrls) {
						gallery.addItems(new MediaGalleryItemBuilder().setURL(url));
					}
				}
			}
		}

		if (!gallery) {
			const attachment = existingMessage.attachments.first();
			if (attachment && attachment.url) {
				gallery = new MediaGalleryBuilder().addItems(
					new MediaGalleryItemBuilder().setURL(attachment.url),
				);
			}
		}
	}

	const container = new ContainerBuilder();
	container.addSectionComponents(section);

	if (gallery) container.addMediaGalleryComponents(gallery);
	container.addTextDisplayComponents(footerText);
	container.addActionRowComponents(buttons);

	return container;
}