add inventory, give and admingive commands. Handle user records in database and minor bugfixes

This commit is contained in:
2025-06-27 12:14:34 +02:00
parent 274b49dd27
commit fa7c45042d
10 changed files with 187 additions and 14 deletions

View File

@@ -1,20 +1,29 @@
import { Item } from ".";
import { changeItemCount, Item } from ".";
import { sendMessage } from "../commands";
import { getUserRecord } from "../db/dbUser";
import parseCommandArgs from "../lib/parseCommandArgs";
import { timeout } from "../lib/timeout";
import { User } from "../user";
export default new Item('blaster', 'Blaster', 's',
const ITEMNAME = 'blaster';
export default new Item(ITEMNAME, 'Blaster', 's',
'Times a specific person out for 60 seconds',
['blaster', 'blast'], ['moderator:manage:banned_users'],
async (msg, user) => {
const userObj = await getUserRecord(user);
if (userObj.inventory[ITEMNAME]! < 1) { await sendMessage(`You don't have any blasters!`, msg.messageId); return; };
const messagequery = parseCommandArgs(msg.messageText);
if (!messagequery[0]) { await sendMessage('Please specify a target'); return; };
const target = await User.initUsername(messagequery[0].toLowerCase());
if (!target) { await sendMessage(`${messagequery[0]} doesn't exist`); return; };
await getUserRecord(target);
if (await user.itemLock()) { await sendMessage('Can\'t use two items at once pepeW', msg.messageId); return; };
await user.setLock();
const result = await timeout(target, `You got blasted by ${user.displayName}!`, 60);
if (result.status) await Promise.all([
sendMessage(`GOTTEM ${target.displayName} got BLASTED by ${user.displayName} GOTTEM`)
sendMessage(`GOTTEM ${target.displayName} got BLASTED by ${user.displayName} GOTTEM`),
changeItemCount(user, userObj, ITEMNAME)
]);
else {
switch (result.reason) {
@@ -24,7 +33,7 @@ export default new Item('blaster', 'Blaster', 's',
case "illegal":
await Promise.all([
sendMessage(`${user.displayName} Nou Nou Nou`),
timeout(user, `You can't just shoot ${target.displayName}!`, 60)
timeout(user, 'nah', 60)
]);
break;
case "unknown":
@@ -32,5 +41,6 @@ export default new Item('blaster', 'Blaster', 's',
break;
};
};
await user.clearLock();
}
);

View File

@@ -1,22 +1,31 @@
import { redis } from "bun";
import { sendMessage } from "../commands";
import { timeout } from "../lib/timeout";
import { Item } from ".";
import { changeItemCount, Item } from ".";
import { User } from "../user";
import { getUserRecord } from "../db/dbUser";
export default new Item('grenade', 'Grenade', 's',
const ITEMNAME = 'grenade';
export default new Item(ITEMNAME, 'Grenade', 's',
'Give a random chatter a 60s timeout',
['grenade'],
['moderator:manage:banned_users'],
async (msg, user) => {
const userObj = await getUserRecord(user);
if (userObj.inventory[ITEMNAME]! < 1) { await sendMessage(`You don't have any grenades!`, msg.messageId); return; };
const targets = await redis.keys('vulnchatters:*');
if (targets.length === 0) { await sendMessage('No vulnerable chatters to blow up', msg.messageId); return; };
const selection = targets[Math.floor(Math.random() * targets.length)]!;
const target = await User.initUserId(selection.split(':')[1]!);
if (await user.itemLock()) { await sendMessage('Can\'t use two items at once pepeW', msg.messageId); return; };
await user.setLock();
await Promise.all([
timeout(target!, `You got hit by ${user.displayName}'s grenade!`, 60),
redis.del(selection),
sendMessage(`wybuh ${target?.displayName} got hit by ${user.displayName}'s grenade wybuh`)
sendMessage(`wybuh ${target?.displayName} got hit by ${user.displayName}'s grenade wybuh`),
changeItemCount(user, userObj, ITEMNAME)
]);
await user.clearLock();
}
);

View File

@@ -29,9 +29,11 @@ export class Item {
};
import { readdir } from 'node:fs/promises';
import type { userRecord } from "../db/connection";
import { updateUserRecord } from "../db/dbUser";
const items = new Map<string, Item>;
const itemintents: string[] = [];
const emptyInventory = {};
const emptyInventory: inventory = {};
const itemarray: string[] = [];
const files = await readdir(import.meta.dir);
@@ -39,7 +41,7 @@ for (const file of files) {
if (!file.endsWith('.ts')) continue;
if (file === import.meta.file) continue;
const item: Item = await import(import.meta.dir + '/' + file.slice(0, -3)).then(a => a.default);
Object.defineProperty(emptyInventory, item.name, { value: 0 });
emptyInventory[item.name] = 0;
itemarray.push(item.name);
itemintents.push(...item.requiredIntents);
for (const alias of item.aliases) {
@@ -49,3 +51,13 @@ for (const file of files) {
export default items;
export { itemintents, emptyInventory, itemarray };
export type inventory = {
[key: string]: number;
};
export async function changeItemCount(user: User, userRecord: userRecord, itemname: string, amount = -1): Promise<false | userRecord> {
userRecord.inventory[itemname] = userRecord.inventory[itemname]! += amount;
if (userRecord.inventory[itemname] < 0) return false;
await updateUserRecord(user, userRecord);
return userRecord;
};