93 lines
3.7 KiB
JavaScript
93 lines
3.7 KiB
JavaScript
const Embed = require("../../functions/embed")
|
|
const { updateUserXP, getUserXP } = require("../../models/user");
|
|
const { xpSystem } = require("../../functions/level/xpSystem");
|
|
|
|
module.exports = {
|
|
config: {
|
|
name: "adjustxp",
|
|
cooldown: 5000,
|
|
available: true,
|
|
usage: true,
|
|
permissions: [],
|
|
aliases: [],
|
|
roles: ['staff'],
|
|
dm: false,
|
|
},
|
|
run: async (client, message, args, db) => {
|
|
if (!args[0]) return message.reply("You must mention a user to adjust their XP", false);
|
|
if (message.mentionIds && message.mentionIds.length > 0) {
|
|
let targetUser;
|
|
try {
|
|
const server = await client.servers.fetch(message.server.id);
|
|
const member = await server.fetchMember(message.mentionIds[0]);
|
|
|
|
if (!member || !member.id || !member.id.user) {
|
|
throw new Error("Invalid member structure");
|
|
}
|
|
|
|
// Fetch the user using the user ID from the member object
|
|
targetUser = await client.users.fetch(member.id.user);
|
|
|
|
if (!targetUser || !targetUser.username) {
|
|
throw new Error("Could not fetch user information");
|
|
}
|
|
|
|
} catch (fetchError) {
|
|
console.error("Fetch error details:", fetchError.message);
|
|
return message.reply({
|
|
embeds: [new Embed()
|
|
.setDescription("Could not find the mentioned user.")
|
|
.setColor(`#FF0000`)]
|
|
}, false);
|
|
}
|
|
|
|
if (!args[1]) return message.reply("You must specify an amount to adjust the XP by. `!adjust_xp @USER +/-<AMOUNT>`", false);
|
|
|
|
// Validate the amount is a valid number
|
|
const amountStr = args[1].replace("+", "").replace("-", "");
|
|
const amount = parseInt(amountStr);
|
|
|
|
if (isNaN(amount)) {
|
|
return message.reply({
|
|
embeds: [new Embed()
|
|
.setDescription("Invalid amount specified. Please provide a valid number.")
|
|
.setColor(`#FF0000`)]
|
|
}, false);
|
|
}
|
|
|
|
try {
|
|
const userDoc = await getUserXP(targetUser.id);
|
|
const currentXP = userDoc.xp || 0;
|
|
const newXP = args[1].includes("+") ? currentXP + amount : currentXP - amount;
|
|
|
|
// Ensure the new XP value is not negative
|
|
if (newXP < 0) {
|
|
return message.reply({
|
|
embeds: [new Embed()
|
|
.setDescription("Cannot set XP below 0.")
|
|
.setColor(`#FF0000`)]
|
|
}, false);
|
|
}
|
|
|
|
await updateUserXP(targetUser, newXP);
|
|
const adjusted_amount = args[1].includes("+") ? `+${amount}` : `-${amount}`;
|
|
await xpSystem(client, message);
|
|
return message.reply({
|
|
embeds: [new Embed()
|
|
.setDescription(`XP adjusted for ${targetUser.username} by ${adjusted_amount}`)
|
|
.setColor(`#00FF00`)]
|
|
}, false);
|
|
} catch (error) {
|
|
console.error("Error updating XP:", error);
|
|
return message.reply({
|
|
embeds: [new Embed()
|
|
.setDescription("An error occurred while updating the XP.")
|
|
.setColor(`#FF0000`)]
|
|
}, false);
|
|
}
|
|
} else {
|
|
return message.reply("You must mention a user to adjust their XP", false);
|
|
}
|
|
},
|
|
};
|