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
feature/api-rewrite
Trivernis 6 years ago
parent 2a2386e545
commit 540ab231db

@ -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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [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 ### Changed
- name of MiscCommands module from `TemplateCommandModule` to `MiscoCommandModule` - 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 ## [0.11.0-beta] - 2019-03-03
### Changed ### Changed

@ -1,12 +1,12 @@
const Discord = require("discord.js"), const Discord = require("discord.js"),
fs = require('fs-extra'), fs = require('fs-extra'),
logging = require('./lib/logging'), logging = require('./lib/utils/logging'),
msgLib = require('./lib/MessageLib'), msgLib = require('./lib/MessageLib'),
guilding = require('./lib/guilding'), guilding = require('./lib/guilding'),
utils = require('./lib/utils'), utils = require('./lib/utils'),
config = require('./config.json'), config = require('./config.json'),
args = require('args-parser')(process.argv), args = require('args-parser')(process.argv),
sqliteAsync = require('./lib/sqliteAsync'), sqliteAsync = require('./lib/utils/sqliteAsync'),
authToken = args.token || config.api.botToken, authToken = args.token || config.api.botToken,
prefix = args.prefix || config.prefix || '~', prefix = args.prefix || config.prefix || '~',
gamepresence = args.game || config.presence; gamepresence = args.game || config.presence;
@ -69,24 +69,24 @@ class Bot {
if (config.webinterface && config.webinterface.enabled) if (config.webinterface && config.webinterface.enabled)
await this.initializeWebserver(); await this.initializeWebserver();
this.logger.verbose('Registering commands'); this.logger.verbose('Registering commands');
await this.messageHandler.registerCommandModule(require('./lib/commands/AnilistApiCommands').module, {}); await this.messageHandler.registerCommandModule(require('./commands/AnilistApiCommands').module, {});
await this.messageHandler.registerCommandModule(require('./lib/commands/UtilityCommands').module, { await this.messageHandler.registerCommandModule(require('./commands/UtilityCommands').module, {
bot: this, bot: this,
config: config config: config
}); });
await this.messageHandler.registerCommandModule(require('./lib/commands/InfoCommands').module, { await this.messageHandler.registerCommandModule(require('./commands/InfoCommands').module, {
client: this.client, client: this.client,
messageHandler: this.messageHandler 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) 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), getGuildHandler: async (g) => await this.getGuildHandler(g),
messageHandler: this.messageHandler, messageHandler: this.messageHandler,
config: config config: config
}); });
await this.messageHandler.registerCommandModule(require('./lib/commands/MiscCommands').module, {}); await this.messageHandler.registerCommandModule(require('./commands/MiscCommands').module, {});
this.registerEvents(); this.registerEvents();
} }

@ -1,5 +1,5 @@
/* template index.js. Doesn't implement actual commands */ /* 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 * A description what the command module includes and why. Doesn't need to list commands but explains

@ -1,5 +1,5 @@
const cmdLib = require('../../CommandLib'), const cmdLib = require('../../lib/CommandLib'),
anilistApi = require('../../api/AnilistApi'); anilistApi = require('../../lib/api/AniListApi');
/** /**
* The AniList commands are all commands that interact with the anilist api. * The AniList commands are all commands that interact with the anilist api.
@ -144,7 +144,7 @@ class RichCharacterInfo extends cmdLib.ExtendedRichEmbed {
.replace(/~!.*?!~/g, '') .replace(/~!.*?!~/g, '')
.replace(/\n\n\n/g, '')); .replace(/\n\n\n/g, ''));
if (characterInfo.media && characterInfo.media.edges) if (characterInfo.media && characterInfo.media.edges)
this.addNonemptyField( this.addField(
'Media Appeareance', 'Media Appeareance',
characterInfo.media.edges.map(x => { characterInfo.media.edges.map(x => {
let media = x.node; let media = x.node;

@ -1,6 +1,6 @@
const cmdLib = require('../../CommandLib'), const cmdLib = require('../../lib/CommandLib'),
fsx = require('fs-extra'), fsx = require('fs-extra'),
utils = require('../../utils'); utils = require('../../lib/utils');
/** /**
* Info commands provide information about the bot. These informations are * Info commands provide information about the bot. These informations are

@ -1,5 +1,5 @@
/* template index.js. Doesn't implement actual commands */ /* 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. * Several commands that are that special that they can't be included in any other module.

@ -1,6 +1,6 @@
const cmdLib = require('../../CommandLib'), const cmdLib = require('../../lib/CommandLib'),
utils = require('../../utils'), utils = require('../../lib/utils'),
config = require('../../../config'); config = require('../../config');
function checkPermission(msg, rolePerm) { function checkPermission(msg, rolePerm) {
if (!rolePerm || ['all', 'any', 'everyone'].includes(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; let vc = gh.musicPlayer.voiceChannel || m.member.voiceChannel;
if (gh.musicPlayer.connected && vc) { if (gh.musicPlayer.connected && vc) {
let votes = gh.updateCommandVote(stop.name, m.author.tag); 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')) { if (neededVotes <= votes.count || checkPermission(m, 'dj')) {
this._logger.debug(`Vote passed. ${votes.count} out of ${neededVotes} for stop or permission granted`); 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; let vc = gh.musicPlayer.voiceChannel || m.member.voiceChannel;
if (gh.musicPlayer.playing && vc) { if (gh.musicPlayer.playing && vc) {
let votes = gh.updateCommandVote(skip.name, m.author.tag); 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')) { if (neededVotes <= votes.count || checkPermission(m, 'dj')) {
this._logger.debug(`Vote passed. ${votes.count} out of ${neededVotes} for skip or permission granted`); this._logger.debug(`Vote passed. ${votes.count} out of ${neededVotes} for skip or permission granted`);

@ -1,4 +1,4 @@
const cmdLib = require('../../CommandLib'); const cmdLib = require('../../lib/CommandLib');
/** /**
* This command module includes utility commands for the server. * This command module includes utility commands for the server.

@ -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 * Utility commands are all commands that allow the user to control the behaviour of the

@ -1,9 +1,9 @@
const Discord = require('discord.js'), const Discord = require('discord.js'),
yaml = require('js-yaml'), yaml = require('js-yaml'),
fsx = require('fs-extra'), fsx = require('fs-extra'),
logging = require('./logging'), logging = require('../utils/logging'),
config = require('../config.json'), config = require('../../config.json'),
utils = require('./utils'); utils = require('../utils');
const scopes = { const scopes = {
'Global': 0, 'Global': 0,
@ -239,13 +239,16 @@ class ExtendedRichEmbed extends Discord.RichEmbed {
* @param value * @param value
*/ */
setDescription(value) { setDescription(value) {
let croppedValue = value; if (value) {
if (value.substring) let croppedValue = value;
croppedValue = value.substring(0, 1024); if (value.substring)
if (croppedValue.length < value.length) croppedValue = value.substring(0, 1024);
croppedValue = croppedValue.replace(/\n.*$/g, ''); if (croppedValue.length < value.length && croppedValue.replace)
if (croppedValue && croppedValue.replace(/\n/g, '').length > 0) croppedValue = croppedValue.replace(/\n.*$/g, '');
super.setDescription(croppedValue); if (croppedValue && croppedValue.replace
&& croppedValue.replace(/\n/g, '').length > 0)
super.setDescription(croppedValue);
}
return this; return this;
} }
@ -255,15 +258,16 @@ class ExtendedRichEmbed extends Discord.RichEmbed {
* @param value * @param value
*/ */
addField(name, value) { addField(name, value) {
let croppedValue = value; if (name && value) {
if (value.substring) let croppedValue = value;
croppedValue = value.substring(0, 1024); if (value.substring)
if (croppedValue.length < value.length) croppedValue = value.substring(0, 1024);
if (croppedValue && croppedValue.length < value.length && croppedValue.replace)
croppedValue = croppedValue.replace(/\n.*$/g, ''); croppedValue = croppedValue.replace(/\n.*$/g, '');
if (name && croppedValue if (croppedValue && croppedValue.replace
&& croppedValue.replace(/\n/g, '').length > 0 && name.replace(/\n/g, '').length > 0) && croppedValue.replace(/\n/g, '').length > 0 && name.replace(/\n/g, '').length > 0)
super.addField(name, croppedValue); super.addField(name, croppedValue);
}
return this; return this;
} }
} }

@ -1,7 +1,7 @@
const cmdLib = require('./CommandLib'), const cmdLib = require('../CommandLib'),
config = require('../config.json'), config = require('../../config.json'),
Discord = require('discord.js'), Discord = require('discord.js'),
logging = require('./logging'), logging = require('../utils/logging'),
promiseWaterfall = require('promise-waterfall'); promiseWaterfall = require('promise-waterfall');
/* eslint no-useless-escape: 0 */ /* eslint no-useless-escape: 0 */

@ -1,9 +1,9 @@
const ytdl = require("ytdl-core"), const ytdl = require("ytdl-core"),
ypi = require('youtube-playlist-info'), ypi = require('youtube-playlist-info'),
yttl = require('get-youtube-title'), yttl = require('get-youtube-title'),
config = require('../config.json'), config = require('../../config.json'),
utils = require('./utils.js'), utils = require('../utils/index.js'),
logging = require('./logging'), logging = require('../utils/logging'),
ytapiKey = config.api.youTubeApiKey; ytapiKey = config.api.youTubeApiKey;
/** /**

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

@ -4,21 +4,21 @@ const express = require('express'),
compression = require('compression'), compression = require('compression'),
md5 = require('js-md5'), md5 = require('js-md5'),
sha512 = require('js-sha512'), sha512 = require('js-sha512'),
logging = require('./logging'), logging = require('../utils/logging'),
fs = require('fs'), fs = require('fs'),
session = require('express-session'), session = require('express-session'),
SQLiteStore = require('connect-sqlite3')(session), SQLiteStore = require('connect-sqlite3')(session),
bodyParser = require('body-parser'), bodyParser = require('body-parser'),
compileSass = require('express-compile-sass'), compileSass = require('express-compile-sass'),
config = require('../config.json'), config = require('../../config.json'),
utils = require('../lib/utils'); utils = require('../utils');
exports.WebServer = class { exports.WebServer = class {
constructor(port) { constructor(port) {
this.app = express(); this.app = express();
this.server = null; this.server = null;
this.port = port; 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.root = {};
this._logger = new logging.Logger(this); this._logger = new logging.Logger(this);
} }

@ -1,8 +1,8 @@
const music = require('./MusicLib'), const music = require('./MusicLib'),
utils = require('./utils'), utils = require('./utils'),
config = require('../config.json'), config = require('../config.json'),
sqliteAsync = require('./sqliteAsync'), sqliteAsync = require('./utils/sqliteAsync'),
logging = require('./logging'), logging = require('./utils/logging'),
fs = require('fs-extra'), fs = require('fs-extra'),
dataDir = config.dataPath || './data'; dataDir = config.dataPath || './data';

@ -13,7 +13,7 @@ mockobjects.mockLogger = {
}; };
describe('lib/utils', function() { describe('lib/utils', function() {
const utils = require('../lib/utils.js'); const utils = require('../lib/utils/index.js');
describe('#getSplitDuration', function() { describe('#getSplitDuration', function() {
it('returns an object from milliseconds', 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) { it('returns the correct extension for a filename', function(done) {
assert(utils.getExtension('test.txt') === '.txt'); assert(utils.getExtension('test.txt') === '.txt');
assert(utils.getExtension('test.tar.gz') === '.gz'); 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'); assert(utils.getExtension('.gitignore') === '.gitignore');
done(); done();
}); });

Loading…
Cancel
Save