Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
GrishMahat committed Jul 18, 2024
1 parent cec5652 commit 300c6c2
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 92 deletions.
46 changes: 18 additions & 28 deletions src/buttons/requestUserInfoBtn.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,48 @@ export default {
try {
const { guild, member, channel } = interaction;

await interaction.deferReply();
await interaction.deferReply({ ephemeral: true });

// Get the ticket setup configuration
const ticketSetup = await ticketSetupSchema.findOne({
guildID: guild.id,
});
if (!ticketSetup) {
return await interaction.editReply({
content:
'Ticket system is not configured properly. Please contact an administrator.',
ephemeral: true,
});
}

// Check if the member has the staff role
if (!member.roles.cache.has(ticketSetup.staffRoleID)) {
return await interaction.editReply({
content: 'You do not have permission to request user info.',
ephemeral: true,
});
}

// Get the ticket information
const sticket = await ticketSchema.findOne({
// Get the current ticket information
const currentTicket = await ticketSchema.findOne({
guildID: guild.id,
ticketChannelID: channel.id,
closed: false,
});

if (!sticket) {
if (!currentTicket) {
return await interaction.editReply({
content: 'This ticket is not valid or is closed.',
ephemeral: true,
});
}

const user = await guild.members
.fetch(sticket.ticketMemberID)
.catch(() => null);
if (!user) {
return await interaction.editReply({
content: 'User not found in this guild.',
ephemeral: true,
});
}

// Fetch all closed tickets for this user
const tickets = await ticketSchema.find({
guildID: guild.id,
ticketMemberID: user.id,
ticketMemberID: currentTicket.ticketMemberID,
closed: true,
});
}).sort({ closedAt: -1 }); // Sort by closing date, newest first

if (!tickets || tickets.length === 0) {
return await interaction.editReply({
content: 'This user has no closed tickets.',
ephemeral: true,
});
}

Expand Down Expand Up @@ -98,28 +84,32 @@ export default {
{
name: '📅 Created At',
value: ticket.createdAt
? ticket.createdAt.toDateString()
? `<t:${Math.floor(new Date(ticket.createdAt).getTime() / 1000)}:F>`
: 'Unknown',
inline: true,
},
{
name: '⏳ Open Duration',
value: `<t:${Math.floor(new Date(ticket.createdAt).getTime() / 1000)}:R>`,
value: ticket.createdAt
? `<t:${Math.floor(new Date(ticket.createdAt).getTime() / 1000)}:R>`
: 'Unknown',
inline: true,
},
{
name: '🔒 Closed At',
value: `<t:${Math.floor(new Date(ticket.closedAt).getTime() / 1000)}:R>`,
value: ticket.closedAt
? `<t:${Math.floor(new Date(ticket.closedAt).getTime() / 1000)}:R>`
: 'Not closed yet',
inline: true,
},
{
name: '🔖 Claimed By',
value: claimedBy || 'Not claimed',
value: claimedBy,
inline: true,
},
{
name: '📝 Close Reason',
value: closeReason || 'No reason specified',
value: closeReason,
inline: true,
},
{
Expand All @@ -140,12 +130,12 @@ export default {
'An error occurred while processing your request. Please try again later.',
ephemeral: true,
});
} else if (interaction.deferred) {
} else {
await interaction.editReply({
content:
'An error occurred while processing your request. Please try again later.',
});
}
}
},
};
};
92 changes: 60 additions & 32 deletions src/commands/developer/eco.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
/** @format */

import { SlashCommandBuilder, EmbedBuilder } from 'discord.js';
import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } from 'discord.js';
import { Balance, Transaction } from '../../schemas/economy.js';
import mconfig from '../../config/messageConfig.js';

const COLORS = {
DEFAULT: 0x7289DA, // Discord blurple
SUCCESS: 0x43B581, // Discord green
WARNING: 0xFAA61A, // Discord yellow
INFO: 0x5865F2, // Discord blue
ERROR: 0xED4245 // Discord red
};


