implement !getloot, sanitize userselection, allow self-blasting

This commit is contained in:
2025-04-01 23:29:54 +02:00
parent b456c2e37e
commit f797a143e5
9 changed files with 73 additions and 12 deletions

33
src/commands/getloot.ts Normal file
View File

@@ -0,0 +1,33 @@
import { createBotCommand } from "@twurple/easy-bot";
import { lootboxReady, resetLootboxTimer } from "../lib/lootboxes";
import { changeItemCount } from "../lib/items"
import api from "../lib/api"
function getTimeDifference(date1: number , date2: number) {
const diff = Math.abs(date1 - date2);
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
return { days, hours, minutes, seconds };
}
export default createBotCommand('getloot', async (_params, { reply, userId/*, broadcasterId*/ }) => {
const user = await api.users.getUserById(userId)
// if (!user?.isSubscribedTo(broadcasterId)) {await reply('Subscribe to receive loot mandoooSmile'); return}
const data = await lootboxReady(user)
if (!data.result) {
const { days, hours, minutes, seconds } = getTimeDifference(data.lastlootbox, Date.now() - 1000 * 60 * 60 * 24 * 30)
await reply(`lootbox ready in:
${days === 0 ? '' : `${days} day${days === 1 ? '' : 's'}, `}
${hours === 0 ? '' : `${hours} hour${hours === 1 ? '' : 's'}, `}
${minutes === 0 ? '' : `${minutes} minute${minutes === 1 ? '' : 's'}, `}
${seconds === 0 ? '' : `${seconds} second${seconds === 1 ? '' : 's'}`}
`)
return
}
await reply(`You got a lootbox mandoooSmile`)
await changeItemCount(user!, 'lootbox', 1)
await resetLootboxTimer(data.DBuser)
})

View File

@@ -4,5 +4,6 @@ import give from "./give"
import inventory from "./inventory"; import inventory from "./inventory";
import stats from "./stats"; import stats from "./stats";
import mbucks from "./mbucks"; import mbucks from "./mbucks";
import getloot from "./getloot";
export default [timeout, thank, give, inventory, stats, mbucks] export default [timeout, thank, give, inventory, stats, mbucks, getloot]

View File

