From 6075860b826c390d6fe7cb2e53f73af197ebd6c2 Mon Sep 17 00:00:00 2001 From: Matt Burchett Date: Thu, 23 Mar 2017 03:44:41 -0500 Subject: [PATCH] inital commit --- LICENSE | 21 +++++++ README.md | 14 +++++ bot.js | 131 ++++++++++++++++++++++++++++++++++++++++++++ config.json.example | 6 ++ package.json | 16 ++++++ 5 files changed, 188 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 bot.js create mode 100644 config.json.example create mode 100644 package.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..baee9cc --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Matt Burchett + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9909679 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# discord-stats-bot + +This is basically a personal project of mine in a effort to learn NodeJS and ELK. + +I wanted to create a bot for discord that would log it's usage to a centralised logging server. Essentially the bot is just logging usage to files, and logstash-forwarder is passing to ELK. + +You need the following modules to use this bot: + +- discordie +- log4js + +```bash +npm install discordie log4js +``` diff --git a/bot.js b/bot.js new file mode 100644 index 0000000..480a717 --- /dev/null +++ b/bot.js @@ -0,0 +1,131 @@ +// Require Discord Libraries +var Discordie = require('discordie'); + +// Require log4js for logging to files +var log4js = require('log4js'); + +// require custom settings +var config = require('./config.json'); + +// Configure log4js +log4js.configure({ + appenders: [ + { type: 'console' }, + { type: 'file', filename: 'logs/console.log', category: 'console'}, + { type: 'file', filename: 'logs/activeusers.log', category: 'activeusers'}, + { type: 'file', filename: 'logs/channels.log', category: 'channels'} + ] +}); + +// set constant log4js variables +const logcon = log4js.getLogger('console'); +const actcon = log4js.getLogger('activeusers'); +const chancon = log4js.getLogger('channels'); + +// begin discord bot +const Events = Discordie.Events; +const client = new Discordie(); + +// issue connect to discord using the bot_token in config.json +client.connect({ + token: config.bot_token +}); + +// once connected +client.Dispatcher.on(Events.GATEWAY_READY, e => { + // acknoledge connection to console logs + logcon.info('Connected as: ' + client.User.username); +}); + +// check for the number of active users every 30 seconds and log to the active users logs +setInterval(function() { + client.Users.fetchMembers(config.guild_id); + actcon.info(config.guild_name + " Active Users: " + client.Users.onlineMembersForGuild(config.guild_id).length);}, 30000); + + // when messages are created + client.Dispatcher.on(Events.MESSAGE_CREATE, e => { + // log the guild name, the channel name, the username, and the message to the channels log and disable DMs. + if (!e.message.isPrivate) + chancon.info(e.message.guild.name + ":" + " #" + e.message.channel.name + ": " + "<" + e.message.displayUsername + ">: "+ e.message.content); + + if (e.message.channel.name == "mod" || e.message.channel.name == "helpers" ) { + // begin chat commands + + // !ping + if (e.message.content == "!ping") + e.message.channel.sendMessage("pong"); + + // !bacon + if (e.message.content == "!bacon") + e.message.channel.sendMessage("*gives " + e.message.author.nickMention + " a strip of delicious bacon.* ") + + // !cookie + if (e.message.content == "!cookie") + e.message.channel.sendMessage("*gives " + e.message.author.nickMention + " a freshly made Oatmeal Raisin cookie.*") + + // !help + if (e.message.content == "!help") + e.message.channel.sendMessage("Sorry."); + + // end chat commands + // begin Baymax easter eggs + + // ow + if (e.message.content == "ow" || e.message.content == "Ow" || e.message.content == "OW" || e.message.content == "oW" || e.message.content == "Ow!" || e.message.content == "Ow.") + e.message.channel.sendMessage("Hello. I am Baymax, your personal Discord companion. \r\r\ + https://cdn.discordapp.com/attachments/265064665099403264/294326730703896577/giphy-2.gif"); + + // heart attack + if (e.message.content.indexOf('heart attack') >=0) + e.message.channel.sendMessage("My hands are equipped with defibrillators. **CLEAR!** \r\r\ + https://cdn.discordapp.com/attachments/265064665099403264/294302721429995520/tumblr_n9h0l4ODbC1ry7whco1_1280.gif"); + + // Cry emoji... There's actually a emoji there, I swear. + if (e.message.content == "😢") + e.message.channel.sendMessage("There, there. \r\r\ + https://cdn.discordapp.com/attachments/265064665099403264/294303907704864770/giphy-4.gif"); + + // fist bump + if (e.message.content == "*fist bump*" || e.message.content == "*fistbump*") + e.message.channel.sendMessage("Ba-la-la-la-la! \r\r\ + https://cdn.discordapp.com/attachments/265064665099403264/294327036388835328/giphy-3.gif") + + // !lollipop + if (e.message.content == "I'm satisfied with my care.") + e.message.channel.sendMessage("You have been good, have a lollipop! \r\r\ + https://cdn.discordapp.com/attachments/265064665099403264/294333704749449216/Baemax-baymax-lollipop.gif") + + // end Baymax easter eggs + // begin misc chat triggers + + // shrug + if (e.message.content == "¯\\_(ツ)_/¯") + e.message.channel.sendMessage("*mic drop*"); + + // end misc chat triggers + } + }); + + // if connection is lost to Discord, issue a reconnect. + + client.Dispatcher.on(Events.DISCONNECTED, e => { + // force disconnection to Discord + client.disconnect(); + logcon.info('Disconnected from server ...'); + + // reconnect to Discord + logcon.info('Reconnecting to Discord ... '); + client.connect({ + token: config.bot_token + }); + }); + + // also, reconnect the bot hourly. + + setInterval(function() { + logcon.info('Reconnecting to server per timeout of ' + config.reconnect + 'ms.'); + client.disconnect(); + client.connect({ + token: config.bot_token + }); + }, config.reconnect); diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..ebad3b3 --- /dev/null +++ b/config.json.example @@ -0,0 +1,6 @@ +{ + "bot_token": "", + "guild_id": "", + "guild_name": "", + "reconnect": "" +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b3044fd --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "discord-stats-bot", + "version": "1.0.0", + "description": "Discord Stats Bot (for exporting into ELK or other centralized logging)", + "main": "bot.js", + "scripts": { + "bot": "nodemon bot.js" + }, + "dependencies": { + "discordie": ">=0.0.0", + "log4js": ">=0.0.0" + }, + "author": "Matt Burchett", + "license": "MIT" +} +