Skip to content

Commit

Permalink
refactor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardozgz committed Nov 29, 2024
1 parent dec5dfe commit a990f5f
Show file tree
Hide file tree
Showing 21 changed files with 1,337 additions and 1,063 deletions.
331 changes: 168 additions & 163 deletions apps/bot/src/@types/resources.d.ts
Original file line number Diff line number Diff line change
@@ -1,184 +1,189 @@
interface Resources {
main: {
interaction: {
commandHandler: {
error: {
title: "ERROR!";
description: "Something went wrong, please try again later.\n\nError ID: {{ERROR_ID}}";
};
};
commands: {
invite: {
definition: {
slash: {
name: "invite";
description: "Provides an invite link to add the bot.";
};
};
description: "Your invite link is ready:\n{{INVITE_URL}}";
addToServer: "Add to server";
addToServerAgain: "Add to this server again";
joinSupportServer: "Join support server";
};
info: {
definition: {
slash: {
name: "info";
description: "List of useful links and other information.";
};
};
embedReply: {
description: "[Add me to your server]({{BOT_INVITE_URL}})\n[Website]({{WEBSITE_URL}})\n[Support server]({{BOT_SUPPORT_URL}})\n[GitHub Repository]({{BOT_REPO_URL}})\n[Bot version: {{VERSION}}]({{VERSION_URL}})";
};
};
profile: {
definition: {
context: {
name: "Profile";
};
slash: {
name: "profile";
description: "Shows related information between you and this bot.";
options: {
user: {
name: "user";
description: "The user whose profile you want to view.";
};
};
};
};
badges: "Badges";
deleteProfileButton: "Delete data";
moreOptionsButton: "More options";
deleteProfilePrompt: {
title: "Are you sure you want to delete the data?";
description: "This action cannot be undone.";
confirmButton: "Yes";
cancelButton: "Cancel";
removeDataSuccess: "The data has been removed successfully.";
};
userNotFound: "User not found";
};
configure: {
definition: {
slash: {
name: "configure";
description: "Get the link to configure the bot on the website.";
};
};
reply: "Click below to configure the bot on the dashboard.\nYou can also set up some basic counters using the {{SETUP_COMMAND}} command without leaving Discord.";
dashboardButton: "Go to dashboard";
};
setup: {
definition: {
slash: {
name: "setup";
description: "Set up some basic counters.";
subcommands: {
server: {
name: "server";
description: "Set up server-related counters.";
};
youtube: {
name: "youtube";
description: "Set up YouTube-related counters.";
options: {
channelUrl: {
name: "url";
description: "The channel URL.";
};
};
};
twitch: {
name: "twitch";
description: "Set up Twitch-related counters.";
options: {
username: {
name: "username";
description: "The channel username.";
};
};
};
};
};
};
status: {
creating: "Creating counters...";
completed: "Creation completed.";
creatingCategory: "{{ICON}} Creating category.";
createdCategory: "{{ICON}} Category created.";
failedCategory: "{{ICON}} Category failed.";
creatingChannel: "{{ICON}} Creating {{NAME}} counter.";
createdChannel: "{{ICON}} {{NAME}} created.";
failedChannel: "{{ICON}} {{NAME}} failed.";
configureSuggestion: "You should check out {{CONFIGURE_COMMAND}} for a more customized setup.";
};
templateCollection: {
server: {
categoryName: "📊 Server Stats 📊";
templates: [
{
name: "Online members";
template: "Online: {{COUNTER}}";
"main": {
"interaction": {
"commandHandler": {
"error": {
"title": "ERROR!",
"description": "Something went wrong, please try again later.\n\nError ID: {{ERROR_ID}}"
}
},
"commands": {
"invite": {
"definition": {
"slash": {
"name": "invite",
"description": "Provides an invite link to add the bot."
}
},
"description": "Your invite link is ready:\n{{INVITE_URL}}",
"addToServer": "Add to server",
"addToServerAgain": "Add to this server again",
"joinSupportServer": "Join support server"
},
"info": {
"definition": {
"slash": {
"name": "info",
"description": "List of useful links and other information."
}
},
"embedReply": {
"description": "[Add me to your server]({{BOT_INVITE_URL}})\n[Website]({{WEBSITE_URL}})\n[Support server]({{BOT_SUPPORT_URL}})\n[GitHub Repository]({{BOT_REPO_URL}})\n[Bot version: {{VERSION}}]({{VERSION_URL}})"
}
},
"profile": {
"definition": {
"context": {
"name": "Profile"
},
"slash": {
"name": "profile",
"description": "Shows related information between you and this bot.",
"options": {
"user": {
"name": "user",
"description": "The user whose profile you want to view."
}
}
}
},
"badges": "Badges",
"deleteProfileButton": "Delete data",
"moreOptionsButton": "More options",
"deleteProfilePrompt": {
"title": "Are you sure you want to delete the data?",
"description": "This action cannot be undone.",
"confirmButton": "Yes",
"cancelButton": "Cancel",
"removeDataSuccess": "The data has been removed successfully."
},
"userNotFound": "User not found"
},
"configure": {
"definition": {
"slash": {
"name": "configure",
"description": "Get the link to configure the bot on the website."
}
},
"reply": "Click below to configure the bot on the dashboard.\nYou can also set up some basic counters using the {{SETUP_COMMAND}} command without leaving Discord.",
"dashboardButton": "Go to dashboard"
},
"setup": {
"definition": {
"slash": {
"name": "setup",
"description": "Set up some basic counters.",
"subcommands": {
"server": {
"name": "server",
"description": "Set up server-related counters."
},
{
name: "Members";
template: "Members: {{COUNTER}}";
"youtube": {
"name": "youtube",
"description": "Set up YouTube-related counters.",
"options": {
"channelUrl": {
"name": "url",
"description": "The channel URL."
}
}
},
"twitch": {
"name": "twitch",
"description": "Set up Twitch-related counters.",
"options": {
"username": {
"name": "username",
"description": "The channel username."
}
}
}
}
}
},
"status": {
"creating": "Creating counters...",
"completed": "Creation completed.",
"creatingCategory": "{{ICON}} Creating category.",
"createdCategory": "{{ICON}} Category created.",
"failedCategory": "{{ICON}} Category failed.",
"creatingChannel": "{{ICON}} Creating {{NAME}} counter.",
"createdChannel": "{{ICON}} {{NAME}} created.",
"failedChannel": "{{ICON}} {{NAME}} failed.",
"configureSuggestion": "You should check out {{CONFIGURE_COMMAND}} for a more customized setup."
},
"templateCollection": {
"server": {
"categoryName": "📊 Server Stats 📊",
"templates": [
{
name: "Nitro boosters";
template: "Boosters: {{COUNTER}}";
"name": "Online members",
"template": "Online: {{COUNTER}}"
},
{
name: "Roles";
template: "Roles: {{COUNTER}}";
"name": "Members",
"template": "Members: {{COUNTER}}"
},
{
name: "Channels";
template: "Channels: {{COUNTER}}";
"name": "Nitro boosters",
"template": "Boosters: {{COUNTER}}"
},
];
};
twitch: {
categoryName: "📊 {{COUNTER}} Twitch Stats 📊";
templates: [
{
name: "Twitch followers";
template: "{{COUNTER}} Followers";
"name": "Roles",
"template": "Roles: {{COUNTER}}"
},
{
name: "Twitch viewers";
template: "Viewers: {{COUNTER}}";
},
];
};
youtube: {
categoryName: "📊 {{COUNTER}} YouTube Stats 📊";
templates: [
"name": "Channels",
"template": "Channels: {{COUNTER}}"
}
]
},
"twitch": {
"categoryName": "📊 {{COUNTER}} Twitch Stats 📊",
"templates": [
{
name: "YouTube subscribers";
template: "{{COUNTER}} Subscribers";
"name": "Twitch followers",
"template": "{{COUNTER}} Followers"
},
{
name: "YouTube videos";
template: "{{COUNTER}} Videos";
"name": "Twitch viewers",
"template": "Viewers: {{COUNTER}}"
}
]
},
"youtube": {
"categoryName": "📊 {{COUNTER}} YouTube Stats 📊",
"templates": [
{
"name": "YouTube subscribers",
"template": "{{COUNTER}} Subscribers"
},
{
name: "YouTube views";
template: "{{COUNTER}} Views";
"name": "YouTube videos",
"template": "{{COUNTER}} Videos"
},
];
};
};
};
};
};
jobs: {
updateChannels: {
computeError: "ERROR!";
};
};
};
{
"name": "YouTube views",
"template": "{{COUNTER}} Views"
}
]
}
}
}
}
},
"jobs": {
"updateChannels": {
"computeError": "ERROR!"
}
},
"errors": {
"UserError": {
"USER_NOT_FOUND": "User not found."
}
}
}
}

export default Resources;
19 changes: 13 additions & 6 deletions apps/bot/src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
InteractionType,
} from "discord.js";

