timeouts are saved in DB, basic inventory system, better timeout, abstract pocketbase connection

This commit is contained in:
2025-03-30 00:44:07 +01:00
parent 35880a354d
commit c47197b516
9 changed files with 162 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
import { createBotCommand } from "@twurple/easy-bot";
export default createBotCommand("thank", async (params, {say, msg}) => {
if (params.length === 0) {await say(`fuck you ${msg.userInfo.userName}`); return}
await say(`fuck you ${params.join(' ')}`)
if (params.length === 0) {await say(`chumpi4Heart ${msg.userInfo.userName}`); return}
await say(`chumpi4Heart ${params.join(' ')}`)
})

View File

@@ -1,13 +1,27 @@
import { createBotCommand } from "@twurple/easy-bot";
import { addTimeoutToDB, timeout } from "../lib/timeoutHelper";
import api from "../lib/api";
import authProvider from "../lib/auth";
import { ApiClient } from "@twurple/api";
const api = new ApiClient({ authProvider })
export default createBotCommand('timeout', async (params, { say, broadcasterId }) => {
export default createBotCommand('timeout', async (params, { say, broadcasterId, userName }) => {
if (params.length === 0) {await say("nice miss bro"); return}
const user = await api.users.getUserByName(params[0])
if (!user) { await say("bro doesn't exist"); return }
await api.moderation.banUser(broadcasterId, { duration: 60, reason: "lmao", user: user.id })
await say("mandoooGOTTEM")
const target = await api.users.getUserByName(params[0])
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 addTimeoutToDB(attacker! ,target! , 'blaster')
}
else {
switch (status.reason){
case 'noexist':
await say(`${params[0]} doesn't exist!`)
break
case 'banned':
await say(`${params[0]} is already dead!`)
break
case 'unknown':
await say(`what the fuck just happened?? mandoooYikes`)
break
}
}
})

4
src/lib/api.ts Normal file
View File

@@ -0,0 +1,4 @@
import authProvider from "../lib/auth";
import { ApiClient } from "@twurple/api";
const api = new ApiClient({ authProvider })
export default api

View File

@@ -1,7 +1,5 @@
import { RefreshingAuthProvider, exchangeCode } from '@twurple/auth'
import PocketBase from 'pocketbase'
const pb = new PocketBase('http://pocketbase:8090')
import pb from './pocketbase'
const ttvauth = await pb.collection('ttvauth').getFullList()
@@ -17,7 +15,7 @@ async function firstAccess() {
if (!CLIENT_ID) {console.error("No 'CLIENT_ID' for OAuth defined in environment variables."); process.exit(1)}
if (!CLIENT_SECRET) {console.error("No 'CLIENT_SECRET' for OAuth defined in environment variables."); process.exit(1)}
if (!OAUTH_CODE) {console.error("No 'OAUTH_CODE' provided. To get the code, please visit this URL, authorize the bot and copy the 'code' from the return URL.")
console.error(`https://id.twitch.tv/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=http://localhost&response_type=code&scope=chat:read+chat:edit+moderator:manage:banned_users`)
console.error(`https://id.twitch.tv/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=http://localhost&response_type=code&scope=chat:read+chat:edit+moderator:manage:banned_users+moderation:read`)
process.exit(1)
}
const tokens = await exchangeCode(CLIENT_ID, CLIENT_SECRET, OAUTH_CODE, "http://localhost")

View File

@@ -0,0 +1,13 @@
import { getInventory, updateInventory } from "../userHelper";
export async function giveBlaster(username:string, amount:number) {
let inv = await getInventory(username)
inv.blaster += amount
await updateInventory(username, inv)
}
export async function loseBlaster(username: string, amount=1) {
let inv = await getInventory(username)
inv.blaster -= amount
await updateInventory(username, inv)
}

3
src/lib/pocketbase.ts Normal file
View File

@@ -0,0 +1,3 @@
import PocketBase from 'pocketbase'
const pb = new PocketBase('http://pocketbase:8090')
export default pb

39
src/lib/timeoutHelper.ts Normal file
View File

@@ -0,0 +1,39 @@
import { HelixUser } from "@twurple/api";
import api from "./api";
import pb from "./pocketbase";
import { getDBID } from "./userHelper";
type shooter = 'blaster'|'grenade'|'silverbullet'|'watergun'|'tnt'
interface statusmessage {
status: boolean,
reason?: string
}
export async function timeout(broadcasterid:string, target:HelixUser, duration:number, reason:string): Promise<statusmessage> {
if (!target) return {status:false, reason: 'noexist'}
if (await api.moderation.checkUserBan(broadcasterid, target)) return {status: false, reason: 'banned'}
try {
await api.moderation.banUser(broadcasterid, {duration, reason, user: target})
return {status: true}
} catch (err) {
console.error(err)
return {status: false, reason: 'unknown'}
}
}
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 timeoutobj = {
source,
attacker: await attackerDB,
target: await targetDB,
attackername: attacker.name,
targetname: target.name
}
await pb.collection('timeouts').create(timeoutobj)
}

73
src/lib/userHelper.ts Normal file
View File

@@ -0,0 +1,73 @@
import pb from './pocketbase'
import api from './api'
import { HelixUser } from '@twurple/api'
interface inventory {
version: number
blaster: number
grenade: number
silverbullet: number
tnt: number
watergun: number
clipboard:number
lootbox:number
}
const EMPTYINV = {
version: 1,
blaster: 0,
grenade: 0,
silverbullet: 0,
tnt: 0,
watergun: 0,
clipboard: 0,
lootbox: 0
}
export async function getDBID(user:HelixUser) {
try {
const DBuser = await pb.collection('users').getFirstListItem(`twitchid="${user!.id}"`)
return DBuser.id
} catch (error) {
await createUser(user!)
const DBuser = await pb.collection('users').getFirstListItem(`twitchid="${user!.id}"`)
return DBuser.id
}
}
export async function getInventory(username:string): Promise<inventory>{
const user = await existanceValidation(username)
const data = await pb.collection('users').getFirstListItem(`twitchid="${user!.id}"`)
return data.inventory
}
export async function updateInventory(username:string, newinv:inventory) {
const user = await existanceValidation(username)
const data = await pb.collection('users').getFirstListItem(`twitchid="${user!.id}"`)
const recordid = data.id
await pb.collection('users').update(recordid, {inventory:newinv})
}
async function existanceValidation(username:string) {
const user = await api.users.getUserByName(username)
try {
await pb.collection('users').getFirstListItem(`twitchid="${user!.id}"`)
} catch (error) {
await createUser(user!)
}
return user
}
async function createUser(user:HelixUser) {
const data = {
twitchid: user?.id,
firstname: user?.name,
inventory: EMPTYINV,
balance: 0
}
await pb.collection('users').create(data)
}