From a810e2bc3d3ca698ea947fb229147f1734897198 Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Sat, 18 May 2019 15:09:05 +0200 Subject: [PATCH 1/7] Fix auth.json loading from working directory Added missing semicolons from linter warnings --- bot.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/bot.js b/bot.js index b6b96ab..19f5b2a 100644 --- a/bot.js +++ b/bot.js @@ -1,7 +1,9 @@ var Discord = require('discord.io'); var logger = require('winston'); -var auth = require('./auth.json'); -var diceRoller = require('./diceRolling.js') +var fs = require('fs'); +var diceRoller = require('./diceRolling.js'); + +var auth = JSON.parse(fs.readFileSync('./auth.json', 'utf8')); logger.remove(logger.transports.Console); logger.add(new logger.transports.Console, { @@ -26,17 +28,17 @@ bot.on('message', function (user, userID, channelID, message, evt) { var args = message.substring(1).split(' '); var cmd = args[0]; - console.log(message) + console.log(message); switch (cmd) { case 'hi': - sendMessage(channelID, 'Hello, World!') + sendMessage(channelID, 'Hello, World!'); case 'roll': if (/([+\-]?\d{0,}d\d{1,})([+\-*x/]\d{1,}){0,}/gi.test(message.substring(6).replace(/\s/g, ''))) { sendMessage( channelID, message.substring(6) + ': ' + diceRoller.roll(message.substring(6)) - ) + ); } else { sendMessage(channelID, "I don't recognize this: \"" + message @@ -48,17 +50,17 @@ bot.on('message', function (user, userID, channelID, message, evt) { break; } } catch (error) { - console.log(error) + console.log(error); } } }); function sendMessage(client, message) { if (message.length > 2500) { - message = 'Response was to long. Sorry' + message = 'Response was to long. Sorry'; } bot.sendMessage({ to: client, message: message - }) + }); } From cfb52959890762c2e252e55641985dbbd04ab33d Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Sat, 18 May 2019 15:11:06 +0200 Subject: [PATCH 2/7] Add Dockerfile --- Dockerfile | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1d98cab --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM node:latest + +RUN mkdir /app/ + +ADD . /app/. + +RUN cd /app/ && npm install + +RUN mkdir /data + +WORKDIR /data/ + +CMD ["node", "/app/bot.js"] From cecbbaff49d03f29e6cd852a65dd47e0860b8d00 Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Sat, 18 May 2019 15:14:43 +0200 Subject: [PATCH 3/7] Updated README.md with docker info --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9901834..c3e35a4 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,23 @@ Rollux is a simple dice rolling bot for discord } ``` 3. Install node.js and npm, then run the following command: `npm install` -4. Run it with `node bot.js` \ No newline at end of file +4. Run it with `node bot.js` + +# Running with docker + +1. Build docker image + +2. Mount `/data` directory in container with your auth.json file + + +## Example docker-compose file: + +``` +version: '2' + +services: + rollux: + image: 'rollux' + volumes: + - ./data:/data +``` From 5123edb7a8ee201e4127f035a2595bcaf29848f0 Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Sat, 18 May 2019 15:16:50 +0200 Subject: [PATCH 4/7] Add docker build example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c3e35a4..c1219db 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,11 @@ Rollux is a simple dice rolling bot for discord # Running with docker -1. Build docker image +1. Build docker image with a tag + Example: `docker build -t rollux .` 2. Mount `/data` directory in container with your auth.json file - ## Example docker-compose file: ``` From bf3c4f229952b415496964276d421ab330b809d1 Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Thu, 23 May 2019 18:32:55 +0200 Subject: [PATCH 5/7] Add loading of auth from environment variable --- bot.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bot.js b/bot.js index 19f5b2a..a771cec 100644 --- a/bot.js +++ b/bot.js @@ -3,7 +3,6 @@ var logger = require('winston'); var fs = require('fs'); var diceRoller = require('./diceRolling.js'); -var auth = JSON.parse(fs.readFileSync('./auth.json', 'utf8')); logger.remove(logger.transports.Console); logger.add(new logger.transports.Console, { @@ -11,6 +10,18 @@ logger.add(new logger.transports.Console, { }); logger.level = 'debug'; +const get_auth = () => { + if (process.env.DISCORD_TOKEN != undefined) { + logger.info('using auth from environment variable'); + return { token: process.env.DISCORD_TOKEN }; + } else { + logger.info('using auth from auth.json file'); + return JSON.parse(fs.readFileSync('./auth.json', 'utf8')); + } +}; + +var auth = get_auth(); + var bot = new Discord.Client({ token: auth.token, autorun: true From ce336bcbe9f94c029a08ec700d1965b4919b0d7c Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Thu, 23 May 2019 18:36:33 +0200 Subject: [PATCH 6/7] Update README --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c1219db..8fef596 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,13 @@ Rollux is a simple dice rolling bot for discord 1. Build docker image with a tag Example: `docker build -t rollux .` -2. Mount `/data` directory in container with your auth.json file +2. Add authentication by either: + 1. Mount `/data` directory in container with your auth.json file + 2. Pass the environment variable `DISCORD_TOKEN` with your discord token ## Example docker-compose file: +For auth.json method: ``` version: '2' @@ -34,3 +37,14 @@ services: volumes: - ./data:/data ``` + +For environment variable: +``` +version: '2' + +services: + rollux: + image: 'rollux' + environment: + - DISCORD_TOKEN={your token} +``` From 05fc12e55c91e86ce3193afe08a28b59e158e0bd Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Thu, 23 May 2019 18:37:34 +0200 Subject: [PATCH 7/7] fix README indentation --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8fef596..d02b11f 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,6 @@ version: '2' services: rollux: image: 'rollux' - environment: - - DISCORD_TOKEN={your token} + environment: + - DISCORD_TOKEN={your token} ```