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]"
);
try {
// 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)
.then(e => {
e.edit({
.then(message => {
message.edit({
embeds: [emptyEmbed]
});
});
return interaction.reply({
content: "Confession removed.",
...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({
content: "An error occured.",
...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.",
content: "An error occured when trying to delete that confession.",
...messageOpts
})
: interaction.reply({
content:
"Either the confession wasn't found or you may not be allowed to remove it.",
...messageOpts
});
} catch (err) {
logger.error("A confession delete interaction occured:", err);
.catch(err => logger.error("An error occured following up:", err));
})
} else {
// If there was a result, the user wasn't allowed to remove it, otherwise it didn't exist.
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
});
}
})
.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")!;
if (dt.isBannedById(guildId, confessionId)) {
try {
return interaction.reply({
content: "That user is already banned!",
...messageOpts
});
} catch (err) {
logger.error("A ban interaction error occured:", err);
}
})
.catch(err => logger.error("A ban interaction error occured:", err));
}
const result = dt.addBanById(guildId, confessionId);
try {
return result
? interaction.reply({
content: "User was banned.",
return interaction.reply({
content: result ? "User was banned." : "No confession with that ID was found.",
...messageOpts
})
: interaction.reply({
content: "No confession with that ID was found.",
...messageOpts
});
} catch (err) {
logger.error("A ban interaction error occured:", err);
}
.catch(err => logger.error("A ban interaction error occured:", err));
// /confessmod banuser <user>
} else if (interaction.options.getSubcommand() === "banuser") {
const { id: userId } = interaction.options.getUser("user")!;
const result = dt.addBanByUser(guildId, userId);
try {
return result
? interaction.reply({
content: "User was banned.",
return interaction.reply({
content: result ? "User was banned." : "How did we get here?",
...messageOpts
})
: interaction.reply({
content: "How did we get here? (An error occured.)}",
...messageOpts
});
} catch (err) {
logger.error("A banuser interaction error occured:", err);
}
.catch(err => logger.log("A ban user interaction error occured:", err));
// /confessmod list
} else if (interaction.options.getSubcommand() === "list") {
const bannedMembers = dt.getBans(guildId);
@ -170,18 +153,12 @@ export async function execute(interaction: ChatInputCommandInteraction) {
}
};
try {
return interaction.reply({
content: determineContent(),
...messageOpts
});
} catch (err) {
logger.error("A banlist interaction error occured:", err);
return interaction.reply({
content: "A server-side error occurred when getting the ban list.",
...messageOpts
});
}
})
.catch(err => logger.error("A banlist interaction error occured:", err));
// /confessmod pardon <id>
} else if (interaction.options.getSubcommand() === "pardon") {
const result = dt.removeBanById(
@ -189,39 +166,26 @@ export async function execute(interaction: ChatInputCommandInteraction) {
interaction.options.getString("id")!
);
try {
return result
? interaction.reply({
content: "User was unbanned.",
return interaction.reply({
content: result ? "User was unbanned." : "No confession with that ID was found.",
...messageOpts
})
: interaction.reply({
content: "No confession with that ID was found.",
...messageOpts
});
} catch (err) {
logger.error("An unban interaction error occured:", err);
}
.catch(err => logger.log("An unban interaction error occured", err));
// /confessmod pardonuser <user>
} else if (interaction.options.getSubcommand() === "pardonuser") {
const { id: userId } = interaction.options.getUser("user")!;
const result = dt.removeBanByUser(guildId, userId);
try {
return result
? interaction.reply({
content: "User was unbanned.",
return interaction.reply({
content: result ? "User was unbanned." : "That user is not banned from confessions.",
...messageOpts
})
: interaction.reply({
content: "That user is not banned from confessions.",
...messageOpts
});
} catch (err) {
logger.error("An unban user interaction error occured:", err);
}
.catch(err => logger.log("Error replying to user ban interaction", err));
}
// Catch-all
return interaction.reply({
content: "Unknown error",
...messageOpts

View file

@ -17,11 +17,15 @@
*/
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import Logger from "../utils/Logger";
export const data = new SlashCommandBuilder()
.setName("ping")
.setDescription("Ping!");
const logger = new Logger("(/) ping");
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({
content: "This guild has already been set up!",
...messageOpts
});
})
.catch(err => logger.error("An error occured during setup:", err));
}
let confessChannel: string, logChannel: string;
@ -176,6 +177,6 @@ export async function execute(interaction: CommandInteraction) {
});
});
} 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({
content: `You can only run the update command once every ${minutes} minutes.`,
...messageOpts
});
})
.catch(err => logger.error("An update error occured:", err));
}
deployCommands({ guildId: guildId });
@ -60,5 +61,6 @@ export async function execute(interaction: ChatInputCommandInteraction) {
return interaction.reply({
content: "Commands refreshed.",
...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!",
...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"
);
try {
attachment
? submitConfession(interaction, messageContent)
: submitConfession(interaction, messageContent, attachment);
} catch (err) {
logger.error("An error occured:", err);
}
submitConfession(interaction, messageContent, attachment ? attachment : undefined)
.catch(err => logger.error("An error occured when submitting a confession", err));
}
});