@@ -6,7 +6,7 @@ import { HelixUser } from "@twurple/api";
export default createBotCommand('inv', async (params, { userName, say }) => { export default createBotCommand('inv', async (params, { userName, say }) => {
let user: HelixUser | null let user: HelixUser | null
if (params.length !== 0) { if (params.length !== 0) {
user = await api.users.getUserByName(params[0]) user = await api.users.getUserByName(params[0].replace(/[^a-zA-Z0-9]/g, ''))
} else user = await api.users.getUserByName(userName) } else user = await api.users.getUserByName(userName)
if (!user) { if (!user) {
await say(`User ${params[0]} not found`) await say(`User ${params[0]} not found`)

View File

@@ -6,7 +6,7 @@ import { getBalance } from "../lib/userHelper";
export default createBotCommand('mbucks', async (params, { userName, say }) => { export default createBotCommand('mbucks', async (params, { userName, say }) => {
let user: HelixUser | null let user: HelixUser | null
if (params.length !== 0) { if (params.length !== 0) {
user = await api.users.getUserByName(params[0]) user = await api.users.getUserByName(params[0].replace(/[^a-zA-Z0-9]/g, ''))
} else user = await api.users.getUserByName(userName) } else user = await api.users.getUserByName(userName)
if (!user) { if (!user) {
await say(`User ${params[0]} not found`) await say(`User ${params[0]} not found`)

View File

@@ -6,7 +6,7 @@ import { HelixUser } from "@twurple/api";
export default createBotCommand('stats', async (params, { say, userName }) => { export default createBotCommand('stats', async (params, { say, userName }) => {
let user: HelixUser | null let user: HelixUser | null
if (params.length !== 0) { if (params.length !== 0) {
user = await api.users.getUserByName(params[0]) user = await api.users.getUserByName(params[0].replace(/[^a-zA-Z0-9]/g, ''))
} else user = await api.users.getUserByName(userName) } else user = await api.users.getUserByName(userName)
if (!user) { if (!user) {
await say(`User ${params[0]} not found`) await say(`User ${params[0]} not found`)

View File

@@ -1,14 +1,18 @@
import { createBotCommand } from "@twurple/easy-bot"; import { createBotCommand } from "@twurple/easy-bot";
import { addTimeoutToDB, timeout } from "../lib/timeoutHelper"; import { addTimeoutToDB, timeout } from "../lib/timeoutHelper";
import { changeBalance, getBalance } from "../lib/userHelper";
import api from "../lib/api"; import api from "../lib/api";
export default createBotCommand('timeout', async (params, { say, broadcasterId, userName }) => { export default createBotCommand('timeout', async (params, { say, broadcasterId, userName }) => {
const attacker = await api.users.getUserByName(userName)
const userbal = await getBalance(attacker!)
if (userbal.balance < 100) { await say('not enough mandoobucks'); return }
if (params.length === 0) { await say("nice miss bro"); return } if (params.length === 0) { await say("nice miss bro"); return }
const target = await api.users.getUserByName(params[0]) const target = await api.users.getUserByName(params[0].replace(/[^a-zA-Z0-9]/g, ''))
const status = await timeout(broadcasterId, target!, 60, `You got blasted by ${userName}`) const status = await timeout(broadcasterId, target!, 60, `You got blasted by ${userName}`)
if (status.status) { if (status.status) {
await say(`${params[0]} got mandoooGun by ${userName}! mandoooGOTTEM`) await say(`${params[0]} got mandoooGun by ${userName}! mandoooGOTTEM ${userName} now has ${userbal.balance - 100} mandoobucks remaining`)
const attacker = await api.users.getUserByName(userName) await changeBalance(attacker!, -100)
await addTimeoutToDB(attacker!, target!, 'blaster') await addTimeoutToDB(attacker!, target!, 'blaster')
} }
else { else {
@@ -20,7 +24,8 @@ export default createBotCommand('timeout', async (params, { say, broadcasterId,
await say(`${params[0]} is already dead!`) await say(`${params[0]} is already dead!`)
break break
case 'unknown': case 'unknown':
await say(`what the fuck just happened?? mandoooYikes`) await say(`NO!`)
await timeout(broadcasterId, attacker!, 60, "NO!")
break break
} }
} }

21
src/lib/lootboxes.ts Normal file
View File

@@ -0,0 +1,21 @@
import { HelixUser } from "@twurple/api"
import pb from "./pocketbase"
const COOLDOWN = 1000 * 60 * 60 * 24 * 30 // 1000 milliseconds * 60 seconds * 60 minutes * 24 hours * 30 days
interface lootboxReadyResult {
result: boolean,
lastlootbox: number,
DBuser: any // TODO: proper types for db user (again). check RecordModel
}
export async function lootboxReady(user: HelixUser | null): Promise<lootboxReadyResult> {
const DBuser = await pb.collection('users').getFirstListItem(`twitchid="${user!.id}"`)
if ((Date.parse(DBuser.lastlootbox) + COOLDOWN) > Date.now()) return { result: false, lastlootbox: Date.parse(DBuser.lastlootbox), DBuser }
return { result: true, lastlootbox: 0, DBuser }
}
export async function resetLootboxTimer(user: any) {
const data = { lastlootbox: new Date(Date.now()).toISOString() }
await pb.collection('users').update(user.id, data)
}

View File

@@ -25,13 +25,13 @@ export async function timeout(broadcasterid: string, target: HelixUser, duration
export async function addTimeoutToDB(attacker: HelixUser, target: HelixUser, source: shooter) { export async function addTimeoutToDB(attacker: HelixUser, target: HelixUser, source: shooter) {
// This has passed the existance check so there's no need to check if the users exist (twitch) // This has passed the existance check so there's no need to check if the users exist (twitch)
const attackerDB = getDBID(attacker) const attackerDB = await getDBID(attacker)
const targetDB = getDBID(target) const targetDB = await getDBID(target)
const timeoutobj = { const timeoutobj = {
source, source,
attacker: await attackerDB, attacker: attackerDB,
target: await targetDB, target: targetDB,
attackername: attacker.name, attackername: attacker.name,
targetname: target.name targetname: target.name
} }

View File

@@ -138,6 +138,7 @@ async function createUser(user: HelixUser) {
firstname: user.name, firstname: user.name,
inventory: EMPTYINV, inventory: EMPTYINV,
itemuses: EMPTYINV, itemuses: EMPTYINV,
lastlootbox: "1970-01-01 12:00:00.000Z",
balance: 0 balance: 0
} }
await pb.collection('users').create(data) await pb.collection('users').create(data)