Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix prefix command #67

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TOKEN=
MONGODB_TOKEN=
GITHUB_TOKEN=
ERROR_WEBHOOK_URL=
ERROR_WEBHOOK_URL=
APPLICATION_ID=
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: 1 addition & 0 deletions src/commands/economy/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default {
devOnly: false,
dmAllowed: true,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
try {
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/beg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/coinflip.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/crime.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/daily.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/hourly.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
try {
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
1 change: 1 addition & 0 deletions src/commands/economy/weekly.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
testMode: false,
devOnly: false,
category: 'economy',
prefix: true,

run: async (client, interaction) => {
const userId = interaction.user.id;
Expand Down
2 changes: 1 addition & 1 deletion 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
13 changes: 7 additions & 6 deletions src/events/validations/seclectMenuValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const loadSelects = async (errorHandler) => {
for (const select of selectFiles) {
if (select && select.customId) {
selects.set(select.customId, select);
} else {
}
}
console.log(`Loaded ${selects.size} select menu commands`.green);
Expand All @@ -59,7 +58,11 @@ const handleSelect = async (client, errorHandler, interaction) => {
);
}

if (selectObject.testMode && interaction.guild.id !== testServerId) {
if (
selectObject.testMode &&
interaction.guild &&
interaction.guild.id !== testServerId
) {
return sendEmbedReply(
interaction,
mConfig.embedColorError,
Expand Down Expand Up @@ -100,7 +103,6 @@ const handleSelect = async (client, errorHandler, interaction) => {
);
}

// Check cooldown
if (selectObject.cooldown) {
const cooldownKey = `${interaction.user.id}-${customId}`;
const cooldownTime = selects.get(cooldownKey);
Expand All @@ -118,18 +120,17 @@ const handleSelect = async (client, errorHandler, interaction) => {
try {
await selectObject.run(client, interaction);
console.log(
`Select menu ${interaction.customId} used by ${interaction.user.tag} in ${interaction.guild.name}`
`Select menu ${interaction.customId} used by ${interaction.user.tag} in ${interaction.guild?.name}`
.yellow
);
} catch (error) {
console.error(`Error executing select menu ${customId}:`.red, error);

// Use errorHandler to handle the error
await errorHandler.handleError(error, {
type: 'selectError',
selectId: customId,
userId: interaction.user.id,
guildId: interaction.guild.id,
guildId: interaction.guild?.id,
});

sendEmbedReply(
Expand Down
Loading