mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-19 00:51:37 +01:00
implement !getloot, sanitize userselection, allow self-blasting
This commit is contained in:
33
src/commands/getloot.ts
Normal file
33
src/commands/getloot.ts
Normal 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)
|
||||
})
|
||||
@@ -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]
|
||||
|
||||
@@ -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`)
|
||||
|
||||
@@ -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`)
|
||||
|
||||
@@ -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`)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
21
src/lib/lootboxes.ts
Normal file
21
src/lib/lootboxes.ts
Normal 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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user