From e978d9c6fda79797d704204c6e789cb107fa471b Mon Sep 17 00:00:00 2001 From: James Moore Date: Tue, 21 Nov 2023 00:58:15 -0500 Subject: [PATCH] move to Dockerfile setup --- .dockerignore | 1 + .github/workflows/publish.yml | 20 ++++++++++++++++++++ Dockerfile | 9 +++++++++ src/index.ts | 9 +++++++-- src/utils/commands.ts | 4 +++- 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/publish.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..fa7b01f --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,20 @@ +name: Publish +on: + workflow_dispatch: + branches: + - master +jobs: + publish: + name: Publish + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: mr-smithers-excellent/docker-build-push@v6 + with: + image: Team-Resourceful/discord-bot-ts + registry: ghcr.io + githubOrg: Team-Resourceful # optional + username: ${{ secrets.GHCR_USERNAME }} + password: ${{ secrets.GHCR_TOKEN }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a876a06 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +# syntax=docker/dockerfile:1 + +FROM node:18.16-alpine +ENV NODE_ENV=production +WORKDIR /app +COPY ["package.json", "package-lock.json*", "./"] +RUN npm install --production +COPY . . +CMD ["npm", "run", "no-build-start"] \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 95e285a..49ea026 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,8 @@ -import { token } from "../configs/config.json"; +//import { token } from "../configs/config.json"; import {Client, GatewayIntentBits} from "discord.js"; import { Interactions } from "./utils/interactions"; import {Commands} from "./utils/commands"; +import * as process from "process"; const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages] }); const interactions = new Interactions(client); @@ -10,7 +11,11 @@ const commands = new Commands(); const getCommands = () => commands; const getInteractions = () => interactions; -client.login(token); +if (!process.env.DISCORD_TOKEN) { + throw new Error("DISCORD_TOKEN environment variable missing.") +} + +client.login(process.env.DISCORD_TOKEN); export { getInteractions } export { getCommands } diff --git a/src/utils/commands.ts b/src/utils/commands.ts index f099f25..184a41c 100644 --- a/src/utils/commands.ts +++ b/src/utils/commands.ts @@ -1,6 +1,7 @@ import { Collection } from "discord.js"; import { Database } from "./database"; import { databaseOptions } from "../../configs/config.json"; +import * as process from "process"; export class Commands { @@ -8,11 +9,12 @@ export class Commands { private commands: Collection>; constructor() { - this.database = new Database(databaseOptions); + this.database = new Database(`{host: ${process.env.DB_HOST}, database: ${process.env.DB_NAME}, password: ${process.env.DB_PASSWORD}, port: ${process.env.DB_PORT}, user: ${process.env.DB_USER}}`); this.commands = new Collection>(Object.values(CommandType).map(type => [type, new Collection()])); this.database.connect() .then(() => { + console.log("connected to database!") for (let type of Object.values(CommandType)) { this.database.query("SELECT `category`, `key`, `value` FROM `commands` WHERE `category` = ?", [type]) .then(async results => {