Skip to content

Commit

Permalink
add: Added error handling across files
Browse files Browse the repository at this point in the history
  • Loading branch information
Kkkermit committed Jun 2, 2024
1 parent 38d4712 commit cecfec8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
23 changes: 21 additions & 2 deletions src/events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,36 @@ module.exports = {

const command = client.commands.get(interaction.commandName);

const color = {
red: '\x1b[31m',
orange: '\x1b[38;5;202m',
yellow: '\x1b[33m',
green: '\x1b[32m',
blue: '\x1b[34m',
reset: '\x1b[0m'
}

function getTimestamp() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

if (!command) return

try{
await command.execute(interaction, client);
} catch (error) {

client.logs.error(error);
console.error(`${color.red}[${getTimestamp()}] [INTERACTION_CREATE] Error while executing command. \n${color.red}[${getTimestamp()}] [INTERACTION_CREATE] Please check you are using the correct execute method: "async execute(interaction, client)":`, error);

const channelID = `${client.config.commandErrorChannel}`;
const channel = client.channels.cache.get(channelID);


const embed = new EmbedBuilder()
.setColor("Blue")
Expand Down
36 changes: 30 additions & 6 deletions src/events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,44 @@ module.exports = {
if (!message.content.startsWith(client.config.prefix)) return;
const args = message.content.slice(client.config.prefix.length).trim().split(/ +/);

const color = {
red: '\x1b[31m',
orange: '\x1b[38;5;202m',
yellow: '\x1b[33m',
green: '\x1b[32m',
blue: '\x1b[34m',
reset: '\x1b[0m'
}

function getTimestamp() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

let cmd = args.shift().toLowerCase();
if (cmd.length === 0) return;

let command = client.pcommands.get(cmd);
if (!command) command = client.pcommands.get(client.aliases.get(cmd));

if (!command) {
try{

const embed = new EmbedBuilder()
.setColor("Red")
.setTitle(`${client.user.username} prefix system ${client.config.arrowEmoji}`)
.setDescription(`> The command you tried **does not exist**. \n> To see **all** commands, use \`\`${client.config.prefix}help\`\``)
const embed = new EmbedBuilder()
.setColor("Red")
.setTitle(`${client.user.username} prefix system ${client.config.arrowEmoji}`)
.setDescription(`> The command you tried **does not exist**. \n> To see **all** commands, use \`\`${client.config.prefix}help\`\``)

return message.reply({ embeds: [embed], ephemeral: true});
return message.reply({ embeds: [embed], ephemeral: true});
} catch (error) {
client.logs.error(`[PREFIX_ERROR] Error sending 'cannot find prefix' embed.`, error);
};
};

if (!command) return;
Expand All @@ -36,7 +60,7 @@ module.exports = {
try {
command.execute(message, client, args);
} catch (error) {
client.logs.error(error)
console.error(`${color.red}[${getTimestamp()}] [MESSAGE_CREATE] Error while executing command. \n${color.red}[${getTimestamp()}] [MESSAGE_CREATE] Please check you are using the correct execute method: "async execute(message, client, args)":`, error);

const embed = new EmbedBuilder()
.setColor("Red")
Expand Down
4 changes: 3 additions & 1 deletion src/functions/handelCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ module.exports = (client) => {
Routes.applicationCommands(clientId), {
body: client.commandArray
},
);
).catch((error) => {
console.error(`${color.red}[${getTimestamp()}] [SLASH_COMMANDS] Error while refreshing application (/) commands. \n${color.red}[${getTimestamp()}] [SLASH_COMMANDS] Check if your clientID is correct and matches your bots token:`, error);
});

client.logs.success(`[SLASH_COMMANDS] Successfully reloaded application (/) commands.`);
} catch (error) {
Expand Down
6 changes: 2 additions & 4 deletions src/functions/handlePrefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ module.exports = (client) => {

(async () => {
try {

client.logs.success(`[PREFIX_COMMANDS] Successfully reloaded prefix (?) commands.`);

} catch (error) {
console.error(error);
}
})();
}
}
};
};
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs');

// Version Control //

const currentVersion = "v2.0";
const currentVersion = "beta";

const client = new Client({ intents: [
GatewayIntentBits.Guilds,
Expand Down
7 changes: 4 additions & 3 deletions src/lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ function getTimestamp() {

async function getLatestVersion() {
try {
const response = await axios.get('https://api.github.com/repos/Kkkermit/DiscordBotV14-template/releases/latest');
const response = await axios.get('https://api.github.com/repos/Kkkermit/Testify/releases/latest');
const latestVersion = response.data.tag_name;
return latestVersion;
} catch (error) {
console.error(`${color.torquise}[${getTimestamp()}] [LATEST_VERSION] Error while retrieving the latest version, ${color.reset}`, error);
// Release has not yet come out yet causing this error to be thrown, just ignore it for it now.
console.error(`${color.torquise}[${getTimestamp()}] [LATEST_VERSION] Error while retrieving the latest version. No release found. ${color.reset}`);
}
}

function checkVersion(currentVersion) {
getLatestVersion().then((latestVersion) => {
if (currentVersion < latestVersion) {
console.log(`${color.torquise}[${getTimestamp()}] [LATEST_VERSION] Attention, a new update is available, please install it - https://github.com/Kkkermit/DiscordBotV14-template`);
console.log(`${color.torquise}[${getTimestamp()}] [LATEST_VERSION] Attention, a new update is available, please install it - https://github.com/Kkkermit/Testify`);
} else {
console.log(`${color.torquise}[${getTimestamp()}] [LATEST_VERSION] You have the latest version of the code.`);
}
Expand Down

0 comments on commit cecfec8

Please sign in to comment.