From 540ab231dbefd2a500c00f01368704888790ee99 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 4 Mar 2019 21:19:03 +0100 Subject: [PATCH] Changes to structure and fixes - added folders for every lib - moved lib files to folders and renamed them to `index.js` - moved commands outside of lib - moved graphql schema to web - removed lib graphql folder - added graphql folder to AniListApi folder - fixed bug on ~skip and ~stop - fixed bug on ExtendedRichEmbed - fixed bug on RichCharacterInfo --- CHANGELOG.md | 7 ++++ bot.js | 16 ++++---- {lib/commands => commands}/.template/index.js | 2 +- .../.template/template.yaml | 0 .../AnilistApiCommands/index.js | 6 +-- .../AnilistApiCommands/template.yaml | 0 .../InfoCommands/index.js | 4 +- .../InfoCommands/template.yaml | 0 .../MiscCommands/index.js | 2 +- .../MiscCommands/template.yaml | 0 .../MusicCommands/index.js | 10 ++--- .../MusicCommands/template.yaml | 0 .../ServerUtilityCommands/index.js | 2 +- .../ServerUtilityCommands/template.yaml | 0 .../UtilityCommands/index.js | 2 +- .../UtilityCommands/template.yaml | 0 lib/{CommandLib.js => CommandLib/index.js} | 40 ++++++++++--------- lib/{MessageLib.js => MessageLib/index.js} | 6 +-- lib/{MusicLib.js => MusicLib/index.js} | 6 +-- lib/StateLib/index.js | 26 ++++++++++++ lib/{WebLib.js => WebLib/index.js} | 8 ++-- .../graphql}/AnimeQuery.gql | 0 .../graphql}/CharacterQuery.gql | 0 .../graphql}/Fragments.yaml | 0 .../graphql}/MangaQuery.gql | 0 .../graphql}/StaffQuery.gql | 0 .../{AnilistApi.js => AniListApi/index.js} | 0 lib/guilding.js | 4 +- lib/{utils.js => utils/index.js} | 0 lib/{ => utils}/logging.js | 0 lib/{ => utils}/sqliteAsync.js | 0 test/test.js | 4 +- {lib => web}/api/graphql/schema.gql | 0 33 files changed, 91 insertions(+), 54 deletions(-) rename {lib/commands => commands}/.template/index.js (95%) rename {lib/commands => commands}/.template/template.yaml (100%) rename {lib/commands => commands}/AnilistApiCommands/index.js (98%) rename {lib/commands => commands}/AnilistApiCommands/template.yaml (100%) rename {lib/commands => commands}/InfoCommands/index.js (98%) rename {lib/commands => commands}/InfoCommands/template.yaml (100%) rename {lib/commands => commands}/MiscCommands/index.js (98%) rename {lib/commands => commands}/MiscCommands/template.yaml (100%) rename {lib/commands => commands}/MusicCommands/index.js (97%) rename {lib/commands => commands}/MusicCommands/template.yaml (100%) rename {lib/commands => commands}/ServerUtilityCommands/index.js (99%) rename {lib/commands => commands}/ServerUtilityCommands/template.yaml (100%) rename {lib/commands => commands}/UtilityCommands/index.js (98%) rename {lib/commands => commands}/UtilityCommands/template.yaml (100%) rename lib/{CommandLib.js => CommandLib/index.js} (88%) rename lib/{MessageLib.js => MessageLib/index.js} (98%) rename lib/{MusicLib.js => MusicLib/index.js} (98%) create mode 100644 lib/StateLib/index.js rename lib/{WebLib.js => WebLib/index.js} (98%) rename lib/api/{graphql/AnilistApi => AniListApi/graphql}/AnimeQuery.gql (100%) rename lib/api/{graphql/AnilistApi => AniListApi/graphql}/CharacterQuery.gql (100%) rename lib/api/{graphql/AnilistApi => AniListApi/graphql}/Fragments.yaml (100%) rename lib/api/{graphql/AnilistApi => AniListApi/graphql}/MangaQuery.gql (100%) rename lib/api/{graphql/AnilistApi => AniListApi/graphql}/StaffQuery.gql (100%) rename lib/api/{AnilistApi.js => AniListApi/index.js} (100%) rename lib/{utils.js => utils/index.js} (100%) rename lib/{ => utils}/logging.js (100%) rename lib/{ => utils}/sqliteAsync.js (100%) rename {lib => web}/api/graphql/schema.gql (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index d58a7ec..c387313 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- bug where the bot counts itself when calculating needed votes to skip/stop music +- bug on the `ExtendedRichEmbed` where `addField` and `setDescription` throws an error when the value is null or undefined +- bug on `AnilistApiCommands` where the `RichCharacterInfo` uses a nonexistent function of the `ExtendedRichEmbed` + ### Changed - name of MiscCommands module from `TemplateCommandModule` to `MiscoCommandModule` +- moved everything in `lib` to subfolders with the same name as the files and renamed the files to `index.js` +- moved commands outside of `lib` ## [0.11.0-beta] - 2019-03-03 ### Changed diff --git a/bot.js b/bot.js index 155ec78..2c3a2f9 100644 --- a/bot.js +++ b/bot.js @@ -1,12 +1,12 @@ const Discord = require("discord.js"), fs = require('fs-extra'), - logging = require('./lib/logging'), + logging = require('./lib/utils/logging'), msgLib = require('./lib/MessageLib'), guilding = require('./lib/guilding'), utils = require('./lib/utils'), config = require('./config.json'), args = require('args-parser')(process.argv), - sqliteAsync = require('./lib/sqliteAsync'), + sqliteAsync = require('./lib/utils/sqliteAsync'), authToken = args.token || config.api.botToken, prefix = args.prefix || config.prefix || '~', gamepresence = args.game || config.presence; @@ -69,24 +69,24 @@ class Bot { if (config.webinterface && config.webinterface.enabled) await this.initializeWebserver(); this.logger.verbose('Registering commands'); - await this.messageHandler.registerCommandModule(require('./lib/commands/AnilistApiCommands').module, {}); - await this.messageHandler.registerCommandModule(require('./lib/commands/UtilityCommands').module, { + await this.messageHandler.registerCommandModule(require('./commands/AnilistApiCommands').module, {}); + await this.messageHandler.registerCommandModule(require('./commands/UtilityCommands').module, { bot: this, config: config }); - await this.messageHandler.registerCommandModule(require('./lib/commands/InfoCommands').module, { + await this.messageHandler.registerCommandModule(require('./commands/InfoCommands').module, { client: this.client, messageHandler: this.messageHandler }); - await this.messageHandler.registerCommandModule(require('./lib/commands/MusicCommands').module, { + await this.messageHandler.registerCommandModule(require('./commands/MusicCommands').module, { getGuildHandler: async (g) => await this.getGuildHandler(g) }); - await this.messageHandler.registerCommandModule(require('./lib/commands/ServerUtilityCommands').module, { + await this.messageHandler.registerCommandModule(require('./commands/ServerUtilityCommands').module, { getGuildHandler: async (g) => await this.getGuildHandler(g), messageHandler: this.messageHandler, config: config }); - await this.messageHandler.registerCommandModule(require('./lib/commands/MiscCommands').module, {}); + await this.messageHandler.registerCommandModule(require('./commands/MiscCommands').module, {}); this.registerEvents(); } diff --git a/lib/commands/.template/index.js b/commands/.template/index.js similarity index 95% rename from lib/commands/.template/index.js rename to commands/.template/index.js index 399ac59..caf55f0 100644 --- a/lib/commands/.template/index.js +++ b/commands/.template/index.js @@ -1,5 +1,5 @@ /* template index.js. Doesn't implement actual commands */ -const cmdLib = require('../../CommandLib'); // required for command objects +const cmdLib = require('../../lib/CommandLib'); // required for command objects /** * A description what the command module includes and why. Doesn't need to list commands but explains diff --git a/lib/commands/.template/template.yaml b/commands/.template/template.yaml similarity index 100% rename from lib/commands/.template/template.yaml rename to commands/.template/template.yaml diff --git a/lib/commands/AnilistApiCommands/index.js b/commands/AnilistApiCommands/index.js similarity index 98% rename from lib/commands/AnilistApiCommands/index.js rename to commands/AnilistApiCommands/index.js index 1e8e4fd..af30baa 100644 --- a/lib/commands/AnilistApiCommands/index.js +++ b/commands/AnilistApiCommands/index.js @@ -1,5 +1,5 @@ -const cmdLib = require('../../CommandLib'), - anilistApi = require('../../api/AnilistApi'); +const cmdLib = require('../../lib/CommandLib'), + anilistApi = require('../../lib/api/AniListApi'); /** * The AniList commands are all commands that interact with the anilist api. @@ -144,7 +144,7 @@ class RichCharacterInfo extends cmdLib.ExtendedRichEmbed { .replace(/~!.*?!~/g, '') .replace(/\n\n\n/g, '')); if (characterInfo.media && characterInfo.media.edges) - this.addNonemptyField( + this.addField( 'Media Appeareance', characterInfo.media.edges.map(x => { let media = x.node; diff --git a/lib/commands/AnilistApiCommands/template.yaml b/commands/AnilistApiCommands/template.yaml similarity index 100% rename from lib/commands/AnilistApiCommands/template.yaml rename to commands/AnilistApiCommands/template.yaml diff --git a/lib/commands/InfoCommands/index.js b/commands/InfoCommands/index.js similarity index 98% rename from lib/commands/InfoCommands/index.js rename to commands/InfoCommands/index.js index fbff255..ac68869 100644 --- a/lib/commands/InfoCommands/index.js +++ b/commands/InfoCommands/index.js @@ -1,6 +1,6 @@ -const cmdLib = require('../../CommandLib'), +const cmdLib = require('../../lib/CommandLib'), fsx = require('fs-extra'), - utils = require('../../utils'); + utils = require('../../lib/utils'); /** * Info commands provide information about the bot. These informations are diff --git a/lib/commands/InfoCommands/template.yaml b/commands/InfoCommands/template.yaml similarity index 100% rename from lib/commands/InfoCommands/template.yaml rename to commands/InfoCommands/template.yaml diff --git a/lib/commands/MiscCommands/index.js b/commands/MiscCommands/index.js similarity index 98% rename from lib/commands/MiscCommands/index.js rename to commands/MiscCommands/index.js index b7e724b..f9cc408 100644 --- a/lib/commands/MiscCommands/index.js +++ b/commands/MiscCommands/index.js @@ -1,5 +1,5 @@ /* template index.js. Doesn't implement actual commands */ -const cmdLib = require('../../CommandLib'); +const cmdLib = require('../../lib/CommandLib'); /** * Several commands that are that special that they can't be included in any other module. diff --git a/lib/commands/MiscCommands/template.yaml b/commands/MiscCommands/template.yaml similarity index 100% rename from lib/commands/MiscCommands/template.yaml rename to commands/MiscCommands/template.yaml diff --git a/lib/commands/MusicCommands/index.js b/commands/MusicCommands/index.js similarity index 97% rename from lib/commands/MusicCommands/index.js rename to commands/MusicCommands/index.js index c701326..469749d 100644 --- a/lib/commands/MusicCommands/index.js +++ b/commands/MusicCommands/index.js @@ -1,6 +1,6 @@ -const cmdLib = require('../../CommandLib'), - utils = require('../../utils'), - config = require('../../../config'); +const cmdLib = require('../../lib/CommandLib'), + utils = require('../../lib/utils'), + config = require('../../config'); function checkPermission(msg, rolePerm) { if (!rolePerm || ['all', 'any', 'everyone'].includes(rolePerm)) @@ -117,7 +117,7 @@ class MusicCommandModule extends cmdLib.CommandModule { let vc = gh.musicPlayer.voiceChannel || m.member.voiceChannel; if (gh.musicPlayer.connected && vc) { let votes = gh.updateCommandVote(stop.name, m.author.tag); - let neededVotes = Math.ceil(vc.members.size/2); + let neededVotes = Math.ceil((vc.members.size - 1) / 2); if (neededVotes <= votes.count || checkPermission(m, 'dj')) { this._logger.debug(`Vote passed. ${votes.count} out of ${neededVotes} for stop or permission granted`); @@ -167,7 +167,7 @@ class MusicCommandModule extends cmdLib.CommandModule { let vc = gh.musicPlayer.voiceChannel || m.member.voiceChannel; if (gh.musicPlayer.playing && vc) { let votes = gh.updateCommandVote(skip.name, m.author.tag); - let neededVotes = Math.ceil(vc.members.size/2); + let neededVotes = Math.ceil((vc.members.size - 1) / 2); if (neededVotes <= votes.count || checkPermission(m, 'dj')) { this._logger.debug(`Vote passed. ${votes.count} out of ${neededVotes} for skip or permission granted`); diff --git a/lib/commands/MusicCommands/template.yaml b/commands/MusicCommands/template.yaml similarity index 100% rename from lib/commands/MusicCommands/template.yaml rename to commands/MusicCommands/template.yaml diff --git a/lib/commands/ServerUtilityCommands/index.js b/commands/ServerUtilityCommands/index.js similarity index 99% rename from lib/commands/ServerUtilityCommands/index.js rename to commands/ServerUtilityCommands/index.js index b9c98ca..a560cc3 100644 --- a/lib/commands/ServerUtilityCommands/index.js +++ b/commands/ServerUtilityCommands/index.js @@ -1,4 +1,4 @@ -const cmdLib = require('../../CommandLib'); +const cmdLib = require('../../lib/CommandLib'); /** * This command module includes utility commands for the server. diff --git a/lib/commands/ServerUtilityCommands/template.yaml b/commands/ServerUtilityCommands/template.yaml similarity index 100% rename from lib/commands/ServerUtilityCommands/template.yaml rename to commands/ServerUtilityCommands/template.yaml diff --git a/lib/commands/UtilityCommands/index.js b/commands/UtilityCommands/index.js similarity index 98% rename from lib/commands/UtilityCommands/index.js rename to commands/UtilityCommands/index.js index 5071ad6..be9e9ff 100644 --- a/lib/commands/UtilityCommands/index.js +++ b/commands/UtilityCommands/index.js @@ -1,4 +1,4 @@ -const cmdLib = require('../../CommandLib'); +const cmdLib = require('../../lib/CommandLib'); /** * Utility commands are all commands that allow the user to control the behaviour of the diff --git a/lib/commands/UtilityCommands/template.yaml b/commands/UtilityCommands/template.yaml similarity index 100% rename from lib/commands/UtilityCommands/template.yaml rename to commands/UtilityCommands/template.yaml diff --git a/lib/CommandLib.js b/lib/CommandLib/index.js similarity index 88% rename from lib/CommandLib.js rename to lib/CommandLib/index.js index a14b1a3..c7dcd0b 100644 --- a/lib/CommandLib.js +++ b/lib/CommandLib/index.js @@ -1,9 +1,9 @@ const Discord = require('discord.js'), yaml = require('js-yaml'), fsx = require('fs-extra'), - logging = require('./logging'), - config = require('../config.json'), - utils = require('./utils'); + logging = require('../utils/logging'), + config = require('../../config.json'), + utils = require('../utils'); const scopes = { 'Global': 0, @@ -239,13 +239,16 @@ class ExtendedRichEmbed extends Discord.RichEmbed { * @param value */ setDescription(value) { - let croppedValue = value; - if (value.substring) - croppedValue = value.substring(0, 1024); - if (croppedValue.length < value.length) - croppedValue = croppedValue.replace(/\n.*$/g, ''); - if (croppedValue && croppedValue.replace(/\n/g, '').length > 0) - super.setDescription(croppedValue); + if (value) { + let croppedValue = value; + if (value.substring) + croppedValue = value.substring(0, 1024); + if (croppedValue.length < value.length && croppedValue.replace) + croppedValue = croppedValue.replace(/\n.*$/g, ''); + if (croppedValue && croppedValue.replace + && croppedValue.replace(/\n/g, '').length > 0) + super.setDescription(croppedValue); + } return this; } @@ -255,15 +258,16 @@ class ExtendedRichEmbed extends Discord.RichEmbed { * @param value */ addField(name, value) { - let croppedValue = value; - if (value.substring) - croppedValue = value.substring(0, 1024); - if (croppedValue.length < value.length) + if (name && value) { + let croppedValue = value; + if (value.substring) + croppedValue = value.substring(0, 1024); + if (croppedValue && croppedValue.length < value.length && croppedValue.replace) croppedValue = croppedValue.replace(/\n.*$/g, ''); - if (name && croppedValue - && croppedValue.replace(/\n/g, '').length > 0 && name.replace(/\n/g, '').length > 0) - super.addField(name, croppedValue); - + if (croppedValue && croppedValue.replace + && croppedValue.replace(/\n/g, '').length > 0 && name.replace(/\n/g, '').length > 0) + super.addField(name, croppedValue); + } return this; } } diff --git a/lib/MessageLib.js b/lib/MessageLib/index.js similarity index 98% rename from lib/MessageLib.js rename to lib/MessageLib/index.js index 3642b43..dd477e3 100644 --- a/lib/MessageLib.js +++ b/lib/MessageLib/index.js @@ -1,7 +1,7 @@ -const cmdLib = require('./CommandLib'), - config = require('../config.json'), +const cmdLib = require('../CommandLib'), + config = require('../../config.json'), Discord = require('discord.js'), - logging = require('./logging'), + logging = require('../utils/logging'), promiseWaterfall = require('promise-waterfall'); /* eslint no-useless-escape: 0 */ diff --git a/lib/MusicLib.js b/lib/MusicLib/index.js similarity index 98% rename from lib/MusicLib.js rename to lib/MusicLib/index.js index d6558da..4090f58 100644 --- a/lib/MusicLib.js +++ b/lib/MusicLib/index.js @@ -1,9 +1,9 @@ const ytdl = require("ytdl-core"), ypi = require('youtube-playlist-info'), yttl = require('get-youtube-title'), - config = require('../config.json'), - utils = require('./utils.js'), - logging = require('./logging'), + config = require('../../config.json'), + utils = require('../utils/index.js'), + logging = require('../utils/logging'), ytapiKey = config.api.youTubeApiKey; /** diff --git a/lib/StateLib/index.js b/lib/StateLib/index.js new file mode 100644 index 0000000..b16339c --- /dev/null +++ b/lib/StateLib/index.js @@ -0,0 +1,26 @@ + +class EventRouter { + + constructor() { + + } + + /** + * Dispatches + * @param event + */ + dispatchEvent(event) { + + } + + /** + * Registeres discord client events to the EventRouter + * @param client + */ + registerClientEvents(client) { + + } + +} + +class EventGroup diff --git a/lib/WebLib.js b/lib/WebLib/index.js similarity index 98% rename from lib/WebLib.js rename to lib/WebLib/index.js index 6bbcc96..c774637 100644 --- a/lib/WebLib.js +++ b/lib/WebLib/index.js @@ -4,21 +4,21 @@ const express = require('express'), compression = require('compression'), md5 = require('js-md5'), sha512 = require('js-sha512'), - logging = require('./logging'), + logging = require('../utils/logging'), fs = require('fs'), session = require('express-session'), SQLiteStore = require('connect-sqlite3')(session), bodyParser = require('body-parser'), compileSass = require('express-compile-sass'), - config = require('../config.json'), - utils = require('../lib/utils'); + config = require('../../config.json'), + utils = require('../utils'); exports.WebServer = class { constructor(port) { this.app = express(); this.server = null; this.port = port; - this.schema = buildSchema(fs.readFileSync('./lib/api/graphql/schema.gql', 'utf-8')); + this.schema = buildSchema(fs.readFileSync('./web/api/graphql/schema.gql', 'utf-8')); this.root = {}; this._logger = new logging.Logger(this); } diff --git a/lib/api/graphql/AnilistApi/AnimeQuery.gql b/lib/api/AniListApi/graphql/AnimeQuery.gql similarity index 100% rename from lib/api/graphql/AnilistApi/AnimeQuery.gql rename to lib/api/AniListApi/graphql/AnimeQuery.gql diff --git a/lib/api/graphql/AnilistApi/CharacterQuery.gql b/lib/api/AniListApi/graphql/CharacterQuery.gql similarity index 100% rename from lib/api/graphql/AnilistApi/CharacterQuery.gql rename to lib/api/AniListApi/graphql/CharacterQuery.gql diff --git a/lib/api/graphql/AnilistApi/Fragments.yaml b/lib/api/AniListApi/graphql/Fragments.yaml similarity index 100% rename from lib/api/graphql/AnilistApi/Fragments.yaml rename to lib/api/AniListApi/graphql/Fragments.yaml diff --git a/lib/api/graphql/AnilistApi/MangaQuery.gql b/lib/api/AniListApi/graphql/MangaQuery.gql similarity index 100% rename from lib/api/graphql/AnilistApi/MangaQuery.gql rename to lib/api/AniListApi/graphql/MangaQuery.gql diff --git a/lib/api/graphql/AnilistApi/StaffQuery.gql b/lib/api/AniListApi/graphql/StaffQuery.gql similarity index 100% rename from lib/api/graphql/AnilistApi/StaffQuery.gql rename to lib/api/AniListApi/graphql/StaffQuery.gql diff --git a/lib/api/AnilistApi.js b/lib/api/AniListApi/index.js similarity index 100% rename from lib/api/AnilistApi.js rename to lib/api/AniListApi/index.js diff --git a/lib/guilding.js b/lib/guilding.js index 63d03b3..1e3a399 100644 --- a/lib/guilding.js +++ b/lib/guilding.js @@ -1,8 +1,8 @@ const music = require('./MusicLib'), utils = require('./utils'), config = require('../config.json'), - sqliteAsync = require('./sqliteAsync'), - logging = require('./logging'), + sqliteAsync = require('./utils/sqliteAsync'), + logging = require('./utils/logging'), fs = require('fs-extra'), dataDir = config.dataPath || './data'; diff --git a/lib/utils.js b/lib/utils/index.js similarity index 100% rename from lib/utils.js rename to lib/utils/index.js diff --git a/lib/logging.js b/lib/utils/logging.js similarity index 100% rename from lib/logging.js rename to lib/utils/logging.js diff --git a/lib/sqliteAsync.js b/lib/utils/sqliteAsync.js similarity index 100% rename from lib/sqliteAsync.js rename to lib/utils/sqliteAsync.js diff --git a/test/test.js b/test/test.js index 5eb5b33..2ac6160 100644 --- a/test/test.js +++ b/test/test.js @@ -13,7 +13,7 @@ mockobjects.mockLogger = { }; describe('lib/utils', function() { - const utils = require('../lib/utils.js'); + const utils = require('../lib/utils/index.js'); describe('#getSplitDuration', function() { it('returns an object from milliseconds', function() { @@ -28,7 +28,7 @@ describe('lib/utils', function() { it('returns the correct extension for a filename', function(done) { assert(utils.getExtension('test.txt') === '.txt'); assert(utils.getExtension('test.tar.gz') === '.gz'); - assert(utils.getExtension('../lib/utils.js') === '.js'); + assert(utils.getExtension('../lib/index.js') === '.js'); assert(utils.getExtension('.gitignore') === '.gitignore'); done(); }); diff --git a/lib/api/graphql/schema.gql b/web/api/graphql/schema.gql similarity index 100% rename from lib/api/graphql/schema.gql rename to web/api/graphql/schema.gql