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

@@ -38,7 +38,7 @@ async function initAuth(userId: string, clientId: string, clientSecret: string,
await deleteAuthRecord(userId); await deleteAuthRecord(userId);
const code = await codepromise; const code = await codepromise;
await server.stop(true); server.stop(false);
console.info(`Authentication code received.`); console.info(`Authentication code received.`);
const tokenData = await exchangeCode(clientId, clientSecret, code, redirectURL); const tokenData = await exchangeCode(clientId, clientSecret, code, redirectURL);
console.info(`Successfully authenticated code.`); console.info(`Successfully authenticated code.`);

30
bot/commands/admingive.ts Normal file
View File

@@ -0,0 +1,30 @@
import { Command, sendMessage } from ".";
import { unbannableUsers } from "..";
import { getUserRecord } from "../db/dbUser";
import items, { changeItemCount } from "../items";
import parseCommandArgs from "../lib/parseCommandArgs";
import { User } from "../user";
export default new Command('admingive', ['admingive'], [], async msg => {
if (!unbannableUsers.includes(msg.chatterId)) { await sendMessage('nah', msg.messageId); return; };
const args = parseCommandArgs(msg.messageText);
if (!args[0]) { await sendMessage('Please specify a user', msg.messageId); return; };
const target = await User.initUsername(args[0].toLowerCase());
if (!target) { await sendMessage(`Chatter ${args[0]} doesn't exist`, msg.messageId); return; };
const userRecord = await getUserRecord(target);
if (!args[1]) { await sendMessage('Please specify an item to give', msg.messageId); return; };
const item = items.get(args[1].toLowerCase());
if (!item) { await sendMessage(`Item ${args[1]} doesn't exist`, msg.messageId); return; };
if (!args[2]) { await sendMessage('Please specify the amount of the item you want to give', msg.messageId); return; };
const amount = Number(args[2]);
if (isNaN(amount)) { await sendMessage(`${args[2]} is not a valid amount`); return; };
await target.setLock();
const data = await changeItemCount(target, userRecord, item.name, amount);
if (data) {
const newamount = data.inventory[item.name]!;
await sendMessage(`${target.displayName} now has ${newamount} ${item.prettyName + (newamount === 1 ? '' : item.plural)}`, msg.messageId);
} else {
await sendMessage(`Failed to give ${target.displayName} ${amount} ${item.prettyName + (amount === 1 ? '' : item.plural)}`, msg.messageId);
};
await target.clearLock();
});

41
bot/commands/give.ts Normal file
View File

@@ -0,0 +1,41 @@
import { Command, sendMessage } from ".";
import type { userRecord } from "../db/connection";
import { getUserRecord } from "../db/dbUser";
import items, { changeItemCount } from "../items";
import parseCommandArgs from "../lib/parseCommandArgs";
import { User } from "../user";
export default new Command('give', ['give'], [], async (msg, user) => {
const args = parseCommandArgs(msg.messageText);
if (!args[0]) { await sendMessage('Please specify a user', msg.messageId); return; };
const target = await User.initUsername(args[0].toLowerCase());
if (!target) { await sendMessage(`Chatter ${args[0]} doesn't exist`, msg.messageId); return; };
const targetRecord = await getUserRecord(target);
if (!args[1]) { await sendMessage('Please specify an item to give', msg.messageId); return; };
const item = items.get(args[1].toLowerCase());
if (!item) { await sendMessage(`Item ${args[1]} doesn't exist`, msg.messageId); return; };
if (!args[2]) { await sendMessage('Please specify the amount of the item you want to give', msg.messageId); return; };
const amount = Number(args[2]);
if (isNaN(amount) || amount < 0) { await sendMessage(`${args[2]} is not a valid amount`); return; };
const userRecord = await getUserRecord(user);
if (userRecord.inventory[item.name]! < amount) { await sendMessage(`You can't give items you don't have!`, msg.messageId); return; };
await user.setLock();
await target.setLock();
const data = await Promise.all([
await changeItemCount(target, targetRecord, item.name, amount),
await changeItemCount(user, userRecord, item.name, -amount)
]);
if (!data.includes(false)) {
const tempdata = data[0] as userRecord;
const newamount = tempdata.inventory[item.name]!;
await sendMessage(`${user.displayName} gave ${amount} ${item.prettyName + (amount === 1 ? '' : item.plural)} to ${target.displayName}. They now have ${newamount} ${item.prettyName + (newamount === 1 ? '' : item.plural)}`, msg.messageId);
} else {
await sendMessage(`Failed to give ${target.displayName} ${amount} ${item.prettyName + (amount === 1 ? '' : item.plural)}`, msg.messageId);
console.error(`WARNING: Item donation failed: target success: ${data[0] !== false}, donator success: ${data[0] !== false}`);
};
await user.clearLock();
await target.clearLock();
});

26
bot/commands/inventory.ts Normal file
View File

@@ -0,0 +1,26 @@
import { Command, sendMessage } from ".";
import { getUserRecord } from "../db/dbUser";
import parseCommandArgs from "../lib/parseCommandArgs";
import { User } from "../user";
import items from "../items";
export default new Command('inventory', ['inv', 'inventory'], [], async (msg, user) => {
const args = parseCommandArgs(msg.messageText);
let target: User = user;
if (args[0]) {
const obj = await User.initUsername(args[0].toLowerCase());
if (!obj) { await sendMessage(`User ${args[0]} doesn't exist`, msg.messageId); return; };
target = obj;
};
const data = await getUserRecord(target);
const messagedata: string[] = [];
for (const [key, amount] of Object.entries(data.inventory)) {
if (amount === 0) continue;
const itemselection = items.get(key);
messagedata.push(`${itemselection?.prettyName}${amount === 1 ? '' : itemselection?.plural}: ${amount}`);
};
if (messagedata.length === 0) { await sendMessage(`${target.displayName} has no items`, msg.messageId); return; };
await sendMessage(`Inventory of ${target.displayName}: ${messagedata.join(', ')}`, msg.messageId);
});

View File

@@ -1,5 +1,6 @@
import type { AccessToken } from "@twurple/auth"; import type { AccessToken } from "@twurple/auth";
import PocketBase, { RecordService } from "pocketbase"; import PocketBase, { RecordService } from "pocketbase";
import type { inventory } from "../items";
const pocketbaseurl = process.env.POCKETBASE_URL ?? "localhost:8090"; const pocketbaseurl = process.env.POCKETBASE_URL ?? "localhost:8090";
if (pocketbaseurl === "") { console.error("Please provide a POCKETBASE_URL in .env."); process.exit(1); }; if (pocketbaseurl === "") { console.error("Please provide a POCKETBASE_URL in .env."); process.exit(1); };
@@ -13,15 +14,15 @@ export type userRecord = {
id: string; id: string;
username: string; username: string;
balance: number; balance: number;
inventory: object; inventory: inventory;
lastlootbox: string; lastlootbox: string;
}; };
type TypedPocketBase = { interface TypedPocketBase extends PocketBase {
collection(idOrName: 'auth'): RecordService<authRecord>; collection(idOrName: 'auth'): RecordService<authRecord>;
collection(idOrName: 'users'): RecordService<userRecord>; collection(idOrName: 'users'): RecordService<userRecord>;
}; };
const pb = new PocketBase(pocketbaseurl) as TypedPocketBase; const pb = new PocketBase(pocketbaseurl) as TypedPocketBase;
export default pb.autoCancellation(false);
export default pb;

