mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-19 00:51:37 +01:00
added !mbucks command, minor fixes
This commit is contained in:
@@ -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 }
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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(', ')}
|
||||
|
||||
19
src/commands/mbucks.ts
Normal file
19
src/commands/mbucks.ts
Normal file
@@ -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'] })
|
||||
@@ -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<balanceGetResult> {
|
||||
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<balanceChangeResult> {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user