export default {
data: new SlashCommandBuilder()
.setName('eco')
.setDescription('Economy management commands')
.addSubcommand((subcommand) =>
subcommand
.setName('add')
.setDescription("Add clienterr coins to a user's balance")
.setDescription("Add coins to a user's balance")
.addUserOption((option) =>
option
.setName('user')
.setDescription('The user you want to add clienterr coins to')
.setDescription('The user you want to add coins to')
.setRequired(true)
)
.addIntegerOption((option) =>
option
.setName('amount')
.setDescription('The amount of clienterr coins to add')
.setDescription('The amount of coins to add')
.setRequired(true)
.setMinValue(1)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName('subtract')
.setDescription("Subtract clienterr coins from a user's balance")
.setDescription("Subtract coins from a user's balance")
.addUserOption((option) =>
option
.setName('user')
.setDescription(
'The user you want to subtract clienterr coins from'
)
.setDescription('The user you want to subtract coins from')
.setRequired(true)
)
.addIntegerOption((option) =>
option
.setName('amount')
.setDescription('The amount of clienterr coins to subtract')
.setDescription('The amount of coins to subtract')
.setRequired(true)
.setMinValue(1)
)
Expand Down Expand Up @@ -75,35 +82,48 @@ export default {
.setRequired(true)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName('reset')
.setDescription("Reset a user's balance to 0")
.addUserOption((option) =>
option
.setName('user')
.setDescription('The user whose balance you want to reset')
.setRequired(true)
)
)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.toJSON(),
userPermissions: [],
userPermissions: ['Administrator'],
botPermissions: [],
cooldown: 5, // Cooldown in seconds
cooldown: 5,
nsfwMode: false,
testMode: false,
devOnly: true,


run: async (client, interaction) => {
const subcommand = interaction.options.getSubcommand();
const user = interaction.options.getUser('user');
const amount = interaction.options.getInteger('amount');

try {
let userBalance = await Balance.findOneAndUpdate(
{ userId: user.id },
{ $setOnInsert: { balance: 0, bank: 0 } },
{ upsert: true, new: true }
);

let responseMessage;
let color;
let color = COLORS.DEFAULT; // Default color
let transactionType;

switch (subcommand) {
case 'add':
userBalance.balance += amount;
responseMessage = `Added ${amount} clienterr coins to ${user.username}'s balance. New balance: ${userBalance.balance} clienterr coins.`;
color = mconfig.embedColorSuccess;
responseMessage = `Added ${amount} coins to ${user.username}'s balance. New balance: ${userBalance.balance} coins.`;
color = COLORS.SUCCESS;
transactionType = 'add';
break;
case 'subtract':
Expand All @@ -113,39 +133,47 @@ export default {
createErrorEmbed(
interaction,
'Insufficient Balance',
`${user.username} does not have enough balance to subtract ${amount} clienterr coins.`
`${user.username} does not have enough balance to subtract ${amount} coins.`
),
],
ephemeral: true,
});
}
userBalance.balance -= amount;
responseMessage = `Subtracted ${amount} clienterr coins from ${user.username}'s balance. New balance: ${userBalance.balance} clienterr coins.`;
color = mconfig.embedColorWarning;
responseMessage = `Subtracted ${amount} coins from ${user.username}'s balance. New balance: ${userBalance.balance} coins.`;
color = COLORS.WARNING;
transactionType = 'subtract';
break;
case 'set':
userBalance.balance = amount;
responseMessage = `Set ${user.username}'s balance to ${amount} clienterr coins.`;
color = mconfig.embedColorInfo;
responseMessage = `Set ${user.username}'s balance to ${amount} coins.`;
color = COLORS.INFO;
transactionType = 'set';
break;
case 'view':
responseMessage = `${user.username}'s current balance: ${userBalance.balance} clienterr coins.`;
color = mconfig.embedColorDefault;
responseMessage = `${user.username}'s current balance: ${userBalance.balance} coins.`;
// color remains default
break;
case 'reset':
userBalance.balance = 0;
responseMessage = `Reset ${user.username}'s balance to 0 coins.`;
color = COLORS.WARNING;
transactionType = 'reset';
break;
default:
throw new Error('Invalid subcommand');
}

if (subcommand !== 'view') {
await userBalance.save();
await Transaction.create({
userId: user.id,
type: transactionType,
amount: amount,
amount: subcommand === 'reset' ? userBalance.balance : amount,
executorId: interaction.user.id,
});
}

const embed = new EmbedBuilder()
.setDescription(responseMessage)
.setColor(color)
Expand All @@ -158,27 +186,27 @@ export default {
}),
})
.setTimestamp();

await interaction.reply({ embeds: [embed] });
} catch (error) {
console.error('Economy command error:', error);
await interaction.reply({
embeds: [
createErrorEmbed(
interaction,
'Error',
'There was an error processing your request.'
'There was an error processing your request. Please try again later.'
),
],
ephemeral: true,
});
throw error;
}
},
};

function createErrorEmbed(interaction, title, description) {
return new EmbedBuilder()
.setColor(mconfig.embedColorError)
.setColor(COLORS.ERROR)
.setTitle(`❌ ${title}`)
.setDescription(description)
.setFooter({
Expand Down
Loading

0 comments on commit 300c6c2

Please sign in to comment.