Skip to content

Commit

Permalink
better error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
GrishMahat committed Jul 29, 2024
1 parent 147c5e8 commit cbf9a09
Show file tree
Hide file tree
Showing 10 changed files with 655 additions and 67 deletions.
158 changes: 158 additions & 0 deletions src/commands/developer/emojiapp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import axios from 'axios';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';

const data = new SlashCommandBuilder()
.setName('emoji-app')
.setDescription('Manage app emojis')
.addSubcommand((command) =>
command
.setName('create')
.setDescription('Create an app emoji')
.addAttachmentOption((option) =>
option
.setName('emoji')
.setDescription('The emoji to add')
.setRequired(true)
)
.addStringOption((option) =>
option
.setName('name')
.setDescription('The name of the emoji')
.setRequired(true)
)
)
.addSubcommand((command) =>
command
.setName('remove')
.setDescription('Remove an app emoji')
.addStringOption((option) =>
option
.setName('emoji-id')
.setDescription('The ID of the emoji to remove')
.setRequired(true)
)
)
.addSubcommand((command) =>
command.setName('list').setDescription('List all app emojis')
)
.toJSON();

export default {
data,
userPermissions: [],
botPermissions: [],
category: 'Misc',
cooldown: 5,
nsfwMode: false,
testMode: false,
devOnly: false,
prefix: false,

run: async (client, interaction) => {
try {
const { options } = interaction;
const subCommand = options.getSubcommand();
const applicationId = process.env.APPLICATION_ID;
const token = process.env.TOKEN;

const sendMessage = async (message) => {
const embed = new EmbedBuilder()
.setColor('Random')
.setDescription(message);
await interaction.reply({ embeds: [embed], ephemeral: true });
};

const apiCall = async (type, data) => {
let output;
const config = {
headers: {
Authorization: `Bot ${token}`,
},
};

switch (type) {
case 'create':
const createResponse = await axios.post(
`https://discord.com/api/v10/applications/${applicationId}/emojis`,
data,
config
);
output = createResponse.data;
break;
case 'remove':
await axios.delete(
`https://discord.com/api/v10/applications/${applicationId}/emojis/${data['emoji-id']}`,
config
);
output = `Removed emoji with ID: ${data['emoji-id']}`;
break;

case 'list':
const listResponse = await axios.get(
`https://discord.com/api/v10/applications/${applicationId}/emojis`,
config
);
output = listResponse.data;
break;
default:
throw new Error('Unknown command type');
}
return output;
};

let responseMessage;
switch (subCommand) {
case 'create':
const emoji = options.getAttachment('emoji');
const name = options.getString('name');

const response = await axios.get(emoji.url, {
responseType: 'arraybuffer',
});
const buffer = Buffer.from(response.data, 'binary');
const base64Image = buffer.toString('base64');

const createData = {
name,
image: `data:image/png;base64,${base64Image}`,
};

const createOutput = await apiCall('create', createData);
if (!createOutput) {
await sendMessage(
'There was an issue creating the emoji. Please check the provided data.'
);
} else {
await sendMessage(
`<:${createOutput.name}:${createOutput.id}> I have created the emoji. Use \`<:${createOutput.name}:${createOutput.id}>\` in your messages.`
);
}
break;

case 'remove':
responseMessage = await apiCall('remove', {
'emoji-id': options.getString('emoji-id'),
});
await sendMessage(responseMessage);
break;

case 'list':
const emojis = await apiCall('list');
responseMessage = 'List of all emojis:\n';
emojis.forEach((emoji) => {
responseMessage += `<:${emoji.name}:${emoji.id}> \`${emoji.name}\` (ID: ${emoji.id})\n`;
});
await sendMessage(responseMessage);
break;

default:
throw new Error('Invalid subcommand');
}
} catch (error) {
console.error(error);
await interaction.editReply(
'An error occurred while processing the command. Please try again later.'
);
}
},
};
1 change: 0 additions & 1 deletion src/commands/economy/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default {
category: 'economy',
prefix: true,


run: async (client, interaction) => {
const userId = interaction.user.id;

Expand Down
1 change: 0 additions & 1 deletion src/commands/economy/crime.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default {
category: 'economy',
prefix: true,


run: async (client, interaction) => {
const userId = interaction.user.id;
const CrimeCooldown = 6 * 60 * 60 * 1000; // 6 hours in milliseconds
Expand Down
1 change: 0 additions & 1 deletion src/commands/economy/shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default {
category: 'economy',
prefix: true,


run: async (client, interaction) => {
try {
const items = await Item.find().lean();
Expand Down
1 change: 0 additions & 1 deletion src/commands/economy/slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default {
category: 'economy',
prefix: true,


run: async (client, interaction) => {
const userId = interaction.user.id;
const betAmount = interaction.options.getNumber('bet');
Expand Down
4 changes: 2 additions & 2 deletions src/events/messageCreate/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function checkCommandPrerequisites(message, command) {

async function handleCommandError(err, command, message, errorHandler) {
await errorHandler.handleError(err, {
type: 'commandError',
type: 'prefixcommandError',
commandName: command.name,
userId: message.author.id,
guildId: message.guild?.id,
Expand Down Expand Up @@ -215,4 +215,4 @@ function applyCooldown(userId, commandName, cooldownTime) {
setTimeout(() => timestamps.delete(userId), cooldownAmount);

return { active: false };
}
}
Loading

0 comments on commit cbf9a09

Please sign in to comment.