Skip to content

Commit

Permalink
Merge pull request #26 from N3aar/feat/exp-adjustments
Browse files Browse the repository at this point in the history
feat: set level command and exp adjustments
  • Loading branch information
N3aar authored Jun 12, 2024
2 parents 1bfbae8 + b9b0601 commit cc750a0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ExpHandler from "@/structures/xpHandler.js";
import ExpHandler from "@/shared/handlers/xpHandler.js";
import { PrismaClient } from "@prisma/client";
import { SapphireClient, container } from "@sapphire/framework";
import type { ClientOptions } from "discord.js";
Expand Down
59 changes: 59 additions & 0 deletions src/commands/admin/setlevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { adminPermission } from "@/utils/contants.js";
import { Command } from "@sapphire/framework";

export class SetLevelCommand extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, { ...options });
}

public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder
.setName("setlevel")
.setDescription("Definir o level de outro usuário")
.setDefaultMemberPermissions(adminPermission)
.addUserOption((option) => {
option.setName("usuario");
option.setDescription("Usuário que receberá o nível");
option.setRequired(true);
return option;
})
.addNumberOption((option) => {
option.setName("level");
option.setDescription("Nivel que o usuário irá receber");
option.setMinValue(1);
option.setRequired(true);
return option;
}),
);
}

public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const user = interaction.options.getUser("usuario");
const level = interaction.options.getNumber("level");

if (!user || !level) return;

await this.container.db.user.upsert({
where: {
discordId: user.id,
},
update: {
level,
xp: 0,
},
create: {
discordId: user.id,
level,
},
});

this.container.expHandler.deleteMemberFromCache(user.id);

await interaction.reply({
content: "Nivel do usuário definido com sucesso!",
ephemeral: true,
fetchReply: false,
});
}
}
12 changes: 9 additions & 3 deletions src/structures/xpHandler.ts → src/shared/handlers/xpHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expValues } from "@/utils/contants.js";
import { getRandomNumber } from "@/utils/random.js";
import { container } from "@sapphire/pieces";
import type { Guild, GuildMember, TextChannel } from "discord.js";

Expand Down Expand Up @@ -36,7 +37,7 @@ export default class ExpHandler {
}

private getRequiredExpAmount(level: number): number {
return Math.floor(expValues.xpBase * expValues.xpFactor ** level);
return 5 * level ** 2 + 50 * level + 100;
}

private async fetchUserStats(member: GuildMember): Promise<ExpStats> {
Expand All @@ -53,12 +54,16 @@ export default class ExpHandler {
};
}

public deleteMemberFromCache(id: string) {
this.users.delete(id);
}

public async addExp(member: GuildMember, guild: Guild, channel: TextChannel) {
const expStats = await this.getStats(member, guild);

if (!expStats?.canGetExp) return;

expStats.exp += expValues.byMessage;
expStats.exp += getRandomNumber(expValues.min, expValues.max);
expStats.canGetExp = false;

const newXp = expStats.exp;
Expand Down Expand Up @@ -119,6 +124,7 @@ export default class ExpHandler {
const requiredExp = this.getRequiredExpAmount(level);
const progress = Math.floor((exp / requiredExp) * length);
const bar = "▰".repeat(progress) + "▱".repeat(length - progress);
return `[${bar}] ${exp}/${requiredExp}`;

return `${bar} ${exp}/${requiredExp}`;
}
}
6 changes: 3 additions & 3 deletions src/utils/contants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export const birthdayVideos = [
"https://utfs.io/f/d82a891f-54b2-4322-aaa2-618b3ebf3cde-z56zz.mp4",
"https://utfs.io/f/8f15b466-6ac0-418d-91e5-3a1ad8a4866a-z5700.mp4",
"https://utfs.io/f/50ff0549-c66c-49c2-8d78-91002251b4a0-z5701.mp4",
"https://utfs.io/f/d5265906-9676-48a0-8a1e-8bb5e7f69826-z5702.mp4",
];

export const expValues = {
byMessage: 3,
xpBase: 25,
xpFactor: 1.5,
min: 10,
max: 25,
cooldown: 60 * 1000,
};

0 comments on commit cc750a0

Please sign in to comment.