diff --git a/.vscode/launch.json b/.vscode/launch.json index 67bd32d..c063aac 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "name": "development", "runtimeExecutable": "node", "env": { - "NODE_ENV": "development" + "CHITANDA_ENV": "development" }, "runtimeArgs": [ "node_modules/nodemon/bin/nodemon.js", diff --git a/bot.js b/bot.js index 9ed763d..7f5c96e 100644 --- a/bot.js +++ b/bot.js @@ -15,7 +15,8 @@ client.once('ready', function() { client.commandManager.loadCommands(); client.database.connect() .then(_ => { - client.database.guildSettingManager.loadSettings(); + client.schedulerManager.startAll(); + // client.database.guildSettingManager.loadSettings(); }); client.presenceManager.start(); }); diff --git a/chitanda.js b/chitanda.js index 947e2b3..1c1e155 100644 --- a/chitanda.js +++ b/chitanda.js @@ -1,9 +1,11 @@ const Discord = require('discord.js') const config = require('./config.json') +const DatabaseFactory = require('./database/databaseFactory') const CommandManager = require('./managers/commandManager.js') const PresenceManager = require('./managers/presenceManager.js') const BotListingManager = require('./managers/botListingManager') +const SchedulerManager = require('./managers/schedulerManager') const ErrorLog = require('./logger/errorlog') const CommandLog = require('./logger/commandLog') const ChatLog = require('./logger/chatLog') @@ -17,8 +19,10 @@ class Chitanda extends Discord.Client { */ constructor(cmdPath, options) { super(options); - this.ready = false + this.ready = false; + this.messageUtil = new MessageUtil(this); this.errorLogger = new ErrorLog(this); + this.database = new DatabaseFactory(this); this.commandManager = new CommandManager(this, cmdPath); this.presenceManager = new PresenceManager(this, config); // this.botListingManager = new BotListingManager(this); @@ -26,7 +30,7 @@ class Chitanda extends Discord.Client { this.commandLogger = new CommandLog(this); this.chatLogger = new ChatLog(this); this.guildLogger = new GuildLog(this); - this.messageUtil = new MessageUtil(this); + this.schedulerManager = new SchedulerManager(this); } delayCreateBotListingManager() { diff --git a/config.json b/config.json index dc44f89..fe9754d 100644 --- a/config.json +++ b/config.json @@ -10,6 +10,7 @@ "dmCommand": true, "tracking": [], "botname": "BotName", + "database": "mongodb-connection-uri", "cleverbot": { "user": "cleverbot.io_api_user", "key": "cleverbot.io_api_key" diff --git a/database/databaseFactory.js b/database/databaseFactory.js new file mode 100644 index 0000000..e978f98 --- /dev/null +++ b/database/databaseFactory.js @@ -0,0 +1,26 @@ +const mongoose = require('mongoose') + +const config = require('../config.json') + +class DatabaseFactory { + /** + * @param {import('../chitanda')} client + */ + constructor(client) { + this.client = client; + } + + connect() { + return mongoose.connect(config.database, { useNewUrlParser: true }) + .then(() => { + this.client.errorLogger.info('Connected to mongodb', true); + return true; + }) + .catch(err => { + this.client.errorLogger.error(err); + process.exit(1); + }); + } +} + +module.exports = DatabaseFactory; \ No newline at end of file diff --git a/logger/errorlog.js b/logger/errorlog.js index 58a1e61..1d7ad77 100644 --- a/logger/errorlog.js +++ b/logger/errorlog.js @@ -1,6 +1,6 @@ -const config = require('../config.json'); -const prodEnv = "PRODUCTION"; -const textChat = ['text', 'dm', 'group']; +const config = require('../config.json') +const util = require('../util/commonUtil') +const textChat = ['text', 'dm', 'group'] //not sending these log to discord channel const ignore = { @@ -25,10 +25,7 @@ class ErrorLog { console.log('[Info] ' + message); try { - if (consoleOnly || - !process.env.CHITANDA_ENV || - process.env.CHITANDA_ENV.toUpperCase() != prodEnv - ) { + if (consoleOnly || !util.isProduction()) { return; } @@ -50,10 +47,7 @@ class ErrorLog { console.warn('[Warn] ' + message); try { - if (consoleOnly || - !process.env.CHITANDA_ENV || - process.env.CHITANDA_ENV.toUpperCase() != prodEnv - ) { + if (consoleOnly || !util.isProduction()) { return; } @@ -81,10 +75,7 @@ class ErrorLog { } try { - if (consoleOnly || - !process.env.CHITANDA_ENV || - process.env.CHITANDA_ENV.toUpperCase() != prodEnv - ) { + if (consoleOnly || !util.isProduction()) { return; } @@ -112,10 +103,7 @@ class ErrorLog { console.error(err.stack); try { - if (consoleOnly || - !process.env.CHITANDA_ENV || - process.env.CHITANDA_ENV.toUpperCase() != prodEnv - ) { + if (consoleOnly || !util.isProduction()) { return; } diff --git a/managers/botListingManager.js b/managers/botListingManager.js index 6d9c29a..51f0799 100644 --- a/managers/botListingManager.js +++ b/managers/botListingManager.js @@ -1,11 +1,6 @@ const DiscordBotOrg = require('../services/discordBots.org') const BotDiscordPw = require('../services/botsDiscord.Pw') -const messages = [ - 'Hey hey. I just got voted. I\'m curious, who did vote me?', - 'Thank for voting me. Please keep the flame' -] - class BotListingManager { /** * @param {import('../chitanda')} client @@ -14,47 +9,7 @@ class BotListingManager { this.client = client; this.discordBotOrg = new DiscordBotOrg(client); this.botDiscordPw = new BotDiscordPw(client); - - this.discordBotOrg.onVote((userId, isWeekend) => { - try { - this.thankVote(userId, isWeekend); - } catch (err) { - this.client.errorLogger.error(err); - } - }); - } - - thankVote(userId, isWeekend) { - //TODO: remove these debug after success on production - console.log(userId) - console.log(isWeekend) - this.client.fetchUser(userId) - .then(user => { - if (user.dmChannel) { - sendThank(this.client, user.dmChannel, user); - } else { - user.createDM().then(dm => { - sendThank(this.client, dm, user); - }) - } - }) - .catch(err => { - this.client.errorLogger.error(err); - }) } } -/** - * - * @param {import('../chitanda')} client - * @param {import('discord.js').DMChannel} dmChannel - * @param {import('discord.js').User} user - */ -var sendThank = function(client, dmChannel, user) { - client.messageUtil.sendFromChannel( - dmChannel, - messages[Math.floor(Math.random() * messages.length)] - ); -} - module.exports = BotListingManager; \ No newline at end of file diff --git a/managers/schedulerManager.js b/managers/schedulerManager.js new file mode 100644 index 0000000..0e81caf --- /dev/null +++ b/managers/schedulerManager.js @@ -0,0 +1,17 @@ +const VoteScheduler = require('../schedulers/voteScheduler') + +class SchedulerManager { + /** + * @param {import('../chitanda')} client + */ + constructor(client) { + this.client = client; + this.voteScheduler = new VoteScheduler(client); + } + + startAll() { + this.voteScheduler.start(); + } +} + +module.exports = SchedulerManager; \ No newline at end of file diff --git a/package.json b/package.json index 04d54ee..44f1f76 100644 --- a/package.json +++ b/package.json @@ -18,14 +18,16 @@ }, "dependencies": { "@types/node": "^9.6.1", + "agenda": "^2.0.1", "bluebird": "^3.5.1", + "body-parser": "^1.18.3", "cleverbot.io": "^1.0.4", "dblapi.js": "^2.2.0", "discord.js": "^11.3.2", "express": "^4.16.3", - "express-http-proxy": "^1.2.0", "leetscript": "^1.1.0", "moment": "^2.22.2", + "mongoose": "^5.2.13", "sync-request": "^6.0.0" }, "devDependencies": { diff --git a/services/discordBots.org.js b/services/discordBots.org.js index 6253a03..b4eb398 100644 --- a/services/discordBots.org.js +++ b/services/discordBots.org.js @@ -9,13 +9,7 @@ class DiscordBotOrg { */ constructor(client) { this.listeners = []; - this.service = new DBL(config.discordBotOrg.token, - { - webhookPort: 5000, - webhookAuth: config.discordBotOrg.webhookAuth - }, - client - ); + this.service = new DBL(config.discordBotOrg.token, client); this.service.on('posted', () => { client.errorLogger.info(message, true); @@ -24,21 +18,6 @@ class DiscordBotOrg { client.errorLogger.error(err); }); - this.service.webhook.on('ready', hook => { - client.errorLogger.info(`Internal webhook running at http://${hook.hostname}:${hook.port}${hook.path}`, true); - }); - this.service.webhook.on('vote', vote => { - //TODO: remove these debug after success on production - console.log(vote) - this.listeners.forEach(listener => { - try { - listener(vote.user, vote.isWeekend); - } catch (err) { - client.errorLogger.error(err); - } - }) - }); - this.service.postStats(client.guilds.size) .then(() => { client.errorLogger.info(message, true); @@ -47,12 +26,6 @@ class DiscordBotOrg { client.errorLogger.error(err); }); } - - onVote(listener) { - if (typeof listener == 'function') { - this.listeners.push(listener); - } - } } module.exports = DiscordBotOrg; \ No newline at end of file diff --git a/util/commonUtil.js b/util/commonUtil.js index 9abe119..e864677 100644 --- a/util/commonUtil.js +++ b/util/commonUtil.js @@ -1,3 +1,9 @@ +const prodEnv = "PRODUCTION"; + exports.randomTrue = () => { return Math.random() > 0.5; +} + +exports.isProduction = () => { + return process.env.CHITANDA_ENV && process.env.CHITANDA_ENV.toUpperCase() == prodEnv; } \ No newline at end of file diff --git a/web.js b/web.js index f85d4e2..526bdd6 100644 --- a/web.js +++ b/web.js @@ -1,20 +1,47 @@ /**======= Copy right - Sait 2018 + Sait 2018 =====================*/ const express = require('express') -const proxy = require('express-http-proxy') +const bodyParser = require('body-parser') +const agenda = require('agenda') const app = express() +const jobQueue = new agenda() + const config = require('./config.json') +const VoteScheduler = require('./schedulers/voteScheduler') const path = '/dblwebhook' const port = process.env.PORT || 5555 -app.use(path, proxy('localhost:5000')); +jobQueue.database(config.database, VoteScheduler.voteCollection, { useNewUrlParser: true }); + +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); app.get('/', (req, res) => { res.send('How did you find this owo'); }) +app.use(path, async (req, res) => { + var auth = config.discordBotOrg.webhookAuth; + if (auth && auth !== req.headers.authorization) { + res.status(401).end(); + return; + } + + await jobQueue.start(); + jobQueue.now(VoteScheduler.voteJob, { + header: { + host: req.headers.host, + origin: req.headers.origin, + // authorization: req.headers.authorization + }, + body: req.body + }).then(() => { + res.status(200).end(); + }) +}) + app.listen(port, () => { console.log(`[Info] Webhook running at ${path} on port ${port}`); }) \ No newline at end of file