From ac3f81857f17201fed997c647fa9345b25bcb852 Mon Sep 17 00:00:00 2001 From: qwerinope Date: Mon, 30 Jun 2025 17:59:27 +0200 Subject: [PATCH] add balance,donate,admindonate commands and minor bugfixes --- README.md | 8 ++++ bot/commands/admindonate.ts | 25 ++++++++++ bot/commands/donateqbucks.ts | 46 +++++++++++++++++++ bot/commands/getbalance.ts | 12 +++++ .../{inventory.ts => getinventory.ts} | 0 bot/commands/{give.ts => giveitem.ts} | 10 ++-- bot/lib/changeBalance.ts | 10 ++++ 7 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 bot/commands/admindonate.ts create mode 100644 bot/commands/donateqbucks.ts create mode 100644 bot/commands/getbalance.ts rename bot/commands/{inventory.ts => getinventory.ts} (100%) rename bot/commands/{give.ts => giveitem.ts} (94%) create mode 100644 bot/lib/changeBalance.ts diff --git a/README.md b/README.md index a708d72..963b2d0 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,14 @@ COMMAND|FUNCTION|USER|ALIASES|DISABLEABLE `yabai`|Random number|anyone|`yabai` `goon`|:white_check_mark: `seiso`|Random number|anyone|`seiso`|:white_check_mark: +### Qweribucks commands + +COMMAND|FUNCTION|USER|ALIASES|DISABLEABLE +-|-|-|-|- +`getbalance [target]`|Get balance of target or self|anyone|`getbalance` `balance` `qbucks` `qweribucks` `wallet` `getwallet`|:white_check_mark: +`donate {target} {amount}`|Give the targeted user some or all of your qweribucks|anyone|`donate`|:white_check_mark: +`admindonate {target} {amount}`|Gives the targeted user amount of qweribucks|admins|`admindonate`|:white_check_mark: + ### Item commands COMMAND|FUNCTION|USER|ALIASES|DISABLEABLE diff --git a/bot/commands/admindonate.ts b/bot/commands/admindonate.ts new file mode 100644 index 0000000..c575f48 --- /dev/null +++ b/bot/commands/admindonate.ts @@ -0,0 +1,25 @@ +import { Command, sendMessage } from "."; +import { getUserRecord } from "../db/dbUser"; +import { changeBalance } from "../lib/changeBalance"; +import parseCommandArgs from "../lib/parseCommandArgs"; +import { User } from "../user"; + +export default new Command('admindonate', ['admindonate'], 'admin', async msg => { + 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 the amount qweribucks you want to give', msg.messageId); return; }; + const amount = Number(args[1]); + if (isNaN(amount)) { await sendMessage(`${args[1]} is not a valid amount`); return; }; + if (await target.itemLock()) { await sendMessage('Cannot give qweribucks: item transaction in progress', msg.messageId); return; }; + await target.setLock(); + const data = await changeBalance(target, userRecord, amount); + if (!data) { + await sendMessage(`Failed to give ${target.displayName} ${amount} qweribuck${amount === 1 ? '' : 's'}`, msg.messageId); + } else { + await sendMessage(`${target.displayName} now has ${data.balance} qweribuck${data.balance === 1 ? '' : 's'}`, msg.messageId); + }; + await target.clearLock(); +}); diff --git a/bot/commands/donateqbucks.ts b/bot/commands/donateqbucks.ts new file mode 100644 index 0000000..a0c30ed --- /dev/null +++ b/bot/commands/donateqbucks.ts @@ -0,0 +1,46 @@ +import { Command, sendMessage } from "."; +import type { userRecord } from "../db/connection"; +import { getUserRecord } from "../db/dbUser"; +import parseCommandArgs from "../lib/parseCommandArgs"; +import { changeBalance } from "../lib/changeBalance"; +import { User } from "../user"; + +export default new Command('donate', ['donate'], 'chatter', 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 the amount of the item you want to give', msg.messageId); return; }; + const amount = Number(args[1]); + if (isNaN(amount) || amount < 0) { await sendMessage(`${args[1]} is not a valid amount`); return; }; + + const userRecord = await getUserRecord(user); + if (userRecord.balance < amount) { await sendMessage(`You can't give qweribucks you don't have!`, msg.messageId); return; }; + + if (await user.itemLock() || await target.itemLock()) { await sendMessage('Cannot give qweribucks. Please try again!', msg.messageId); return; }; + + await Promise.all([ + user.setLock(), + target.setLock() + ]); + + const data = await Promise.all([ + await changeBalance(target, targetRecord, amount), + await changeBalance(user, userRecord, -amount) + ]); + + if (!data.includes(false)) { + const { balance: newamount } = data[0] as userRecord; + await sendMessage(`${user.displayName} gave ${amount} qweribuck${amount === 1 ? '' : 's'} to ${target.displayName}. They now have ${newamount} qweribuck${newamount === 1 ? '' : 's'}`, msg.messageId); + } else { + // TODO: Rewrite this section + await sendMessage(`Failed to give ${target.displayName} ${amount} qbuck${(amount === 1 ? '' : 's')}`, msg.messageId); + console.error(`WARNING: Qweribucks donation failed: target success: ${data[0] !== false}, donator success: ${data[1] !== false}`); + }; + + await Promise.all([ + user.clearLock(), + target.clearLock() + ]); +}); diff --git a/bot/commands/getbalance.ts b/bot/commands/getbalance.ts new file mode 100644 index 0000000..af29ce3 --- /dev/null +++ b/bot/commands/getbalance.ts @@ -0,0 +1,12 @@ +import { Command, sendMessage } from "."; +import { getUserRecord } from "../db/dbUser"; +import parseCommandArgs from "../lib/parseCommandArgs"; +import { User } from "../user"; + +export default new Command('getbalance', ['getbalance', 'balance', 'qbucks', 'qweribucks', 'wallet', 'getwallet'], 'chatter', async (msg, user) => { + const args = parseCommandArgs(msg.messageText); + const target = args[0] ? await User.initUsername(args[0].toLowerCase()) : user; + if (!target) { await sendMessage(`Chatter ${args[0]} doesn't exist!`, msg.messageId); return; }; + const data = await getUserRecord(target); + await sendMessage(`${target.displayName} has ${data.balance} qbuck${data.balance === 1 ? '' : 's'}`, msg.messageId); +}); diff --git a/bot/commands/inventory.ts b/bot/commands/getinventory.ts similarity index 100% rename from bot/commands/inventory.ts rename to bot/commands/getinventory.ts diff --git a/bot/commands/give.ts b/bot/commands/giveitem.ts similarity index 94% rename from bot/commands/give.ts rename to bot/commands/giveitem.ts index 3050471..3923edc 100644 --- a/bot/commands/give.ts +++ b/bot/commands/giveitem.ts @@ -22,8 +22,12 @@ export default new Command('give', ['give'], 'chatter', async (msg, user) => { if (userRecord.inventory[item.name]! < amount) { await sendMessage(`You can't give items you don't have!`, msg.messageId); return; }; if (await user.itemLock() || await target.itemLock()) { await sendMessage('Cannot give item. Please try again!', msg.messageId); return; }; - await user.setLock(); - await target.setLock(); + + await Promise.all([ + user.setLock(), + target.setLock() + ]); + const data = await Promise.all([ await changeItemCount(target, targetRecord, item.name, amount), await changeItemCount(user, userRecord, item.name, -amount) @@ -36,7 +40,7 @@ export default new Command('give', ['give'], 'chatter', async (msg, user) => { } else { // TODO: Rewrite this section 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}`); + console.error(`WARNING: Item donation failed: target success: ${data[0] !== false}, donator success: ${data[1] !== false}`); }; await user.clearLock(); await target.clearLock(); diff --git a/bot/lib/changeBalance.ts b/bot/lib/changeBalance.ts new file mode 100644 index 0000000..e6ba2d0 --- /dev/null +++ b/bot/lib/changeBalance.ts @@ -0,0 +1,10 @@ +import { updateUserRecord } from "../db/dbUser"; +import { type userRecord } from "../db/connection"; +import { User } from "../user"; + +export async function changeBalance(user: User, userRecord: userRecord, amount: number): Promise { + userRecord.balance = userRecord.balance += amount; + if (userRecord.balance < 0) return false; + await updateUserRecord(user, userRecord); + return userRecord; +};