From b456c2e37e61df87e4be8462b694aac77ad11b45 Mon Sep 17 00:00:00 2001 From: qwerinope Date: Tue, 1 Apr 2025 12:27:17 +0200 Subject: [PATCH] added !mbucks command, minor fixes --- src/commands/give.ts | 3 ++- src/commands/index.ts | 3 ++- src/commands/inventory.ts | 2 ++ src/commands/mbucks.ts | 19 +++++++++++++++++++ src/lib/userHelper.ts | 20 +++++++++----------- 5 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 src/commands/mbucks.ts diff --git a/src/commands/give.ts b/src/commands/give.ts index a479ed6..5cf8c89 100644 --- a/src/commands/give.ts +++ b/src/commands/give.ts @@ -1,6 +1,7 @@ import { createBotCommand } from "@twurple/easy-bot"; import api from "../lib/api"; import { changeItemCount } from "../lib/items"; +import { changeBalance } from "../lib/userHelper"; export default createBotCommand('give', async (params, { say, broadcasterId, userId }) => { if (userId !== broadcasterId) return @@ -10,7 +11,7 @@ export default createBotCommand('give', async (params, { say, broadcasterId, use if (isNaN(parseInt(params[2]))) { await say(`Specify the amount`); return } - const data = await changeItemCount(target, params[1].toLowerCase(), parseInt(params[2])) + const data = params[1].toLowerCase() === 'mbucks' ? await changeBalance(target, parseInt(params[2])) : await changeItemCount(target, params[1].toLowerCase(), parseInt(params[2])) if (data.reason === 'negative') { await say(`${target.name} only has ${data.count}. Cannot yoink ${-parseInt(params[2])} ${params[1]}`); return } else if (data.reason === 'noexist') { await say(`Can't find item ${params[1]}`); return } diff --git a/src/commands/index.ts b/src/commands/index.ts index 790c27b..ca1e2f0 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -3,5 +3,6 @@ import thank from "./thank" import give from "./give" import inventory from "./inventory"; import stats from "./stats"; +import mbucks from "./mbucks"; -export default [timeout, thank, give, inventory, stats] +export default [timeout, thank, give, inventory, stats, mbucks] diff --git a/src/commands/inventory.ts b/src/commands/inventory.ts index 85b47eb..9a94f0a 100644 --- a/src/commands/inventory.ts +++ b/src/commands/inventory.ts @@ -43,6 +43,8 @@ export default createBotCommand('inv', async (params, { userName, say }) => { messagedata.push(`${item.name + (item.amount === 1 ? '' : item.plural)}: ${item.amount}`) } + if (messagedata.length === 0) {await say(`${data.me ? userName : params[0]} has no items mandoooYikes`); return} + await say(` inventory of ${data.me ? userName : params[0]}: ${messagedata.join(', ')} diff --git a/src/commands/mbucks.ts b/src/commands/mbucks.ts new file mode 100644 index 0000000..0ac8920 --- /dev/null +++ b/src/commands/mbucks.ts @@ -0,0 +1,19 @@ +import { createBotCommand } from "@twurple/easy-bot"; +import { HelixUser } from "@twurple/api"; +import api from "../lib/api"; +import { getBalance } from "../lib/userHelper"; + +export default createBotCommand('mbucks', async (params, { userName, say }) => { + let user: HelixUser | null + if (params.length !== 0) { + user = await api.users.getUserByName(params[0]) + } else user = await api.users.getUserByName(userName) + if (!user) { + await say(`User ${params[0]} not found`) + return + } + + const data = await getBalance(user) + await say(`${user.name} has ${data.balance} mbucks ${data.balance === 0 ? 'mandoooYikes' : 'mandoooSmile'}`) + +}, { aliases: ['mbux', 'mandoobucks'] }) diff --git a/src/lib/userHelper.ts b/src/lib/userHelper.ts index fb1668e..3e13204 100644 --- a/src/lib/userHelper.ts +++ b/src/lib/userHelper.ts @@ -27,29 +27,27 @@ export async function getDBID(user: HelixUser) { type balanceGetResult = { balance: number, - user: HelixUser + data: any // TODO: propet type for data returned from database } export async function getBalance(user: HelixUser): Promise { await DBValidation(user) const data = await pb.collection('users').getFirstListItem(`twitchid="${user!.id}"`) - return { balance: data.balance, user } + return { balance: data.balance, data } } type balanceChangeResult = { result: boolean, - userBalance: balanceGetResult + reason: string, + count: number } export async function changeBalance(user: HelixUser, amount: number): Promise { - let userBalance = await getBalance(user) - if (amount < 0 && userBalance.balance - amount < 0) return { result: false, userBalance } - const dbuser = await pb.collection('users').getFirstListItem(`twitchid="${userBalance.user.id}"`) - let data = dbuser - data.balance += amount - userBalance.balance += amount - await pb.collection('users').update(dbuser.id, data) - return { result: true, userBalance } + let { balance, data } = await getBalance(user) + if (amount < 0 && balance - amount < 0) return { result: false, reason: 'negative', count: balance } + data.balance = balance + amount + await pb.collection('users').update(data.id, data) + return { result: true, reason: '', count: data.balance } } interface timeoutsGetResult {