import { KnownError } from "@mc/common/KnownError/index";

import { env } from "~/env";
import { DiscordBrandingColors as Colors } from "../Constants";
import { initI18n } from "../i18n";
import handleCommand from "../interactions/commands";
import { EventHandler } from "../structures";
import { BaseEmbed } from "../utils/BaseMessageEmbed";
import { UserError } from "../utils/UserError";

export const interactionCreateEvent = new EventHandler({
name: "interactionCreate",
Expand All @@ -33,7 +34,7 @@ export const interactionCreateEvent = new EventHandler({
if (interaction.isRepliable()) {
const embed = new BaseEmbed(interaction.client);
const id = randomUUID();
let i18n: i18n;
let i18n: i18n | undefined = undefined;
let title: string, description: string, supportServerBtn: string;

try {
Expand All @@ -51,11 +52,17 @@ export const interactionCreateEvent = new EventHandler({
supportServerBtn = "Join support server";
}

if (error instanceof UserError) {
embed.setDescription(error.message);
} else {
embed.setDescription(
description.replaceAll("{{ERROR_ID}}", inlineCode(id)),
);

if (
error instanceof KnownError &&
i18n &&
error.cause.type === "UserError"
) {
embed.setDescription(
description.replaceAll("{{ERROR_ID}}", inlineCode(id)),
i18n.t(`errors.${error.cause.type}.${error.cause.name}`),
);
}

Expand Down
Loading

0 comments on commit a990f5f

Please sign in to comment.