Skip to content

Commit

Permalink
Stripped off unwanted try catches
Browse files Browse the repository at this point in the history
  • Loading branch information
yanukadeneth99 committed Aug 22, 2022
1 parent a2b9636 commit c917fcf
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 258 deletions.
9 changes: 2 additions & 7 deletions delete-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@

const deleteCommands = require("./utils/deleteCommands");

try {
// Pass true to remove all commands globally (i.e : deleteCommands(true))
deleteCommands();
} catch (error) {
console.error(`Error deleting commands in delete-commands : ${error}`);
throw new Error(error);
}
// Pass true to remove all commands globally (i.e : deleteCommands(true))
deleteCommands();
29 changes: 12 additions & 17 deletions deploy-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@ const fs = require("node:fs");
const path = require("node:path");
const assignCommands = require("./utils/assignCommands");

try {
// Get all commands from the `/commands` folder
const commands = [];
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
// Get all commands from the `/commands` folder
const commands = [];
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const commandPath = path.join(commandsPath, file);
const command = require(commandPath);
commands.push(command.data.toJSON());
}
// Pass true to assign commands globally (i.e: assignCommands(commands,true))
assignCommands(commands);
} catch (error) {
console.error(`Error pushing commands in deploy-commands : ${error}`);
throw new Error(error);
for (const file of commandFiles) {
const commandPath = path.join(commandsPath, file);
const command = require(commandPath);
commands.push(command.data.toJSON());
}
// Pass true to assign commands globally (i.e: assignCommands(commands,true))
assignCommands(commands);
107 changes: 45 additions & 62 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//* Handles all kinds of interactions
// Add all new commands build to here

// TODO : Make two individual catches for commands and modals
// TODO : Bit too much Try Catches and Error Handling, try to minimize them as much as possible
// TODO : Remove Catches from here and handle them in the responses themselves

const { InteractionType } = require("discord.js");
const { channels } = require("../config.json");
Expand All @@ -17,91 +16,76 @@ module.exports = {

//* Chat Command Interactions
if (interaction.isChatInputCommand()) {
try {
if (interaction.commandName === "info") {
await require("../responses/info_response")(interaction);
} else if (interaction.commandName === "ping") {
await require("../responses/ping_response")(interaction);
} else if (interaction.commandName === "feedback") {
await require("../responses/feedback_response")(interaction);
} else if (interaction.commandName === "balance") {
await require("../responses/balance_response")(interaction);
} else if (interaction.commandName === "faucet") {
await require("../responses/faucet_response")(interaction);
}
// Invalid Chat command passed
else {
await interaction.reply({
content: "👀 This Command does not exist!",
ephemeral: true,
});
logchannel.send(
`[ERROR]\n${new Date(
Date.now()
).toUTCString()}\nInvalid Chat Command Passed\nBy : ${
interaction.user.username
}`
);
return;
}
} catch (error) {
// Handle Errors thrown by the Commands
console.error(`Error handling Chat Input : ${error}`);
if (interaction.commandName === "info") {
require("../responses/info_response")(interaction);
} else if (interaction.commandName === "ping") {
require("../responses/ping_response")(interaction);
} else if (interaction.commandName === "feedback") {
require("../responses/feedback_response")(interaction);
} else if (interaction.commandName === "balance") {
require("../responses/balance_response")(interaction);
} else if (interaction.commandName === "faucet") {
require("../responses/faucet_response")(interaction);
}
// Invalid Chat command passed
else {
await interaction.reply({
content: "👀 This Command does not exist!",
ephemeral: true,
});
logchannel.send(
`[ERROR]\n${new Date(
Date.now()
).toUTCString()}\nHandling Chat Inputs\n${error}`
).toUTCString()}\nInvalid Chat Command Passed\nBy : ${
interaction.user.username
}`
);
await interaction.editReply({
content: "🙇‍♂️ Error, please try again later",
ephemeral: true,
});
return;
}
}
//* Modal Command Interactions
else if (interaction.type === InteractionType.ModalSubmit) {
try {
if (interaction.customId === "feedbackModal") {
if (interaction.customId === "feedbackModal") {
try {
// Handle the data from the modal
feedback_handle(client, interaction);
await feedback_handle(client, interaction);
// Reply the user
await interaction.reply({
content: `💁🏼‍♂️ Your feedback was received successfully!`,
ephemeral: true,
});
return;
}
// Unknown Modal Submitted
else {
await interaction.reply({
content: "👀 Invalid Modal Interaction!",
ephemeral: true,
});
} catch (error) {
console.error(`Error Submitting Feedback : ${error}`);
logchannel.send(
`[ERROR]\n${new Date(
Date.now()
).toUTCString()}\nInvalid Modal Interaction\nBy : ${
interaction.user.username
}`
).toUTCString()}\nSubmittingFeedback\n${error}`
);
return;
await interaction.reply({
content: "🙇‍♂️ Error, please try again later",
ephemeral: true,
});
}
} catch (error) {
// Handle Errors thrown by Modals
console.error(`Error handling Chat Input : ${error}`);
}
// Unknown Modal Submitted
else {
await interaction.reply({
content: "👀 Invalid Modal Interaction!",
ephemeral: true,
});
logchannel.send(
`[ERROR]\n${new Date(
Date.now()
).toUTCString()}\nHandling Chat Inputs\n${error}`
).toUTCString()}\nInvalid Modal Interaction\nBy : ${
interaction.user.username
}`
);
await interaction.editReply({
content: "🙇‍♂️ Error, please try again later",
ephemeral: true,
});
return;
}
}
// Different kind of interaction
//* Different kind of interaction
else {
console.log(`Different kind of interaction : ${interaction}`);
return;
}
} catch (error) {
Expand All @@ -112,7 +96,6 @@ module.exports = {
Date.now()
).toUTCString()}\nInteraction Handling\n${error}`
);
throw new Error("Unable to Run Interactions");
}
},
};
1 change: 0 additions & 1 deletion events/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module.exports = {
logchannel.send(
`[ERROR]\n${new Date(Date.now()).toUTCString()}\nWaking BOT\n${error}`
);
throw new Error(error);
}
},
};
56 changes: 23 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,40 @@ const { bot } = require("./config.json");
const client = require("./client"); // Get Client

