Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Dec 5, 2024
1 parent fc3758c commit 603ad13
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 67 deletions.
17 changes: 10 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ async function getGuildData(guildId) {
if (!guild) {
guild = new Guild({
guildId: guildId,
levelingEnabled: true,
users: [],
});
await guild.save();
Expand Down Expand Up @@ -116,21 +117,23 @@ async function updateUserLevel(guildId, userId, xpGain) {
const { guild, user } = await getUserInGuild(userId, guildId);

user.xp += xpGain;
let nextLevelXP = user.level * 75;
let nextLevelXP = user.level * 50;
let xpNeededForNextLevel = user.level * nextLevelXP;
let previousXPNeeded = xpNeededForNextLevel;
let previousLevel = user.level;
let currentLevel = user.level;

user.messageTimeout = Date.now();

while (user.xp >= xpNeededForNextLevel) {
user.level += 1;
nextLevelXP = user.level * 75;
nextLevelXP = user.level * 50;
xpNeededForNextLevel = user.level * nextLevelXP;
currentLevel = user.level;
}

await guild.save();

return { previousXPNeeded, xpNeededForNextLevel };
return { xpNeededForNextLevel, previousLevel, currentLevel };
}

client.on("messageCreate", async (message) => {
Expand All @@ -146,21 +149,21 @@ client.on("messageCreate", async (message) => {

if (Date.now() - user.messageTimeout >= 60000 && guild.levelingEnabled) {
const xpGain = Math.floor(Math.random() * 15) + 10;
const { previousXPNeeded, xpNeededForNextLevel } = await updateUserLevel(
const { previousLevel, xpNeededForNextLevel, currentLevel } = await updateUserLevel(
guildId,
userId,
xpGain,
);

if (user.xp >= previousXPNeeded) {
if (currentLevel > previousLevel) {
const levelbed = new MessageEmbed()
.setColor("#3498db")
.setTitle("Level Up!")
.setAuthor(
message.author.username,
message.author.displayAvatarURL({ dynamic: true }),
)
.setDescription(`You have reached level ${user.level}`)
.setDescription(`You have reached level ${currentLevel}`)
.setFooter(`XP: ${user.xp}/${xpNeededForNextLevel}`);

message.channel.send({ embeds: [levelbed] });
Expand Down
2 changes: 1 addition & 1 deletion src/commands/economy/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = class extends Command {
cooldown: 3,
})
}
async run(message, args) {
async run(message) {
const user = message.mentions.members.first() || message.author;

const profile = await Profile.findOne({
Expand Down
51 changes: 36 additions & 15 deletions src/commands/economy/pay.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ module.exports = class extends Command {
category: "Economy",
usage: "pay <user> <amount>",
examples: "pay @Peter 500",
cooldown: 5,
cooldown: 5
});
}
async run(message, args) {
const user = message.mentions.members.first();
const amount = args.slice(1).join("");
const profile = await Profile.findOne({ userID: message.author.id, guildId: message.guild.id });
const profileChecker = await Profile.findOne({ userID: user.id, guildId: message.guild.id });
if (!profile) {
await createProfile(message.author, message.guild);
await message.channel.sendCustom({
Expand Down Expand Up @@ -45,20 +46,40 @@ module.exports = class extends Command {
]
});
} else {
await Profile.updateOne({
userID: message.author.id, guildId: message.guild.id
}, { $inc: { wallet: -amount } });
await Profile.updateOne({
userID: user.id, guildId: message.guild.id
}, { $inc: { wallet: amount } });
await message.channel.sendCustom({
embeds: [
new MessageEmbed()
.setColor(message.client.color.green)
.setDescription(`You payed $${amount} to ${user}.`)
]
});
if (!profileChecker) {
await message.channel.sendCustom({
embeds: [
new MessageEmbed()
.setColor("RED")
.setDescription(`${user} does not have a profile!`)
]
});
} else {
await Profile.updateOne({
userID: message.author.id,
guildId: message.guild.id
}, {
$inc: {
wallet: - amount
}
});
await Profile.updateOne({
userID: user.id,
guildId: message.guild.id
}, {
$inc: {
wallet: amount
}
});
await message.channel.sendCustom({
embeds: [
new MessageEmbed()
.setColor(message.client.color.green)
.setDescription(`You payed $${amount} to ${user}.`)
]
});
}
}
}
}
};
};
2 changes: 1 addition & 1 deletion src/commands/economy/rob.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = class extends Command {
examples: "rob @Peter"
})
}
async run(message, args) {
async run(message) {
const user = message.mentions.members.first();
const profile = await Profile.findOne({ userID: message.author.id, guildId: message.guild.id });
if (!profile) {
Expand Down
12 changes: 3 additions & 9 deletions src/commands/rank/addxp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// AddXPCommand.js
const Command = require("../../structures/Command");
const fs = require("fs");
const { MessageEmbed, MessageActionRow, MessageButton } = require("discord.js");
const { MessageEmbed } = require("discord.js");
const Guild = require("../../database/models/leveling");

module.exports = class extends Command {
Expand Down Expand Up @@ -50,7 +49,7 @@ module.exports = class extends Command {
});
}

let nextLevelXP = user.level * 75;
let nextLevelXP = user.level * 50;
let xpNeededForNextLevel = user.level * nextLevelXP;

if (!(amount >= 1000000000)) {
Expand All @@ -76,12 +75,7 @@ module.exports = class extends Command {
)
.setFooter(`XP: ${user.xp}/${xpNeededForNextLevel}`);

const row = new MessageActionRow().addComponents(
new MessageButton()
.setCustomId("levelup")
.setLabel("Level Up")
.setStyle("SUCCESS"),
);

if(user.level - previousLevel >= 1) {
message.channel.sendCustom({
embeds: [levelbed],
Expand Down
1 change: 1 addition & 0 deletions src/commands/rank/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = class extends Command {
name: "leaderboard",
description: "Display the server's leaderboard based on levels.",
category: "Leveling",
aliases: ["leaderb", "lb", "lboard"],
cooldown: 5,
guildOnly: true,
});
Expand Down
15 changes: 9 additions & 6 deletions src/commands/rank/leveltoggle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// LevelingToggleCommand.js

const Command = require("../../structures/Command");
const fs = require("fs");
const Guild = require("../../database/models/leveling");
let toggle = true;

Expand Down Expand Up @@ -44,16 +43,20 @@ module.exports = class extends Command {
// Update the guild's levelingEnabled property
if (action === "enable") {
toggle = true;
userData.guilds[guildId].levelingEnabled = toggle;
message.reply("Leveling system enabled for this server.");
await guild.updateOne({
levelingEnabled: toggle
});
message.channel.send("Leveling system enabled for this server.");
} else if (action === "disable") {
toggle = false;
userData.guilds[guildId].levelingEnabled = toggle;
message.reply("Leveling system disabled for this server.");
await guild.updateOne({
levelingEnabled: toggle
});
message.channel.send("Leveling system disabled for this server.");
} else {
return message.reply("Invalid action. Use `enable` or `disable`.");
}
} catch (erorr) {
} catch (error) {
console.error("Error occured: ", error);
message.reply("An error occured while updating the leveling system.");
}
Expand Down
8 changes: 4 additions & 4 deletions src/commands/rank/rank.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Command = require("../../structures/Command");
const { createCanvas, loadImage } = require("canvas");
const { MessageAttachment, MessageEmbed } = require("discord.js");
const { MessageAttachment } = require("discord.js");
const Guild = require("../../database/models/leveling");

// Calculate the required XP for a certain level
function calculateRequiredXP(level) {
const baseXP = 75;
const increment = level * 75;
const baseXP = 50;
const increment = level * 50;
const xpNeeded = level * increment;
if (level === 0) {
return baseXP + xpNeeded;
Expand All @@ -16,7 +16,7 @@ function calculateRequiredXP(level) {
}

function abbreviateNumber(number) {
return number >= 1e12 ? `${(number / 1e12).toFixed(2)}T` : number >= 1e9 ? `${(number / 1e9).toFixed(2)}B` : number >= 1e6 ? `${(number / 1e6).toFixed(2)}M` : number >= 1e3 ? `${(number / 1e3).toFixed(2)}K` : number.toString();
return number >= 1e12 ? `${(number / 1e12).toFixed(2)}T` : number >= 1e9 ? `${(number / 1e9).toFixed(2)}B` : number >= 1e6 ? `${(number / 1e6).toFixed(2)}M` : number >= 1e3 ? `${(number / 1e3).toFixed(2)}K` : number.toString();
}


Expand Down
3 changes: 1 addition & 2 deletions src/commands/rank/removexp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// AddXPCommand.js
const Command = require("../../structures/Command");
const fs = require("fs");
const { MessageEmbed, MessageActionRow, MessageButton } = require("discord.js");
const Guild = require("../../database/models/leveling");

Expand Down Expand Up @@ -50,7 +49,7 @@ module.exports = class extends Command {
});
}

let nextLevelXP = user.level * 75;
let nextLevelXP = user.level * 50;
let xpNeededForNextLevel = user.level * nextLevelXP;

if (!(amount >= 1000000000)) {
Expand Down
2 changes: 0 additions & 2 deletions src/commands/rank/setbackground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const Command = require("../../structures/Command");
const Guild = require("../../database/models/leveling");
const fs = require("fs");
const guildData = require("../../data/users.json");
module.exports = class extends Command {
constructor(...args) {
super(...args, {
Expand Down
14 changes: 3 additions & 11 deletions src/commands/rank/setlevelxp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const Command = require("../../structures/Command");
const fs = require("fs");
const { MessageEmbed, MessageActionRow, MessageButton } = require("discord.js");
const { MessageEmbed } = require("discord.js");
const Guild = require("../../database/models/leveling");

module.exports = class extends Command {
Expand Down Expand Up @@ -46,14 +45,14 @@ module.exports = class extends Command {

const actualLevelAmount = amount - 1;

let nextLevelXP = actualLevelAmount * 75;
let nextLevelXP = actualLevelAmount * 50;
let nextLevel = amount;
let xpNeededForNextLevel = actualLevelAmount * nextLevelXP;

if (!(amount >= 1000)) {
user.xp = actualLevelAmount * nextLevelXP;
user.level = amount;
nextLevelXP = nextLevel * 75;
nextLevelXP = nextLevel * 50;
xpNeededForNextLevel = nextLevel * nextLevelXP;
} else {
const embed = new MessageEmbed().setDescription(
Expand All @@ -70,13 +69,6 @@ module.exports = class extends Command {
.setFooter(
`XP: ${user.xp}/${xpNeededForNextLevel}`,
);

const row = new MessageActionRow().addComponents(
new MessageButton()
.setCustomId("levelup")
.setLabel("Level Up")
.setStyle("SUCCESS"),
);
message.channel.sendCustom({
embeds: [levelbed],
});
Expand Down
60 changes: 60 additions & 0 deletions src/slashCommands/economy/deposit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageEmbed } = require("discord.js");
const Profile = require("../../database/models/economy/profile");
const { createProfile } = require("../../utils/utils");

module.exports = {
data: new SlashCommandBuilder()
.setName("deposit")
.setDescription("Deposit a number of money to the bank.")
.addStringOption((option) => option.setName("amount").setDescription("The amount to deposit.").setRequired(true)),
async execute(interaction) {
const profile = await Profile.findOne({ userID: interaction.user.id, guildId: interaction.guild.id });
if (!profile) {
await createProfile(interaction.user, interaction.guild);
await interaction.reply({
embeds: [
new MessageEmbed()
.setColor("BLURPLE")
.setDescription(`Creating profile.\nUse this command again.`)
],
ephemeral: true
});
} else {
const amount = interaction.options.getString("amount");
if (amount === "all") {
await Profile.updateOne({
userID: interaction.user.id, guildId: interaction.guild.id
}, { $inc: { wallet: -profile.wallet, bank: profile.wallet } })
await interaction.reply({
embeds: [
new MessageEmbed()
.setColor("BLURPLE")
.setDescription(`Deposited $${profile.wallet} to your bank.`)
]
});
} else if (amount > profile.wallet) {
await interaction.reply({
embeds: [
new MessageEmbed()
.setColor("BLURPLE")
.setDescription(`You don't have enough money to deposit!`)
],
ephemeral: true
});
} else {
await Profile.updateOne({
userID: interaction.user.id, guildId: interaction.guild.id
},
{ $inc: { wallet: -amount, bank: amount } });
await interaction.reply({
embeds: [
new MessageEmbed()
.setColor("BLURPLE")
.setDescription(`Deposited $${amount} to your bank.`)
]
});
}
}
}
}
2 changes: 1 addition & 1 deletion src/slashCommands/moderation/ban.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
const logging = await Logging.findOne({
guildId: interaction.guild.id,
});
if (!interaction.member.permissions.has("MODERATE_MEMBERS"))
if (!interaction.member.permissions.has("BAN_MEMBERS"))
return interaction.followUp({
content: "You do not have permission to use this command.",
});
Expand Down
2 changes: 1 addition & 1 deletion src/slashCommands/moderation/clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module.exports = {
"There was an error trying to delete messages in this channel.",
});
}
await delay(5000);
await delay(10000);
}

const embed = new MessageEmbed()
Expand Down
Loading

0 comments on commit 603ad13

Please sign in to comment.