major refactor, docker support. Check if there's a better way to do commands/index.ts

This commit is contained in:
2025-03-29 16:58:07 +01:00
parent a3b3f353f0
commit 447ed8dff0
9 changed files with 98 additions and 52 deletions

23
Dockerfile Normal file
View File

@@ -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", "." ]

View File

@@ -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

View File

@@ -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!")
})

View File

@@ -1,4 +1,5 @@
{
"main": "src/bot.ts",
"dependencies": {
"@twurple/api": "^7.2.1",
"@twurple/chat": "^7.2.1",

15
src/bot.ts Normal file
View File

@@ -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)
})

4
src/commands/index.ts Normal file
View File

@@ -0,0 +1,4 @@
import timeout from "./timeout";
import thank from "./thank"
export default [timeout, thank]

6
src/commands/thank.ts Normal file
View File

@@ -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(' ')}`)
})

13
src/commands/timeout.ts Normal file
View File

@@ -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")
})

26
src/lib/auth.ts Normal file
View File

@@ -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