Conciseness with .catch

This commit is contained in:
powermaker450 2024-10-25 12:20:07 -04:00
parent a40c150012
commit 125a872fbb
6 changed files with 82 additions and 126 deletions

View file

@ -73,46 +73,34 @@ export async function execute(interaction: ChatInputCommandInteraction) {
: "[Confession removed by moderator]" : "[Confession removed by moderator]"
); );
try {
// Replace the given confession with an empty embed // Replace the given confession with an empty embed
await (BotClient.channels.cache.get(channelId) as TextChannel).messages (BotClient.channels.cache.get(channelId) as TextChannel).messages
.fetch(confession) .fetch(confession)
.then(e => { .then(message => {
e.edit({ message.edit({
embeds: [emptyEmbed] embeds: [emptyEmbed]
}); });
});
return interaction.reply({ return interaction.reply({
content: "Confession removed.", content: "Confession removed.",
...messageOpts ...messageOpts
}); });
} catch (err) { })
logger.error("A confession delete error occured:", err); .catch(async err => {
logger.error("An error occured deleting a confession:", err);
return interaction.reply({ return interaction.reply({
content: "An error occured.", content: "An error occured when trying to delete that confession.",
...messageOpts
});
}
} else {
try {
// If there was a result, the user wasn't allowed to remove it, otherwise it didn't exist.
return result
? interaction.reply({
content: "You are not allowed to remove this confession.",
...messageOpts ...messageOpts
}) })
: interaction.reply({ .catch(err => logger.error("An error occured following up:", err));
content: })
"Either the confession wasn't found or you may not be allowed to remove it.", } else {
...messageOpts // If there was a result, the user wasn't allowed to remove it, otherwise it didn't exist.
});
} catch (err) {
logger.error("A confession delete interaction occured:", err);
return interaction.reply({ return interaction.reply({
content: "An error occured.", content: result ? "You are not allowed to remove this confession." : "Either the confession wasn't found or you may not be allowed to remove it.",
...messageOpts ...messageOpts
}); })
} .catch(err => logger.error("A confession delete error occured:", err));
} }
} }

View file

