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"] diff --git a/README.md b/README.md index 9901834..d02b11f 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,37 @@ 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 with a tag + Example: `docker build -t rollux .` + +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' + +services: + rollux: + image: 'rollux' + volumes: + - ./data:/data +``` + +For environment variable: +``` +version: '2' + +services: + rollux: + image: 'rollux' + environment: + - DISCORD_TOKEN={your token} +``` diff --git a/bot.js b/bot.js index b6b96ab..a771cec 100644 --- a/bot.js +++ b/bot.js @@ -1,7 +1,8 @@ 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'); + logger.remove(logger.transports.Console); logger.add(new logger.transports.Console, { @@ -9,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 @@ -26,17 +39,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 +61,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 - }) + }); }