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())));
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;

View File

@ -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);

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 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));
}

View File

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