From 447ed8dff0b76ce60bd13693b0c0c2f9a6aa99a3 Mon Sep 17 00:00:00 2001 From: qwerinope Date: Sat, 29 Mar 2025 16:58:07 +0100 Subject: [PATCH] major refactor, docker support. Check if there's a better way to do commands/index.ts --- Dockerfile | 23 ++++++++++++++++++ compose.yml | 10 ++++++++ index.ts | 52 ----------------------------------------- package.json | 1 + src/bot.ts | 15 ++++++++++++ src/commands/index.ts | 4 ++++ src/commands/thank.ts | 6 +++++ src/commands/timeout.ts | 13 +++++++++++ src/lib/auth.ts | 26 +++++++++++++++++++++ 9 files changed, 98 insertions(+), 52 deletions(-) create mode 100644 Dockerfile delete mode 100644 index.ts create mode 100644 src/bot.ts create mode 100644 src/commands/index.ts create mode 100644 src/commands/thank.ts create mode 100644 src/commands/timeout.ts create mode 100644 src/lib/auth.ts diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3290cc2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM oven/bun AS base +WORKDIR /app + +FROM base AS install +RUN mkdir -p /temp/dev +COPY package.json bun.lock /temp/dev/ +RUN cd /temp/dev && bun install --frozen-lockfile + +RUN mkdir -p /temp/prod +COPY package.json bun.lock /temp/prod/ +RUN cd /temp/prod && bun install --frozen-lockfile --production + +FROM base AS prerelease +COPY --from=install /temp/dev/node_modules node_modules +COPY . . + +FROM base AS release +COPY --from=install /temp/prod/node_modules node_modules +COPY --from=prerelease /app/src ./src +COPY --from=prerelease /app/auth.json . +COPY --from=prerelease /app/package.json . + +CMD [ "bun", "." ] \ No newline at end of file diff --git a/compose.yml b/compose.yml index d92269a..fefc31d 100644 --- a/compose.yml +++ b/compose.yml @@ -9,3 +9,13 @@ services: volumes: - ./pb:/pb/pb_data restart: no + bot: + container_name: bot + build: + context: . + dockerfile: Dockerfile + restart: no + develop: + watch: + - action: rebuild + path: ./src diff --git a/index.ts b/index.ts deleted file mode 100644 index ccbf52a..0000000 --- a/index.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Bot, createBotCommand } from '@twurple/easy-bot' -import { ApiClient } from '@twurple/api' -import { RefreshingAuthProvider } from '@twurple/auth' - -let auth = await Bun.file('auth.json').json() - -const authProvider = new RefreshingAuthProvider({ - clientId: auth.CLIENT_ID, - clientSecret: auth.CLIENT_SECRET -}) - -await authProvider.addUserForToken({ - accessToken: auth.ACCESS_TOKEN, - refreshToken: auth.REFRESH_TOKEN, - expiresIn: auth.EXPIRESIN, - obtainmentTimestamp: auth.OBTAINMENTTIMESTAMP -}, ['chat', 'moderator:manage:banned_users']) - -authProvider.onRefresh(async (_id, newTokenData) => { - auth.ACCESS_TOKEN = newTokenData.accessToken - auth.REFRESH_TOKEN = newTokenData.refreshToken! - auth.EXPIRESIN = newTokenData.expiresIn! - auth.OBTAINMENTTIMESTAMP = newTokenData.obtainmentTimestamp - await Bun.file('auth.json').write(JSON.stringify(auth)) - console.log("Refreshed OAuth tokens!") -}) - -await authProvider.refreshAccessTokenForUser(238377856) - -const api = new ApiClient({ authProvider }) - -const bot = new Bot({ - authProvider, - channel: "qwerinope", - commands: [ - 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") - }), - createBotCommand("thank", async (params, {say, msg}) => { - if (params.length === 0) {await say(`fuck you ${msg.userInfo.userName}`); return} - await say(`fuck you ${params.join(' ')}`) - }) - ] -}) - -bot.onConnect(()=> { - console.log("Ready!") -}) diff --git a/package.json b/package.json index fd6c54d..429a365 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { + "main": "src/bot.ts", "dependencies": { "@twurple/api": "^7.2.1", "@twurple/chat": "^7.2.1", diff --git a/src/bot.ts b/src/bot.ts new file mode 100644 index 0000000..31bc5c7 --- /dev/null +++ b/src/bot.ts @@ -0,0 +1,15 @@ +import { Bot } from '@twurple/easy-bot' + +import authProvider from './lib/auth'; +import commands from './commands' + +const bot = new Bot({ + authProvider, + channel: "qwerinope", + commands +}) + +bot.onConnect(async ()=> { + console.log("Ready to accept commands!") + await authProvider.refreshAccessTokenForUser(238377856) +}) diff --git a/src/commands/index.ts b/src/commands/index.ts new file mode 100644 index 0000000..dbfd51f --- /dev/null +++ b/src/commands/index.ts @@ -0,0 +1,4 @@ +import timeout from "./timeout"; +import thank from "./thank" + +export default [timeout, thank] \ No newline at end of file diff --git a/src/commands/thank.ts b/src/commands/thank.ts new file mode 100644 index 0000000..e7bed5e --- /dev/null +++ b/src/commands/thank.ts @@ -0,0 +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(' ')}`) +}) \ No newline at end of file diff --git a/src/commands/timeout.ts b/src/commands/timeout.ts new file mode 100644 index 0000000..9f7b726 --- /dev/null +++ b/src/commands/timeout.ts @@ -0,0 +1,13 @@ +import { createBotCommand } from "@twurple/easy-bot"; + +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 diff --git a/src/lib/auth.ts b/src/lib/auth.ts new file mode 100644 index 0000000..9bd62f7 --- /dev/null +++ b/src/lib/auth.ts @@ -0,0 +1,26 @@ +import { RefreshingAuthProvider } from '@twurple/auth' + +let auth = await Bun.file('auth.json').json() + +const authProvider = new RefreshingAuthProvider({ + clientId: auth.CLIENT_ID, + clientSecret: auth.CLIENT_SECRET +}) + +await authProvider.addUserForToken({ + accessToken: auth.ACCESS_TOKEN, + refreshToken: auth.REFRESH_TOKEN, + expiresIn: auth.EXPIRESIN, + obtainmentTimestamp: auth.OBTAINMENTTIMESTAMP +}, ['chat', 'moderator:manage:banned_users']) + +authProvider.onRefresh(async (_id, newTokenData) => { + auth.ACCESS_TOKEN = newTokenData.accessToken + auth.REFRESH_TOKEN = newTokenData.refreshToken! + auth.EXPIRESIN = newTokenData.expiresIn! + auth.OBTAINMENTTIMESTAMP = newTokenData.obtainmentTimestamp + await Bun.file('auth.json').write(JSON.stringify(auth)) + console.log("Refreshed OAuth tokens.") +}) + +export default authProvider \ No newline at end of file