44
bot/db/dbUser.ts Normal file
View File

@@ -0,0 +1,44 @@
import pocketbase, { type userRecord } from "./connection";
import { emptyInventory, itemarray } from "../items";
import type { User } from "../user";
const pb = pocketbase.collection('users');
/** Use this function to both ensure existance and to retreive data */
export async function getUserRecord(user: User): Promise<userRecord> {
try {
const data = await pb.getOne<userRecord>(user.id);
if (Object.keys(data.inventory).sort().toString() !== itemarray.sort().toString()) { // If the items in the user inventory are missing an item.
itemarray.forEach(key => {
if (!(key in data.inventory)) Object.defineProperty(data.inventory, key, { value: 0 });
});
};
return data;
} catch (err) {
// This gets triggered if the user doesn't exist in the database
return await createUserRecord(user);
};
};
async function createUserRecord(user: User): Promise<userRecord> {
const data = await pb.create<userRecord>({
id: user.id,
username: user.username,
balance: 0,
inventory: emptyInventory,
lastlootbox: new Date(0).toISOString()
});
return data;
};
export async function updateUserRecord(user: User, newData: userRecord): Promise<boolean> {
try {
await pb.update(user.id, newData);
return true;
} catch (err) {
console.error(err);
return false;
};
};

View File

