diff --git a/config/config.js b/config/config.js index dfc9d0d..923a0b2 100644 --- a/config/config.js +++ b/config/config.js @@ -9,6 +9,7 @@ var path = require("path"); var baseConfig = require(path.join(__dirname, path.join("environments/" + env.toLowerCase()))); baseConfig.steamBot = {}; +baseConfig.discordBot = {}; if (!test) { if (process.env.PORT) { @@ -30,6 +31,15 @@ if (!test) { if (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; diff --git a/index.js b/index.js index 3145574..ad81fc1 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,11 @@ require(path.join(__dirname, "db/index")); // 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 require(path.join(__dirname, "config/express"))(app); diff --git a/lib/discord/bot.js b/lib/discord/bot.js new file mode 100644 index 0000000..d65efe1 --- /dev/null +++ b/lib/discord/bot.js @@ -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; +}; \ No newline at end of file diff --git a/lib/gather/gather.js b/lib/gather/gather.js index 01a1d83..485296b 100644 --- a/lib/gather/gather.js +++ b/lib/gather/gather.js @@ -13,6 +13,7 @@ const Gatherer = require("./gatherer"); const StateMachine = require("javascript-state-machine"); +const discordBot = require("../discord/bot")(); function Gather (options) { if (options === undefined) options = {}; @@ -86,7 +87,16 @@ StateMachine.create({ if (this.needsToCoolOff(user)) return false; if (this.failsTest(user)) return false; 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 @@ -96,6 +106,7 @@ StateMachine.create({ }, onenterelection: function () { + discordBot.notifyChannel("Gather is starting! Pick your captains! https://gathers.ensl.org"); // Setup timer for elections this.startElectionCountdown(); }, @@ -158,6 +169,7 @@ StateMachine.create({ self.modifyGatherer(user, (gatherer) => gatherer.voteRegather(vote)); if (self.regatherVotes() >= self.REGATHER_THRESHOLD) { self.resetState(); + discordBot.notifyChannel("@here Gather got reseted! Rejoin to play"); return true; } else { return false; @@ -166,6 +178,7 @@ StateMachine.create({ // On enter done onenterdone: function () { + discordBot.notifyChannel("Picking finished! Join the server!"); this.done.time = new Date(); this.onDone.apply(this, [].slice.call(arguments)); } diff --git a/package.json b/package.json index 0750c05..fd6f96e 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "cookie-parser": "~1.3.5", "cors": "~2.7.1", "css-brunch": ">= 1.0 < 1.8", + "discord.js": "^11.1.0", "express": "~4.13.1", "express-handlebars": "~2.0.1", "extend": "~3.0.0",