src/interactions/commands/bug/_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 |
import config from "@/config";
import { type Bug, attachments, db } from "@/database";
import {
type Client,
ContainerBuilder,
FileUploadBuilder,
LabelBuilder,
ModalBuilder,
SectionBuilder,
type Snowflake,
StringSelectMenuBuilder,
TextDisplayBuilder,
TextInputBuilder,
TextInputStyle,
ThumbnailBuilder,
} from "discord.js";
import { and, eq } from "drizzle-orm";
// -- types ----------------------------------------------------------------------------------------
type ProjectChoice<T extends "name" | "label" = "name"> = T extends "name"
? { name: string; value: string }
: { label: string; value: string; default: boolean };
// -- constants ------------------------------------------------------------------------------------
export const PROJECT_MAP = config.projects;
export const INPUT_IDS = {
AFFECTED: "affectedInput",
TITLE: "titleInput",
DESCRIPTION: "descriptionInput",
MEDIA: "fileUploadInput",
} as const;
export const MODAL_IDS = {
REPORT: "bug:report",
EDIT: "bug:edit",
} as const;
export const VALIDATION = {
TITLE_MAX_LENGTH: 100,
DESCRIPTION_MAX_LENGTH: 1000,
THREAD_NAME_MAX_LENGTH: 50,
AUTO_ARCHIVE_DURATION: 1440,
} as const;
// -- local helpres --------------------------------------------------------------------------------
export function getProjectChoices<T extends "name" | "label" = "name">(
label: T = "name" as T,
defaultProjectKey?: string,
): ProjectChoice<T>[] {
return Object.entries(PROJECT_MAP).map(([key, project]) =>
label === "name"
? { name: project.displayName, value: key }
: {
label: project.displayName,
value: key,
default: key === defaultProjectKey,
},
) as ProjectChoice<T>[];
}
// -- main -----------------------------------------------------------------------------------------
export async function constructContainer(
bug: Bug,
userId: Snowflake,
): Promise<ContainerBuilder | undefined> {
const project = PROJECT_MAP[bug.projects[0]];
if (!project) return undefined;
const text = {
body: new TextDisplayBuilder().setContent(
[`### ${bug.title}`, `${bug.description}`].join("\n"),
),
footer: {
text: new TextDisplayBuilder().setContent(
[`-# #${bug.id}`, project.displayName, `Reported by <@${userId}>`].join(
" • ",
),
),
icon: project.iconURL
? new ThumbnailBuilder().setURL(project.iconURL)
: undefined,
},
};
const section = new SectionBuilder();
if (text.footer.icon) section.setThumbnailAccessory(text.footer.icon);
section.addTextDisplayComponents(text.body);
const media = await db
.select()
.from(attachments)
.where(
and(eq(attachments.ownerId, bug.id), eq(attachments.ownerType, "bug")),
);
const urls = media.map((attachment) => attachment.url);
section.addTextDisplayComponents(text.footer.text);
const container = new ContainerBuilder().addSectionComponents(section);
return container;
}
export function buildReportModal(bugId?: string, bug?: Bug) {
const terminology = config.terminology;
const capitalizedTerminology =
terminology.charAt(0).toUpperCase() + terminology.slice(1);
const projectOptions = bug
? getProjectChoices("label", bug.projects[0])
: getProjectChoices("label");
const selectedProject = bug
? projectOptions.find((opt) => opt.value === bug.projects[0])
: undefined;
const gameLabel = new LabelBuilder()
.setLabel(capitalizedTerminology)
.setDescription(`The ${terminology} affected by this bug`)
.setStringSelectMenuComponent(
new StringSelectMenuBuilder()
.setMaxValues(1)
.setMinValues(1)
.setCustomId(INPUT_IDS.AFFECTED)
.setPlaceholder(selectedProject?.label || `Select the ${terminology}`)
.addOptions(projectOptions),
);
const titleLabel = new LabelBuilder()
.setLabel("Bug Title")
.setDescription(bug ? "Update the bug title" : "A short summary of the bug")
.setTextInputComponent(
(() => {
const input = new TextInputBuilder()
.setCustomId(INPUT_IDS.TITLE)
.setStyle(TextInputStyle.Short)
.setRequired(true)
.setMaxLength(VALIDATION.TITLE_MAX_LENGTH);
if (bug) input.setValue(bug.title);
return input;
})(),
);
const descriptionLabel = new LabelBuilder()
.setLabel("Bug Description")
.setDescription(
bug ? "Update the bug description" : "Describe the bug in detail",
)
.setTextInputComponent(
(() => {
const input = new TextInputBuilder()
.setCustomId(INPUT_IDS.DESCRIPTION)
.setStyle(TextInputStyle.Paragraph)
.setRequired(true)
.setMaxLength(VALIDATION.DESCRIPTION_MAX_LENGTH);
if (bug) input.setValue(bug.description);
return input;
})(),
);
const modal = new ModalBuilder()
.setCustomId(bug ? `${MODAL_IDS.EDIT}:${bugId}` : `${MODAL_IDS.REPORT}:NEW`)
.setTitle(bug ? `Edit Bug #${bugId}` : "Report a Bug");
if (bug) {
modal.setLabelComponents(gameLabel, titleLabel, descriptionLabel);
} else {
const mediaLabel = new LabelBuilder()
.setLabel("Media")
.setDescription(
"Provide any screenshots or videos that demonstrate the bug",
)
.setFileUploadComponent(
new FileUploadBuilder().setCustomId(INPUT_IDS.MEDIA).setRequired(false),
);
modal.setLabelComponents(
gameLabel,
titleLabel,
descriptionLabel,
mediaLabel,
);
}
return modal;
}
export async function updateBugEmbed(
client: Client,
bug: Bug,
messageId: Snowflake,
channelId: Snowflake,
) {
const channel = await client.channels.fetch(channelId);
if (!channel || !channel.isTextBased() || !channel.isSendable()) return;
const message = await channel.messages.fetch(messageId);
const project = PROJECT_MAP[bug.projects[0]];
if (!project) return;
const container = await constructContainer(bug, message.author.id);
if (!container) return;
await message.edit({
components: [container],
flags: ["IsComponentsV2"],
});
}
|