From 8e576e2a3f1f0aba778bc156f00ca2c14bb9675d Mon Sep 17 00:00:00 2001 From: chris062689 Date: Wed, 4 Oct 2017 22:21:36 -0400 Subject: [PATCH] Updated folder structure. Added back responses.json into src. Tweaked includes. --- Dockerfile | 2 +- commands/ban.js | 25 ------------ commands/warn.js | 26 ------------- commands/warnings.js | 9 ----- commands/whoami.js | 6 --- data.js | 41 -------------------- src/commands/ban.js | 24 ++++++++++++ {commands => src/commands}/clearWarnings.js | 16 ++++---- {commands => src/commands}/grantDeveloper.js | 5 +-- {commands => src/commands}/quote.js | 10 ++--- src/commands/warn.js | 24 ++++++++++++ src/commands/warnings.js | 8 ++++ src/data.js | 41 ++++++++++++++++++++ logging.js => src/logging.js | 0 {models => src/models}/UserBan.js | 2 +- {models => src/models}/UserWarning.js | 2 +- src/responses.json | 22 +++++++++++ server.js => src/server.js | 33 ++++++++-------- app.js => src/state.js | 4 +- {triggers => src/triggers}/github.js | 22 +++++------ 20 files changed, 163 insertions(+), 159 deletions(-) delete mode 100644 commands/ban.js delete mode 100644 commands/warn.js delete mode 100644 commands/warnings.js delete mode 100644 commands/whoami.js delete mode 100644 data.js create mode 100644 src/commands/ban.js rename {commands => src/commands}/clearWarnings.js (51%) rename {commands => src/commands}/grantDeveloper.js (82%) rename {commands => src/commands}/quote.js (69%) create mode 100644 src/commands/warn.js create mode 100644 src/commands/warnings.js create mode 100644 src/data.js rename logging.js => src/logging.js (100%) rename {models => src/models}/UserBan.js (76%) rename {models => src/models}/UserWarning.js (77%) create mode 100644 src/responses.json rename server.js => src/server.js (81%) rename app.js => src/state.js (72%) rename {triggers => src/triggers}/github.js (62%) diff --git a/Dockerfile b/Dockerfile index 9c860e7..9a3fa84 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,4 @@ RUN addgroup -S app -g 50000 && \ adduser -S -g app -u 50000 app USER app -ENTRYPOINT [ "node", "server.js" ] +ENTRYPOINT [ "node", "src/server.js" ] diff --git a/commands/ban.js b/commands/ban.js deleted file mode 100644 index f56d59d..0000000 --- a/commands/ban.js +++ /dev/null @@ -1,25 +0,0 @@ -var discord = require('discord.js'); -var app = require('../app.js'); -var data = require('../data.js'); -var logger = require('../logging.js'); -var UserBan = require('../models/UserBan.js'); - -exports.roles = ['Admins', 'Moderators', 'CitraBot']; -exports.command = function(message) { - message.mentions.users.map((user) => { - var count = app.warnings.filter(x => x.id == user.id && !x.cleared).length || 0; - - message.channel.sendMessage(`${user} ${user.username}, You will now be banned from this channel.`); - logger.info(`${message.author.toString()} has banned ${user.toString()} ${user} ${user.username}.`); - app.logChannel.sendMessage(`${message.author} has banned ${user} ${user.username} [${count}].`); - - app.bans.push(new UserBan(user.id, user.username, message.author.id, message.author.username, count)); - - message.guild.member(user).ban().catch(function (error) { - app.logChannel.sendMessage(`Error banning ${user} ${user.username}`); - logger.error(`Error banning ${user.toString()} ${user} ${user.username}.`, error); - }); - - data.flushBans(); - }); -} diff --git a/commands/warn.js b/commands/warn.js deleted file mode 100644 index f8f951b..0000000 --- a/commands/warn.js +++ /dev/null @@ -1,26 +0,0 @@ -var discord = require('discord.js'); -var app = require('../app.js'); -var data = require('../data.js'); -var logger = require('../logging.js'); -var UserWarning = require('../models/UserWarning.js'); - -exports.roles = ['Admins', 'Moderators']; -exports.command = function(message) { - message.mentions.users.map((user) => { - var count = app.warnings.filter(x => x.id == user.id && !x.cleared).length || 0; - message.channel.sendMessage(`${user} You have been warned. Additional infractions may result in a ban.`); - - logger.info(`${message.author.username} ${message.author} has warned ${user.username} ${user} [${count} + 1].`); - app.logChannel.sendMessage(`${message.author} has warned ${user} [${count} + 1].`); - - app.warnings.push(new UserWarning(user.id, user.username, message.author.id, message.author.username, count)); - data.flushWarnings(); - - app.stats.warnings += 1; - - if (count + 1 >= 3) { - app.logChannel.sendMessage(`.ban ${user}`); - } - - }); -} diff --git a/commands/warnings.js b/commands/warnings.js deleted file mode 100644 index 4a4e3df..0000000 --- a/commands/warnings.js +++ /dev/null @@ -1,9 +0,0 @@ -var app = require('../app.js'); -var logger = require('../logging.js'); - -exports.command = function(message) { - message.mentions.users.map((user) => { - var warnings = app.warnings.filter(x => x.id == user.id && !x.cleared); - message.channel.sendMessage(`${user}, you have ${warnings.length} total warnings.`); - }); -} diff --git a/commands/whoami.js b/commands/whoami.js deleted file mode 100644 index f7317d4..0000000 --- a/commands/whoami.js +++ /dev/null @@ -1,6 +0,0 @@ -var app = require('../app.js'); - -exports.roles = ['Admins', 'Moderators']; -exports.command = function (message, reply) { - app.logChannel.sendMessage(`I am running under ${process.env.USER}.`); -}; diff --git a/data.js b/data.js deleted file mode 100644 index b108a6e..0000000 --- a/data.js +++ /dev/null @@ -1,41 +0,0 @@ -var fs = require('fs'); -var app = require('./app.js'); -var logger = require('./logging.js'); - -function readWarnings () { - // Load the warnings file into the bans variable. - fs.readFile('./data/discordWarnings.json', 'utf8', function (err, data) { - if (err && err.code === 'ENOENT') { return; } - if (err) { logger.error(err); } - app.warnings = JSON.parse(data); - logger.debug('Loaded warnings file.'); - }); -} - -function readBans () { - // Load the ban file into the bans variable. - fs.readFile('./data/discordBans.json', 'utf8', function (err, data) { - if (err && err.code === 'ENOENT') { return; } - if (err) { logger.error(err); } - app.bans = JSON.parse(data); - logger.debug('Loaded bans file.'); - }); -} - -function flushWarnings () { - var warningsJson = JSON.stringify(app.warnings, null, 4); - if (!fs.existsSync('./data/')) fs.mkdirSync('./data/'); - fs.writeFile('./data/discordWarnings.json', warningsJson, 'utf8', function (err) { - if (err) { logger.error(err); } - }); -} - -function flushBans () { - var bansJson = JSON.stringify(app.bans, null, 4); - if (!fs.existsSync('./data/')) fs.mkdirSync('./data/'); - fs.writeFile('./data/discordBans.json', bansJson, 'utf8', function (err) { - if (err) { logger.error(err); } - }); -} - -module.exports = { readWarnings: readWarnings, readBans: readBans, flushWarnings: flushWarnings, flushBans: flushBans }; diff --git a/src/commands/ban.js b/src/commands/ban.js new file mode 100644 index 0000000..21d682c --- /dev/null +++ b/src/commands/ban.js @@ -0,0 +1,24 @@ +const state = require('../state.js'); +const data = require('../data.js'); +const logger = require('../logging.js'); +const UserBan = require('../models/UserBan.js'); + +exports.roles = ['Admins', 'Moderators', 'CitraBot']; +exports.command = function (message) { + message.mentions.users.map((user) => { + var count = state.warnings.filter(x => x.id === user.id && !x.cleared).length || 0; + + message.channel.sendMessage(`${user} ${user.username}, You will now be banned from this channel.`); + logger.info(`${message.author.toString()} has banned ${user.toString()} ${user} ${user.username}.`); + state.logChannel.sendMessage(`${message.author} has banned ${user} ${user.username} [${count}].`); + + state.bans.push(new UserBan(user.id, user.username, message.author.id, message.author.username, count)); + + message.guild.member(user).ban().catch(function (error) { + state.logChannel.sendMessage(`Error banning ${user} ${user.username}`); + logger.error(`Error banning ${user.toString()} ${user} ${user.username}.`, error); + }); + + data.flushBans(); + }); +}; diff --git a/commands/clearWarnings.js b/src/commands/clearWarnings.js similarity index 51% rename from commands/clearWarnings.js rename to src/commands/clearWarnings.js index 95f72c3..143eb62 100644 --- a/commands/clearWarnings.js +++ b/src/commands/clearWarnings.js @@ -1,13 +1,13 @@ -var app = require('../app.js'); -var data = require('../data.js'); -var logger = require('../logging.js'); +const state = require('../state.js'); +const data = require('../data.js'); +const logger = require('../logging.js'); exports.roles = ['Admins', 'Moderators']; -exports.command = function(message) { +exports.command = function (message) { message.mentions.users.map((user) => { - var count = app.warnings.filter(x => x.id == user.id && !x.cleared); + var count = state.warnings.filter(x => x.id === user.id && !x.cleared); if (count != null && count.length > 0) { - count.forEach(warning => warning.cleared = true); + count.forEach(warning => { warning.cleared = true; }); data.flushWarnings(); message.channel.sendMessage(`${user}, your warnings have been cleared.`); } else { @@ -15,6 +15,6 @@ exports.command = function(message) { } logger.info(`${message.author.toString()} has cleared all warnings for ${user.toString()} [${count}].`); - app.logChannel.sendMessage(`${message.author.toString()} has cleared all warnings for ${user.toString()} [${count}].`); + state.logChannel.sendMessage(`${message.author.toString()} has cleared all warnings for ${user.toString()} [${count}].`); }); -} +}; diff --git a/commands/grantDeveloper.js b/src/commands/grantDeveloper.js similarity index 82% rename from commands/grantDeveloper.js rename to src/commands/grantDeveloper.js index 66e6e0a..7f6b66f 100644 --- a/commands/grantDeveloper.js +++ b/src/commands/grantDeveloper.js @@ -1,8 +1,5 @@ -var app = require('../app.js'); -var logger = require('../logging.js'); - exports.roles = ['Admins', 'Moderators', 'CitraBot']; -exports.command = function(message) { +exports.command = function (message) { var role = '345247291843805185'; message.mentions.users.map((user) => { let member = message.guild.member(user); diff --git a/commands/quote.js b/src/commands/quote.js similarity index 69% rename from commands/quote.js rename to src/commands/quote.js index d3a4ea1..7f1c36b 100644 --- a/commands/quote.js +++ b/src/commands/quote.js @@ -1,15 +1,11 @@ -var app = require('../app.js'); -var logger = require('../logging.js'); - exports.roles = ['Admins', 'Moderators']; -exports.command = function(message, reply) { +exports.command = function (message, reply) { let replyMessage = 'Hello.'; if (reply == null) { replyMessage = message.content.substr(message.content.indexOf(' ') + 1); - } - else { + } else { replyMessage = `${message.mentions.users.map(user => `${user}`)} ${reply}`; } message.channel.sendMessage(replyMessage); -} +}; diff --git a/src/commands/warn.js b/src/commands/warn.js new file mode 100644 index 0000000..de6e796 --- /dev/null +++ b/src/commands/warn.js @@ -0,0 +1,24 @@ +const state = require('../state.js'); +const data = require('../data.js'); +const logger = require('../logging.js'); +const UserWarning = require('../models/UserWarning.js'); + +exports.roles = ['Admins', 'Moderators']; +exports.command = function (message) { + message.mentions.users.map((user) => { + var count = state.warnings.filter(x => x.id === user.id && !x.cleared).length || 0; + message.channel.sendMessage(`${user} You have been warned. Additional infractions may result in a ban.`); + + logger.info(`${message.author.username} ${message.author} has warned ${user.username} ${user} [${count} + 1].`); + state.logChannel.sendMessage(`${message.author} has warned ${user} [${count} + 1].`); + + state.warnings.push(new UserWarning(user.id, user.username, message.author.id, message.author.username, count)); + data.flushWarnings(); + + state.stats.warnings += 1; + + if (count + 1 >= 3) { + state.logChannel.sendMessage(`.ban ${user}`); + } + }); +}; diff --git a/src/commands/warnings.js b/src/commands/warnings.js new file mode 100644 index 0000000..3fcbb3a --- /dev/null +++ b/src/commands/warnings.js @@ -0,0 +1,8 @@ +const state = require('../state.js'); + +exports.command = function (message) { + message.mentions.users.map((user) => { + var warnings = state.warnings.filter(x => x.id === user.id && !x.cleared); + message.channel.sendMessage(`${user}, you have ${warnings.length} total warnings.`); + }); +}; diff --git a/src/data.js b/src/data.js new file mode 100644 index 0000000..15e6139 --- /dev/null +++ b/src/data.js @@ -0,0 +1,41 @@ +const fs = require('fs'); +const state = require('./state.js'); +const logger = require('./logging.js'); + +function readWarnings () { + // Load the warnings file into the bans variable. + fs.readFile('data/discordWarnings.json', 'utf8', function (err, data) { + if (err && err.code === 'ENOENT') { return; } + if (err) { logger.error(err); } + state.warnings = JSON.parse(data); + logger.debug('Loaded warnings file.'); + }); +} + +function readBans () { + // Load the ban file into the bans variable. + fs.readFile('data/discordBans.json', 'utf8', function (err, data) { + if (err && err.code === 'ENOENT') { return; } + if (err) { logger.error(err); } + state.bans = JSON.parse(data); + logger.debug('Loaded bans file.'); + }); +} + +function flushWarnings () { + var warningsJson = JSON.stringify(state.warnings, null, 4); + if (!fs.existsSync('./data/')) fs.mkdirSync('data'); + fs.writeFile('data/discordWarnings.json', warningsJson, 'utf8', function (err) { + if (err) { logger.error(err); } + }); +} + +function flushBans () { + var bansJson = JSON.stringify(state.bans, null, 4); + if (!fs.existsSync('data')) fs.mkdirSync('data'); + fs.writeFile('data/discordBans.json', bansJson, 'utf8', function (err) { + if (err) { logger.error(err); } + }); +} + +module.exports = { readWarnings: readWarnings, readBans: readBans, flushWarnings: flushWarnings, flushBans: flushBans }; diff --git a/logging.js b/src/logging.js similarity index 100% rename from logging.js rename to src/logging.js diff --git a/models/UserBan.js b/src/models/UserBan.js similarity index 76% rename from models/UserBan.js rename to src/models/UserBan.js index 5ee3a59..b16f629 100644 --- a/models/UserBan.js +++ b/src/models/UserBan.js @@ -1,5 +1,5 @@ class UserBan { - constructor(id, username, warnedBy, warnedByUsername, priorWarnings) { + constructor (id, username, warnedBy, warnedByUsername, priorWarnings) { this.id = id; this.username = username; this.date = new Date(); diff --git a/models/UserWarning.js b/src/models/UserWarning.js similarity index 77% rename from models/UserWarning.js rename to src/models/UserWarning.js index 9b4a233..81675ee 100644 --- a/models/UserWarning.js +++ b/src/models/UserWarning.js @@ -1,5 +1,5 @@ class UserWarning { - constructor(id, username, warnedBy, warnedByUsername, priorWarnings) { + constructor (id, username, warnedBy, warnedByUsername, priorWarnings) { this.id = id; this.username = username; this.date = new Date(); diff --git a/src/responses.json b/src/responses.json new file mode 100644 index 0000000..5cb0092 --- /dev/null +++ b/src/responses.json @@ -0,0 +1,22 @@ +{ + "pmReply": "Please refer to our **Frequently Asked Questions**. ", + "quotes": { + "faq": { "reply": "Please refer to our **Frequently Asked Questions**. " }, + "cpu": { "reply": "Citra requires powerful single-core performance. Refer to your CPU in this graph. Your experience with Citra won't be enjoyable in most games if it's below 1,800. " }, + "requirements": { "reply": "Please refer to our **Frequently Asked Questions**. The only requirements for Citra are a GPU that supports at least OpenGL 3.3 and a 64-bit OS, but you definitely want a processor with the highest possible performance per core. "}, + "roms": { "warn": true, "reply": "Please read our __community rules__. Warez/downloading games talk is strictly prohibited. To prevent legal issues, you are not allowed to post links or refer to any kind of ROM, NAND, ISO, game, or other copyrighted material that has been illegally obtained or shared. "}, + "dump-game": { "reply": "Please refer to our __game dumping guides__. & "}, + "dump-system": { "reply": "Please refer to our __system dumping guide__. "}, + "pokemon": { "reply": "Click here to view our game compatibility list: "}, + "alpha": { "reply": "*Citra* is currently in very early stages of development. Games usually run less than full-speed even on the best computers. Expect bugs and glitches to appear in most games. Many features found in more mature emulators are still in the works. For any major updates, please visit "}, + "updates": { "reply": "You can check our latest updates on *Github*. "}, + "download": { "reply": "Please only download from the official *Citra* website, as downloading from other sources is not supported here. "}, + "legal": { "reply": "*Citra* is legal, we don't support illegal activities. Dumping your purchased games and system files from your 3DS is legal. Downloading them is not."}, + "building": { "reply": "Please refer to our building guides.\nWindows: \nmacOS: \nLinux: "}, + "controller": { "reply": "This forum topic tells you how to __configure your gamepad / controller__: "}, + "issues": { "reply": "This forum topic lists __known issues in games and their workarounds__: \nPlease read it carefully. It includes help with most games"}, + "forum": { "reply": "This question might be more suitable for the *Citra* forum. "}, + "game": { "reply": "Click here to view our game compatibility list: "}, + "log": { "reply": "This forum topic tells you how to __get the log file__: "} + } +} diff --git a/server.js b/src/server.js similarity index 81% rename from server.js rename to src/server.js index f2f3a86..3e3f718 100644 --- a/server.js +++ b/src/server.js @@ -4,12 +4,13 @@ require('checkenv').check(); const discord = require('discord.js'); const path = require('path'); const schedule = require('node-schedule'); +const fs = require('fs'); const logger = require('./logging.js'); -const app = require('./app.js'); +const state = require('./state.js'); const data = require('./data.js'); -var responses = require('./data/responses.json'); +var responses = require('./responses.json'); var cachedModules = []; var cachedTriggers = []; @@ -27,30 +28,30 @@ function findArray (haystack, arr) { client.on('ready', () => { // Initalize app channels. - app.logChannel = client.channels.get(process.env.DISCORD_LOG_CHANNEL); - app.guild = app.logChannel.guild; + state.logChannel = client.channels.get(process.env.DISCORD_LOG_CHANNEL); + state.guild = state.logChannel.guild; logger.info('Bot is now online and connected to server.'); }); client.on('guildMemberAdd', (member) => { - app.stats.joins += 1; + state.stats.joins += 1; }); client.on('guildMemberRemove', (member) => { - app.stats.leaves += 1; + state.stats.leaves += 1; }); -// Output the stats for app.stats every 24 hours. +// Output the stats for state.stats every 24 hours. // Server is in UTC mode, 11:30 EST would be 03:30 UTC. schedule.scheduleJob({ hour: 3, minute: 30 }, function () { - logger.info(`Here are today's stats for ${(new Date()).toLocaleDateString()}! ${app.stats.joins} users have joined, ${app.stats.leaves} users have left, ${app.stats.warnings} warnings have been issued.`); - app.logChannel.sendMessage(`Here are today's stats for ${(new Date()).toLocaleDateString()}! ${app.stats.joins} users have joined, ${app.stats.leaves} users have left, ${app.stats.warnings} warnings have been issued.`); + logger.info(`Here are today's stats for ${(new Date()).toLocaleDateString()}! ${state.stats.joins} users have joined, ${state.stats.leaves} users have left, ${state.stats.warnings} warnings have been issued.`); + state.logChannel.sendMessage(`Here are today's stats for ${(new Date()).toLocaleDateString()}! ${state.stats.joins} users have joined, ${state.stats.leaves} users have left, ${state.stats.warnings} warnings have been issued.`); // Clear the stats for the day. - app.stats.joins = 0; - app.stats.leaves = 0; - app.stats.warnings = 0; + state.stats.joins = 0; + state.stats.leaves = 0; + state.stats.warnings = 0; }); client.on('message', message => { @@ -59,7 +60,7 @@ client.on('message', message => { if (message.guild == null && responses.pmReply) { // We want to log PM attempts. logger.info(`${message.author.username} ${message.author} [PM]: ${message.content}`); - app.logChannel.sendMessage(`${message.author} [PM]: ${message.content}`); + state.logChannel.sendMessage(`${message.author} [PM]: ${message.content}`); message.reply(responses.pmReply); return; } @@ -79,7 +80,7 @@ client.on('message', message => { if (cachedModule) { // Check access permissions. if (cachedModule.roles !== undefined && findArray(message.member.roles.map(function (x) { return x.name; }), cachedModule.roles) === false) { - app.logChannel.sendMessage(`${message.author} attempted to use admin command: ${message.content}`); + state.logChannel.sendMessage(`${message.author} attempted to use admin command: ${message.content}`); logger.info(`${message.author.username} ${message.author} attempted to use admin command: ${message.content}`); return false; } @@ -127,7 +128,7 @@ client.on('message', message => { // Cache all command modules. cachedModules = []; -require('fs').readdirSync('./commands/').forEach(function (file) { +fs.readdirSync('./src/commands/').forEach(function (file) { // Load the module if it's a script. if (path.extname(file) === '.js') { if (file.includes('.disabled')) { @@ -141,7 +142,7 @@ require('fs').readdirSync('./commands/').forEach(function (file) { // Cache all triggers. cachedTriggers = []; -require('fs').readdirSync('./triggers/').forEach(function (file) { +fs.readdirSync('./src/triggers/').forEach(function (file) { // Load the trigger if it's a script. if (path.extname(file) === '.js') { if (file.includes('.disabled')) { diff --git a/app.js b/src/state.js similarity index 72% rename from app.js rename to src/state.js index ab25fd0..a3021b6 100644 --- a/app.js +++ b/src/state.js @@ -1,5 +1,5 @@ /* Application State */ -var Application = function () { +var State = function () { this.guild = null; this.logChannel = null; this.warnings = []; @@ -11,4 +11,4 @@ var Application = function () { }; }; -module.exports = new Application(); +module.exports = new State(); diff --git a/triggers/github.js b/src/triggers/github.js similarity index 62% rename from triggers/github.js rename to src/triggers/github.js index 36dc6f7..9411b36 100644 --- a/triggers/github.js +++ b/src/triggers/github.js @@ -1,20 +1,18 @@ -var request = require('request'); -var app = require('../app.js'); -var logger = require('../logging.js'); +const request = require('request'); -var regex = /[^\<\\]\#(\d+)/ig; +const regex = /[^\<\\]\#(\d+)/ig; -exports.trigger = function(message) { +exports.trigger = function (message) { return new RegExp(regex).test(message.content); -} +}; -exports.execute = function(message) { +exports.execute = function (message) { let matcher = new RegExp(regex); let match = matcher.exec(message.content); let matched = []; - while(match != null) { - if(matched.indexOf(match[1]) == -1) { + while (match != null) { + if (matched.indexOf(match[1]) === -1) { matched.push(match[1]); } else { match = matcher.exec(message.content); @@ -22,18 +20,18 @@ exports.execute = function(message) { } // We do not want to automatically match old issues / PRs. - // This usually happens when someone messes up pining another person or + // This usually happens when someone messes up pinging another person or // in general conversation. // ex: You're #1! if (match[1] <= 2000) { return; } let url = `https://github.com/citra-emu/citra/pull/${match[1]}`; request(url, function (error, response, body) { - if (!error && response.statusCode == 200) { + if (!error && response.statusCode === 200) { message.channel.sendMessage(`Github Pull Request: ${url}`); } }); match = matcher.exec(message.content); } -} +};