mirror of
https://github.com/veganhacktivists/arabot.git
synced 2025-05-18 13:34:14 +02:00
feat(arabot): add xp system
This commit is contained in:
parent
15f2f710a9
commit
80f38b2ca5
13
prisma/migrations/20230304234222_xp/migration.sql
Normal file
13
prisma/migrations/20230304234222_xp/migration.sql
Normal file
@ -0,0 +1,13 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "Xp" (
|
||||
"userId" TEXT NOT NULL,
|
||||
"level" INTEGER NOT NULL DEFAULT 0,
|
||||
"xp" INTEGER NOT NULL DEFAULT 0,
|
||||
"messageCount" INTEGER NOT NULL DEFAULT 0,
|
||||
"lastMessage" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "Xp_pkey" PRIMARY KEY ("userId")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Xp" ADD CONSTRAINT "Xp_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
@ -37,6 +37,7 @@ model User {
|
||||
muted Boolean @default(false)
|
||||
VerifyUser Verify[] @relation("verUser")
|
||||
VerifyVerifier Verify[] @relation("verVerifier")
|
||||
Xp Xp?
|
||||
Balance Balance?
|
||||
Daily Daily[]
|
||||
SendPayment Payment[] @relation("sendPayment")
|
||||
@ -94,6 +95,15 @@ model Verify {
|
||||
notes String?
|
||||
}
|
||||
|
||||
model Xp {
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
userId String @id
|
||||
level Int @default(0)
|
||||
xp Int @default(0)
|
||||
messageCount Int @default(0)
|
||||
lastMessage DateTime @default(now())
|
||||
}
|
||||
|
||||
// Economy
|
||||
|
||||
model Balance {
|
||||
|
48
src/listeners/xp.ts
Normal file
48
src/listeners/xp.ts
Normal file
@ -0,0 +1,48 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
/*
|
||||
Animal Rights Advocates Discord Bot
|
||||
Copyright (C) 2023 Anthony Berg
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Listener } from '@sapphire/framework';
|
||||
import type { Message } from 'discord.js';
|
||||
import { addXp, checkCanAddXp } from '#utils/database/xp';
|
||||
import { randint } from '#utils/maths';
|
||||
|
||||
export class XpListener extends Listener {
|
||||
public constructor(context: Listener.Context, options: Listener.Options) {
|
||||
super(context, {
|
||||
...options,
|
||||
event: 'messageCreate',
|
||||
});
|
||||
}
|
||||
|
||||
public async run(message: Message) {
|
||||
const user = message.author;
|
||||
|
||||
if (user.bot) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!await checkCanAddXp(user.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const xp = randint(15, 25);
|
||||
|
||||
await addXp(user.id, xp);
|
||||
}
|
||||
}
|
70
src/utils/database/xp.ts
Normal file
70
src/utils/database/xp.ts
Normal file
@ -0,0 +1,70 @@
|
||||
import { container } from '@sapphire/framework';
|
||||
import { Time } from '@sapphire/time-utilities';
|
||||
import type { Snowflake } from 'discord.js';
|
||||
|
||||
function xpToNextLevel(level: number, xp: number) {
|
||||
return 5 * (level * level) + (50 * level) + 100 - xp;
|
||||
}
|
||||
|
||||
export async function addXp(userId: Snowflake, xp: number) {
|
||||
const user = await container.database.xp.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
select: {
|
||||
xp: true,
|
||||
level: true,
|
||||
},
|
||||
});
|
||||
|
||||
let level = 0;
|
||||
if (user !== null
|
||||
&& xpToNextLevel(user.level, user.xp + xp) < 0) {
|
||||
level = 1;
|
||||
}
|
||||
|
||||
await container.database.xp.upsert({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
update: {
|
||||
xp: { increment: xp },
|
||||
level: { increment: level },
|
||||
messageCount: { increment: 1 },
|
||||
lastMessage: new Date(),
|
||||
},
|
||||
create: {
|
||||
user: {
|
||||
connectOrCreate: {
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
create: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
messageCount: 1,
|
||||
xp,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function checkCanAddXp(userId: Snowflake) {
|
||||
const message = await container.database.xp.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
select: {
|
||||
lastMessage: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (message === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const cooldown = Time.Minute;
|
||||
|
||||
return Date.now() - message.lastMessage.getTime() > cooldown;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user