@ -93,50 +93,33 @@ export async function execute(interaction: ChatInputCommandInteraction) {
const confessionId = interaction.options.getString("id")!; const confessionId = interaction.options.getString("id")!;
if (dt.isBannedById(guildId, confessionId)) { if (dt.isBannedById(guildId, confessionId)) {
try {
return interaction.reply({ return interaction.reply({
content: "That user is already banned!", content: "That user is already banned!",
...messageOpts ...messageOpts
}); })
} catch (err) { .catch(err => logger.error("A ban interaction error occured:", err));
logger.error("A ban interaction error occured:", err);
}
} }
const result = dt.addBanById(guildId, confessionId); const result = dt.addBanById(guildId, confessionId);
try { return interaction.reply({
return result content: result ? "User was banned." : "No confession with that ID was found.",
? interaction.reply({
content: "User was banned.",
...messageOpts ...messageOpts
}) })
: interaction.reply({ .catch(err => logger.error("A ban interaction error occured:", err));
content: "No confession with that ID was found.",
...messageOpts
});
} catch (err) {
logger.error("A ban interaction error occured:", err);
}
// /confessmod banuser <user> // /confessmod banuser <user>
} else if (interaction.options.getSubcommand() === "banuser") { } else if (interaction.options.getSubcommand() === "banuser") {
const { id: userId } = interaction.options.getUser("user")!; const { id: userId } = interaction.options.getUser("user")!;
const result = dt.addBanByUser(guildId, userId); const result = dt.addBanByUser(guildId, userId);
try { return interaction.reply({
return result content: result ? "User was banned." : "How did we get here?",
? interaction.reply({
content: "User was banned.",
...messageOpts ...messageOpts
}) })
: interaction.reply({ .catch(err => logger.log("A ban user interaction error occured:", err));
content: "How did we get here? (An error occured.)}",
...messageOpts
});
} catch (err) {
logger.error("A banuser interaction error occured:", err);
}
// /confessmod list // /confessmod list
} else if (interaction.options.getSubcommand() === "list") { } else if (interaction.options.getSubcommand() === "list") {
const bannedMembers = dt.getBans(guildId); const bannedMembers = dt.getBans(guildId);
@ -170,18 +153,12 @@ export async function execute(interaction: ChatInputCommandInteraction) {
} }
}; };
try {
return interaction.reply({ return interaction.reply({
content: determineContent(), content: determineContent(),
...messageOpts ...messageOpts
}); })
} catch (err) { .catch(err => logger.error("A banlist interaction error occured:", err));
logger.error("A banlist interaction error occured:", err);
return interaction.reply({
content: "A server-side error occurred when getting the ban list.",
...messageOpts
});
}
// /confessmod pardon <id> // /confessmod pardon <id>
} else if (interaction.options.getSubcommand() === "pardon") { } else if (interaction.options.getSubcommand() === "pardon") {
const result = dt.removeBanById( const result = dt.removeBanById(
@ -189,39 +166,26 @@ export async function execute(interaction: ChatInputCommandInteraction) {
interaction.options.getString("id")! interaction.options.getString("id")!
); );
try { return interaction.reply({
return result content: result ? "User was unbanned." : "No confession with that ID was found.",
? interaction.reply({
content: "User was unbanned.",
...messageOpts ...messageOpts
}) })
: interaction.reply({ .catch(err => logger.log("An unban interaction error occured", err));
content: "No confession with that ID was found.",
...messageOpts // /confessmod pardonuser <user>
});
} catch (err) {
logger.error("An unban interaction error occured:", err);
}
} else if (interaction.options.getSubcommand() === "pardonuser") { } else if (interaction.options.getSubcommand() === "pardonuser") {
const { id: userId } = interaction.options.getUser("user")!; const { id: userId } = interaction.options.getUser("user")!;
const result = dt.removeBanByUser(guildId, userId); const result = dt.removeBanByUser(guildId, userId);
try { return interaction.reply({
return result content: result ? "User was unbanned." : "That user is not banned from confessions.",
? interaction.reply({
content: "User was unbanned.",
...messageOpts ...messageOpts
}) })
: interaction.reply({ .catch(err => logger.log("Error replying to user ban interaction", err));
content: "That user is not banned from confessions.",
...messageOpts
});
} catch (err) {
logger.error("An unban user interaction error occured:", err);
}
} }
// Catch-all
return interaction.reply({ return interaction.reply({
content: "Unknown error", content: "Unknown error",
...messageOpts ...messageOpts

View file

@ -17,11 +17,15 @@
*/ */
import { CommandInteraction, SlashCommandBuilder } from "discord.js"; import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import Logger from "../utils/Logger";
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName("ping") .setName("ping")
.setDescription("Ping!"); .setDescription("Ping!");
const logger = new Logger("(/) ping");
export async function execute(interaction: CommandInteraction) { export async function execute(interaction: CommandInteraction) {
return interaction.reply("I'm an OSS confessions bot!"); return interaction.reply("I'm an OSS confessions bot!")
.catch(err => logger.error("I'm honestly suprised this even happened:", err));
} }

View file

@ -46,7 +46,8 @@ export async function execute(interaction: CommandInteraction) {
return interaction.reply({ return interaction.reply({
content: "This guild has already been set up!", content: "This guild has already been set up!",
...messageOpts ...messageOpts
}); })
.catch(err => logger.error("An error occured during setup:", err));
} }
let confessChannel: string, logChannel: string; let confessChannel: string, logChannel: string;
@ -176,6 +177,6 @@ export async function execute(interaction: CommandInteraction) {
}); });
}); });
} catch (err) { } catch (err) {
logger.error("An error occured:", err); logger.error("An error occured during setup:", err);
} }
} }

View file

@ -41,7 +41,8 @@ export async function execute(interaction: ChatInputCommandInteraction) {
return interaction.reply({ return interaction.reply({
content: `You can only run the update command once every ${minutes} minutes.`, content: `You can only run the update command once every ${minutes} minutes.`,
...messageOpts ...messageOpts
}); })
.catch(err => logger.error("An update error occured:", err));
} }
deployCommands({ guildId: guildId }); deployCommands({ guildId: guildId });
@ -60,5 +61,6 @@ export async function execute(interaction: ChatInputCommandInteraction) {
return interaction.reply({ return interaction.reply({
content: "Commands refreshed.", content: "Commands refreshed.",
...messageOpts ...messageOpts
}); })
.catch(err => logger.error("An update error occured", err));
} }

View file

@ -95,7 +95,9 @@ BotClient.on(Events.InteractionCreate, async interaction => {
content: "You are banned from confessions in this server!", content: "You are banned from confessions in this server!",
...messageOpts ...messageOpts
}) })
: interaction.showModal(submit); .catch(err => logger.error("An error occured during a requestSubmit", err))
: interaction.showModal(submit)
.catch(err => logger.error("An error occured during a requestSubmit", err));
} }
}); });
@ -110,13 +112,8 @@ BotClient.on(Events.InteractionCreate, interaction => {
"confessionAttachment" "confessionAttachment"
); );
try { submitConfession(interaction, messageContent, attachment ? attachment : undefined)
attachment .catch(err => logger.error("An error occured when submitting a confession", err));
? submitConfession(interaction, messageContent)
: submitConfession(interaction, messageContent, attachment);
} catch (err) {
logger.error("An error occured:", err);
}
} }
}); });