diff --git a/src/commands/getloot.ts b/src/commands/getloot.ts new file mode 100644 index 0000000..80be143 --- /dev/null +++ b/src/commands/getloot.ts @@ -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) +}) diff --git a/src/commands/index.ts b/src/commands/index.ts index ca1e2f0..103b1b5 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -4,5 +4,6 @@ import give from "./give" import inventory from "./inventory"; import stats from "./stats"; 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] diff --git a/src/commands/inventory.ts b/src/commands/inventory.ts index 9a94f0a..ea2b464 100644 --- a/src/commands/inventory.ts +++ b/src/commands/inventory.ts @@ -6,7 +6,7 @@ import { HelixUser } from "@twurple/api"; export default createBotCommand('inv', async (params, { userName, say }) => { let user: HelixUser | null 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) if (!user) { await say(`User ${params[0]} not found`) diff --git a/src/commands/mbucks.ts b/src/commands/mbucks.ts index 0ac8920..48574e4 100644 --- a/src/commands/mbucks.ts +++ b/src/commands/mbucks.ts @@ -6,7 +6,7 @@ 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]) + user = await api.users.getUserByName(params[0].replace(/[^a-zA-Z0-9]/g, '')) } else user = await api.users.getUserByName(userName) if (!user) { await say(`User ${params[0]} not found`) diff --git a/src/commands/stats.ts b/src/commands/stats.ts index f596d6c..dd512e7 100644 --- a/src/commands/stats.ts +++ b/src/commands/stats.ts @@ -6,7 +6,7 @@ import { HelixUser } from "@twurple/api"; export default createBotCommand('stats', async (params, { say, userName }) => { let user: HelixUser | null 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) if (!user) { await say(`User ${params[0]} not found`) diff --git a/src/commands/timeout.ts b/src/commands/timeout.ts index 78c292a..5895403 100644 --- a/src/commands/timeout.ts +++ b/src/commands/timeout.ts @@ -1,14 +1,18 @@ import { createBotCommand } from "@twurple/easy-bot"; import { addTimeoutToDB, timeout } from "../lib/timeoutHelper"; +import { changeBalance, getBalance } from "../lib/userHelper"; import api from "../lib/api"; 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 } - 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}`) if (status.status) { - await say(`${params[0]} got mandoooGun by ${userName}! mandoooGOTTEM`) - const attacker = await api.users.getUserByName(userName) + await say(`${params[0]} got mandoooGun by ${userName}! mandoooGOTTEM ${userName} now has ${userbal.balance - 100} mandoobucks remaining`) + await changeBalance(attacker!, -100) await addTimeoutToDB(attacker!, target!, 'blaster') } else { @@ -20,7 +24,8 @@ export default createBotCommand('timeout', async (params, { say, broadcasterId, await say(`${params[0]} is already dead!`) break case 'unknown': - await say(`what the fuck just happened?? mandoooYikes`) + await say(`NO!`) + await timeout(broadcasterId, attacker!, 60, "NO!") break } } diff --git a/src/lib/lootboxes.ts b/src/lib/lootboxes.ts new file mode 100644 index 0000000..97bbca8 --- /dev/null +++ b/src/lib/lootboxes.ts @@ -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 { + 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) +} diff --git a/src/lib/timeoutHelper.ts b/src/lib/timeoutHelper.ts index 43714f6..cfec5dd 100644 --- a/src/lib/timeoutHelper.ts +++ b/src/lib/timeoutHelper.ts @@ -25,13 +25,13 @@ export async function timeout(broadcasterid: string, target: HelixUser, duration 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) - const attackerDB = getDBID(attacker) - const targetDB = getDBID(target) + const attackerDB = await getDBID(attacker) + const targetDB = await getDBID(target) const timeoutobj = { source, - attacker: await attackerDB, - target: await targetDB, + attacker: attackerDB, + target: targetDB, attackername: attacker.name, targetname: target.name } diff --git a/src/lib/userHelper.ts b/src/lib/userHelper.ts index 3e13204..29c15d2 100644 --- a/src/lib/userHelper.ts +++ b/src/lib/userHelper.ts @@ -138,6 +138,7 @@ async function createUser(user: HelixUser) { firstname: user.name, inventory: EMPTYINV, itemuses: EMPTYINV, + lastlootbox: "1970-01-01 12:00:00.000Z", balance: 0 } await pb.collection('users').create(data)