@@ -23,7 +23,7 @@ eventSub.onChannelChatMessage(streamerId, streamerId, async msg => {
if (msg.messageText.startsWith(commandPrefix)) { if (msg.messageText.startsWith(commandPrefix)) {
const commandSelection = msg.messageText.slice(commandPrefix.length).split(' ')[0]!; const commandSelection = msg.messageText.slice(commandPrefix.length).split(' ')[0]!;
const selected = commands.get(commandSelection.toLowerCase()); const selected = commands.get(commandSelection.toLowerCase());
if (!selected) { await sendMessage(`${commandSelection} command does not exist`, msg.messageId); return; }; if (!selected) return;
try { await selected.execute(msg, user!); } try { await selected.execute(msg, user!); }
catch (err) { console.error(err); }; catch (err) { console.error(err); };
}; };

View File

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

View File

@@ -1,22 +1,31 @@
import { redis } from "bun"; import { redis } from "bun";
import { sendMessage } from "../commands"; import { sendMessage } from "../commands";
import { timeout } from "../lib/timeout"; import { timeout } from "../lib/timeout";
import { Item } from "."; import { changeItemCount, Item } from ".";
import { User } from "../user"; 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', 'Give a random chatter a 60s timeout',
['grenade'], ['grenade'],
['moderator:manage:banned_users'], ['moderator:manage:banned_users'],
async (msg, user) => { 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:*'); const targets = await redis.keys('vulnchatters:*');
if (targets.length === 0) { await sendMessage('No vulnerable chatters to blow up', msg.messageId); return; }; 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 selection = targets[Math.floor(Math.random() * targets.length)]!;
const target = await User.initUserId(selection.split(':')[1]!); 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([ await Promise.all([
timeout(target!, `You got hit by ${user.displayName}'s grenade!`, 60), timeout(target!, `You got hit by ${user.displayName}'s grenade!`, 60),
redis.del(selection), 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 { readdir } from 'node:fs/promises';
import type { userRecord } from "../db/connection";
import { updateUserRecord } from "../db/dbUser";
const items = new Map<string, Item>; const items = new Map<string, Item>;
const itemintents: string[] = []; const itemintents: string[] = [];
const emptyInventory = {}; const emptyInventory: inventory = {};
const itemarray: string[] = []; const itemarray: string[] = [];
const files = await readdir(import.meta.dir); const files = await readdir(import.meta.dir);
@@ -39,7 +41,7 @@ for (const file of files) {
if (!file.endsWith('.ts')) continue; if (!file.endsWith('.ts')) continue;
if (file === import.meta.file) continue; if (file === import.meta.file) continue;
const item: Item = await import(import.meta.dir + '/' + file.slice(0, -3)).then(a => a.default); 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); itemarray.push(item.name);
itemintents.push(...item.requiredIntents); itemintents.push(...item.requiredIntents);
for (const alias of item.aliases) { for (const alias of item.aliases) {
@@ -49,3 +51,13 @@ for (const file of files) {
export default items; export default items;
export { itemintents, emptyInventory, itemarray }; 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;
};