added discord bot

This commit is contained in:
Absurdon 2017-06-22 15:47:29 +02:00
parent 5519eb6cf1
commit edbbddfe0c
5 changed files with 55 additions and 1 deletions

View file

@ -9,6 +9,7 @@ var path = require("path");
var baseConfig = require(path.join(__dirname, path.join("environments/" + env.toLowerCase()))); var baseConfig = require(path.join(__dirname, path.join("environments/" + env.toLowerCase())));
baseConfig.steamBot = {}; baseConfig.steamBot = {};
baseConfig.discordBot = {};
if (!test) { if (!test) {
if (process.env.PORT) { if (process.env.PORT) {
@ -30,6 +31,15 @@ if (!test) {
if (process.env.GATHER_STEAM_PASSWORD) { if (process.env.GATHER_STEAM_PASSWORD) {
baseConfig.steamBot.password = process.env.GATHER_STEAM_PASSWORD; baseConfig.steamBot.password = process.env.GATHER_STEAM_PASSWORD;
} }
if (process.env.GATHER_DISCORD_HOOK_ID) {
baseConfig.discordBot.hook_id = process.env.GATHER_DISCORD_HOOK_ID;
}
if (process.env.GATHER_DISCORD_HOOK_TOKEN) {
baseConfig.discordBot.hook_token = process.env.GATHER_DISCORD_HOOK_TOKEN;
}
} }
module.exports = baseConfig; module.exports = baseConfig;

View file

@ -19,6 +19,11 @@ require(path.join(__dirname, "db/index"));
// require(path.join(__dirname, "lib/steam/bot"))(config.steamBot); // require(path.join(__dirname, "lib/steam/bot"))(config.steamBot);
//} //}
//Initialise Discord Bot
if (env === "production") {
require(path.join(__dirname, "lib/discord/bot"))(config.discordBot);
}
// Configure express // Configure express
require(path.join(__dirname, "config/express"))(app); require(path.join(__dirname, "config/express"))(app);

25
lib/discord/bot.js Normal file
View file

@ -0,0 +1,25 @@
"use strict"
// Import the discord.js module
const Discord = require('discord.js');
function DiscordBot(config) {
this.hook = new Discord.WebhookClient(config.hook_id,config.hook_token);
this.spamProtection = {
fillStatus: null,
};
}
DiscordBot.prototype.notifyChannel = function(message) {
this.hook.send(message);
};
var bot;
module.exports = (config) => {
if (bot) return bot;
if (!config) throw new Error("No credentials provided for Discord Gather Bot");
bot = new DiscordBot(config);
return bot;
};

View file

@ -13,6 +13,7 @@
const Gatherer = require("./gatherer"); const Gatherer = require("./gatherer");
const StateMachine = require("javascript-state-machine"); const StateMachine = require("javascript-state-machine");
const discordBot = require("../discord/bot")();
function Gather (options) { function Gather (options) {
if (options === undefined) options = {}; if (options === undefined) options = {};
@ -86,7 +87,16 @@ StateMachine.create({
if (this.needsToCoolOff(user)) return false; if (this.needsToCoolOff(user)) return false;
if (this.failsTest(user)) return false; if (this.failsTest(user)) return false;
this.addUser(user); this.addUser(user);
if (!this.lobbyFull()) return false; if (!this.lobbyFull()) {
if(this.gatherers.length > this.teamSize &&
(null === discordBot.spamProtection.fillStatus ||
((new Date()).getTime() - discordBot.spamProtection.fillStatus.getTime())/1000 > 180)) {
discordBot.notifyChannel("Join the gather at https://gathers.ensl.org | " + this.gatherers.length + " players are already waiting!");
discordBot.spamProtection.fillStatus = new Date();
}
return false;
}
}, },
// Election State // Election State
@ -96,6 +106,7 @@ StateMachine.create({
}, },
onenterelection: function () { onenterelection: function () {
discordBot.notifyChannel("Gather is starting! Pick your captains! https://gathers.ensl.org");
// Setup timer for elections // Setup timer for elections
this.startElectionCountdown(); this.startElectionCountdown();
}, },
@ -158,6 +169,7 @@ StateMachine.create({
self.modifyGatherer(user, (gatherer) => gatherer.voteRegather(vote)); self.modifyGatherer(user, (gatherer) => gatherer.voteRegather(vote));
if (self.regatherVotes() >= self.REGATHER_THRESHOLD) { if (self.regatherVotes() >= self.REGATHER_THRESHOLD) {
self.resetState(); self.resetState();
discordBot.notifyChannel("@here Gather got reseted! Rejoin to play");
return true; return true;
} else { } else {
return false; return false;
@ -166,6 +178,7 @@ StateMachine.create({
// On enter done // On enter done
onenterdone: function () { onenterdone: function () {
discordBot.notifyChannel("Picking finished! Join the server!");
this.done.time = new Date(); this.done.time = new Date();
this.onDone.apply(this, [].slice.call(arguments)); this.onDone.apply(this, [].slice.call(arguments));
} }

View file

@ -40,6 +40,7 @@
"cookie-parser": "~1.3.5", "cookie-parser": "~1.3.5",
"cors": "~2.7.1", "cors": "~2.7.1",
"css-brunch": ">= 1.0 < 1.8", "css-brunch": ">= 1.0 < 1.8",
"discord.js": "^11.1.0",
"express": "~4.13.1", "express": "~4.13.1",
"express-handlebars": "~2.0.1", "express-handlebars": "~2.0.1",
"extend": "~3.0.0", "extend": "~3.0.0",