Merge pull request #1 from Northcode/master

Add Docker support!
This commit is contained in:
AadneEM 2019-05-23 18:44:44 +02:00 committed by GitHub
commit b66314e8c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 9 deletions

13
Dockerfile Normal file
View File

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

View File

@ -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` 3. Install node.js and npm, then run the following command: `npm install`
4. Run it with `node bot.js` 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}
```

29
bot.js
View File

@ -1,7 +1,8 @@
var Discord = require('discord.io'); var Discord = require('discord.io');
var logger = require('winston'); var logger = require('winston');
var auth = require('./auth.json'); var fs = require('fs');
var diceRoller = require('./diceRolling.js') var diceRoller = require('./diceRolling.js');
logger.remove(logger.transports.Console); logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, { logger.add(new logger.transports.Console, {
@ -9,6 +10,18 @@ logger.add(new logger.transports.Console, {
}); });
logger.level = 'debug'; 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({ var bot = new Discord.Client({
token: auth.token, token: auth.token,
autorun: true autorun: true
@ -26,17 +39,17 @@ bot.on('message', function (user, userID, channelID, message, evt) {
var args = message.substring(1).split(' '); var args = message.substring(1).split(' ');
var cmd = args[0]; var cmd = args[0];
console.log(message) console.log(message);
switch (cmd) { switch (cmd) {
case 'hi': case 'hi':
sendMessage(channelID, 'Hello, World!') sendMessage(channelID, 'Hello, World!');
case 'roll': case 'roll':
if (/([+\-]?\d{0,}d\d{1,})([+\-*x/]\d{1,}){0,}/gi.test(message.substring(6).replace(/\s/g, ''))) { if (/([+\-]?\d{0,}d\d{1,})([+\-*x/]\d{1,}){0,}/gi.test(message.substring(6).replace(/\s/g, ''))) {
sendMessage( sendMessage(
channelID, channelID,
message.substring(6) + ': ' + diceRoller.roll(message.substring(6)) message.substring(6) + ': ' + diceRoller.roll(message.substring(6))
) );
} else { } else {
sendMessage(channelID, "I don't recognize this: \"" sendMessage(channelID, "I don't recognize this: \""
+ message + message
@ -48,17 +61,17 @@ bot.on('message', function (user, userID, channelID, message, evt) {
break; break;
} }
} catch (error) { } catch (error) {
console.log(error) console.log(error);
} }
} }
}); });
function sendMessage(client, message) { function sendMessage(client, message) {
if (message.length > 2500) { if (message.length > 2500) {
message = 'Response was to long. Sorry' message = 'Response was to long. Sorry';
} }
bot.sendMessage({ bot.sendMessage({
to: client, to: client,
message: message message: message
}) });
} }