// Run the Events
try {
const eventsPath = path.join(__dirname, "events");
const eventFiles = fs
.readdirSync(eventsPath)
.filter((file) => file.endsWith(".js"));
const eventsPath = path.join(__dirname, "events");
const eventFiles = fs
.readdirSync(eventsPath)
.filter((file) => file.endsWith(".js"));

for (const file of eventFiles) {
const ePath = path.join(eventsPath, file);
const event = require(ePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(client, ...args));
}
for (const file of eventFiles) {
const ePath = path.join(eventsPath, file);
const event = require(ePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(client, ...args));
}
} catch (error) {
console.error(`Error getting the events in index : ${error}`);
throw new Error(error);
}

// Gets all command files, and sets them
try {
client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
//Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}
} catch (error) {
console.error(`Error getting the commands in index : ${error}`);
throw new Error(error);
client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
//Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}

// Login to Bot with token
try {
client.login(bot.token);
} catch (error) {
console.error(`Error login to BOT at index : ${error}`);
throw new Error(error);
}
32 changes: 11 additions & 21 deletions modals/feedback_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@
const { channels } = require("../config.json");

module.exports = async (client, interaction) => {
try {
// Get the Feedback Channel
const fdChannel = await client.channels.cache.get(channels.feedback);
// Get the Feedback Channel
const fdChannel = await client.channels.cache.get(channels.feedback);

// Get the value of the modal interactions
const subject = interaction.fields.getTextInputValue("subject");
const description = interaction.fields.getTextInputValue("description");
const user = interaction.user.username;
// Get the value of the modal interactions
const subject = interaction.fields.getTextInputValue("subject");
const description = interaction.fields.getTextInputValue("description");
const user = interaction.user.username;

fdChannel.send(
`[FEEDBACK]\n${new Date(
Date.now()
).toUTCString()}\nPerson : ${user}\nSubject : ${subject}\nDescription : ${description}`
);
} catch (error) {
console.error(error);
const logchannel = await client.channels.cache.get(channels.log);
logchannel.send(
`[ERROR]\n${new Date(
Date.now()
).toUTCString()}\nHandling Feedback Submission\n${error}`
);
}
fdChannel.send(
`[FEEDBACK]\n${new Date(
Date.now()
).toUTCString()}\nPerson : ${user}\nSubject : ${subject}\nDescription : ${description}`
);
};
16 changes: 7 additions & 9 deletions responses/balance_response.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ const getBalance = require("../utils/getBalance");
const { networks, channels } = require("../config.json");

module.exports = async (interaction) => {
try {
// Initial Responce to client
await interaction.reply({
content: "👩‍💻 Calculating....",
fetchReply: true,
});
// Initial Responce to client
await interaction.deferReply();
await interaction.editReply("👩‍💻 Calculating....");

try {
let balance; // Holds the final balance (string)

// Get the Network and token from user input
Expand All @@ -24,10 +22,10 @@ module.exports = async (interaction) => {

if (networks[networkName].nativeCurrency == tokenName) {
//* Token not passed or native Currency (No ERC20 tokens)
balance = await getBalance(interaction, provider);
balance = await getBalance(provider);
} else {
//* Non native token (ERC 20 token)
balance = await getBalance(interaction, provider, tokenName, networkName);
balance = await getBalance(provider, tokenName, networkName);
}

// Rounding off the value
Expand Down Expand Up @@ -61,6 +59,6 @@ module.exports = async (interaction) => {
Date.now()
).toUTCString()}\nGetting Balance\n${error}`
);
await interaction.reply("🙇‍♂️ Error, please try again later");
await interaction.editReply("🙇‍♂️ Error, please try again later");
}
};
Loading

0 comments on commit c917fcf

Please sign in to comment.