diff --git a/src/bot.ts b/src/bot.ts index 31bc5c7..eabff25 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -12,4 +12,4 @@ const bot = new Bot({ bot.onConnect(async ()=> { console.log("Ready to accept commands!") await authProvider.refreshAccessTokenForUser(238377856) -}) +}) \ No newline at end of file diff --git a/src/commands/thank.ts b/src/commands/thank.ts index e7bed5e..983e96e 100644 --- a/src/commands/thank.ts +++ b/src/commands/thank.ts @@ -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(' ')}`) }) \ No newline at end of file diff --git a/src/commands/timeout.ts b/src/commands/timeout.ts index 9f7b726..5e1ee4a 100644 --- a/src/commands/timeout.ts +++ b/src/commands/timeout.ts @@ -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 }) => { - 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") - }) \ No newline at end of file +export default createBotCommand('timeout', async (params, { say, broadcasterId, userName }) => { + if (params.length === 0) {await say("nice miss bro"); return} + 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 + } + } +}) \ No newline at end of file diff --git a/src/lib/api.ts b/src/lib/api.ts new file mode 100644 index 0000000..25bec24 --- /dev/null +++ b/src/lib/api.ts @@ -0,0 +1,4 @@ +import authProvider from "../lib/auth"; +import { ApiClient } from "@twurple/api"; +const api = new ApiClient({ authProvider }) +export default api \ No newline at end of file diff --git a/src/lib/auth.ts b/src/lib/auth.ts index d77fae4..105d6ec 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -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") diff --git a/src/lib/itemHelpers/blaster.ts b/src/lib/itemHelpers/blaster.ts new file mode 100644 index 0000000..a3ec0f6 --- /dev/null +++ b/src/lib/itemHelpers/blaster.ts @@ -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) +} \ No newline at end of file diff --git a/src/lib/pocketbase.ts b/src/lib/pocketbase.ts new file mode 100644 index 0000000..bbb608b --- /dev/null +++ b/src/lib/pocketbase.ts @@ -0,0 +1,3 @@ +import PocketBase from 'pocketbase' +const pb = new PocketBase('http://pocketbase:8090') +export default pb \ No newline at end of file diff --git a/src/lib/timeoutHelper.ts b/src/lib/timeoutHelper.ts new file mode 100644 index 0000000..661105d --- /dev/null +++ b/src/lib/timeoutHelper.ts @@ -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 { + 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) +} \ No newline at end of file diff --git a/src/lib/userHelper.ts b/src/lib/userHelper.ts new file mode 100644 index 0000000..4e81125 --- /dev/null +++ b/src/lib/userHelper.ts @@ -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{ + 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) +} \ No newline at end of file