mirror of
https://github.com/xHyroM/roles-bot.git
synced 2024-11-24 17:21:05 +01:00
feat: improved errors
This commit is contained in:
parent
c62cfa2438
commit
b18eff081c
3 changed files with 53 additions and 7 deletions
18
packages/bot/src/utils/errors.ts
Normal file
18
packages/bot/src/utils/errors.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||||
|
type RecursiveError = Record<string, any>;
|
||||||
|
|
||||||
|
export function parseErrors(
|
||||||
|
obj: RecursiveError,
|
||||||
|
): { code: string; message: string }[] {
|
||||||
|
const errors = [];
|
||||||
|
|
||||||
|
for (const key in obj) {
|
||||||
|
if (key === "_errors") {
|
||||||
|
errors.push(obj[key]);
|
||||||
|
} else if (typeof obj[key] === "object") {
|
||||||
|
errors.push(...parseErrors(obj[key]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.flat();
|
||||||
|
}
|
|
@ -34,6 +34,7 @@ export default async function (data: BasicData, ctx: Context, rawRole: string) {
|
||||||
new TextInputBuilder()
|
new TextInputBuilder()
|
||||||
.setLabel("Label")
|
.setLabel("Label")
|
||||||
.setCustomId("label")
|
.setCustomId("label")
|
||||||
|
.setMaxLength(data.selecting === "buttons" ? 80 : 100)
|
||||||
.setPlaceholder("Ping")
|
.setPlaceholder("Ping")
|
||||||
.setStyle(TextInputStyle.Short)
|
.setStyle(TextInputStyle.Short)
|
||||||
.setRequired(true),
|
.setRequired(true),
|
||||||
|
@ -75,6 +76,7 @@ export default async function (data: BasicData, ctx: Context, rawRole: string) {
|
||||||
new TextInputBuilder()
|
new TextInputBuilder()
|
||||||
.setLabel("Description")
|
.setLabel("Description")
|
||||||
.setCustomId("description")
|
.setCustomId("description")
|
||||||
|
.setMaxLength(100)
|
||||||
.setPlaceholder("pingping pong pong")
|
.setPlaceholder("pingping pong pong")
|
||||||
.setStyle(TextInputStyle.Short)
|
.setStyle(TextInputStyle.Short)
|
||||||
.setRequired(false),
|
.setRequired(false),
|
||||||
|
|
|
@ -21,6 +21,7 @@ import splitArray from "./splitArray";
|
||||||
import { resolvePartialEmoji } from "./resolveEmoji";
|
import { resolvePartialEmoji } from "./resolveEmoji";
|
||||||
import { BasicData } from "../types";
|
import { BasicData } from "../types";
|
||||||
import resolveButtonStyle from "./resolveButtonStyle";
|
import resolveButtonStyle from "./resolveButtonStyle";
|
||||||
|
import { parseErrors } from "./errors";
|
||||||
|
|
||||||
type DataBot = BasicData & {
|
type DataBot = BasicData & {
|
||||||
sendAs: "bot";
|
sendAs: "bot";
|
||||||
|
@ -66,7 +67,9 @@ export default async function (ctx: Context, data: Data) {
|
||||||
const components: APIActionRowComponent<APIMessageActionRowComponent>[] = [];
|
const components: APIActionRowComponent<APIMessageActionRowComponent>[] = [];
|
||||||
const array = splitArray(data.roleIds, data.selecting === "buttons" ? 5 : 24);
|
const array = splitArray(data.roleIds, data.selecting === "buttons" ? 5 : 24);
|
||||||
for (const items of array) {
|
for (const items of array) {
|
||||||
const actionRow = new ActionRowBuilder();
|
const actionRow = new ActionRowBuilder<
|
||||||
|
ButtonBuilder | StringSelectMenuBuilder
|
||||||
|
>();
|
||||||
|
|
||||||
const selectMenuNaiveOption = new StringSelectMenuOptionBuilder()
|
const selectMenuNaiveOption = new StringSelectMenuOptionBuilder()
|
||||||
.setLabel("Make a choice")
|
.setLabel("Make a choice")
|
||||||
|
@ -113,8 +116,7 @@ export default async function (ctx: Context, data: Data) {
|
||||||
|
|
||||||
if (data.selecting === "dropdowns") actionRow.addComponents(selectMenu);
|
if (data.selecting === "dropdowns") actionRow.addComponents(selectMenu);
|
||||||
|
|
||||||
// @ts-expect-error i know i know
|
components.push(actionRow.toJSON());
|
||||||
components.push(actionRow);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
payload.components = components;
|
payload.components = components;
|
||||||
|
@ -152,11 +154,23 @@ export default async function (ctx: Context, data: Data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const json: { message: string; code: string } = await res.json();
|
const json: {
|
||||||
|
message: string;
|
||||||
|
code: string;
|
||||||
|
errors: Record<string, unknown>;
|
||||||
|
} = await res.json();
|
||||||
|
const errors = parseErrors(json?.errors ?? {});
|
||||||
|
|
||||||
return ctx.respond({
|
return ctx.respond({
|
||||||
type: InteractionResponseType.ChannelMessageWithSource,
|
type: InteractionResponseType.ChannelMessageWithSource,
|
||||||
data: {
|
data: {
|
||||||
content: `Error: ${json.message} (${json.code})`,
|
content: `Error: ${json.message} (${json.code})${
|
||||||
|
errors.length > 0
|
||||||
|
? `\n${errors
|
||||||
|
.map((e) => ` - ${e.message} (${e.code})`)
|
||||||
|
.join("\n")}`
|
||||||
|
: ""
|
||||||
|
}`,
|
||||||
flags: MessageFlags.Ephemeral,
|
flags: MessageFlags.Ephemeral,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -200,11 +214,23 @@ export default async function (ctx: Context, data: Data) {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const json: { message: string; code: string } = await res.json();
|
const json: {
|
||||||
|
message: string;
|
||||||
|
code: string;
|
||||||
|
errors: Record<string, unknown>;
|
||||||
|
} = await res.json();
|
||||||
|
const errors = parseErrors(json?.errors ?? {});
|
||||||
|
|
||||||
return ctx.respond({
|
return ctx.respond({
|
||||||
type: InteractionResponseType.ChannelMessageWithSource,
|
type: InteractionResponseType.ChannelMessageWithSource,
|
||||||
data: {
|
data: {
|
||||||
content: `Error: ${json.message} (${json.code})`,
|
content: `Error: ${json.message} (${json.code})${
|
||||||
|
errors.length > 0
|
||||||
|
? `\n${errors
|
||||||
|
.map((e) => ` - ${e.message} (${e.code})`)
|
||||||
|
.join("\n")}`
|
||||||
|
: ""
|
||||||
|
}`,
|
||||||
flags: MessageFlags.Ephemeral,
|
flags: MessageFlags.Ephemeral,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue