From 1938b657867e1ddb91c0eb8ea15aa9d25f1735b7 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Tue, 22 Aug 2017 21:01:08 +0000 Subject: [PATCH] shorten in Util --- commands/search/anime.js | 6 +++--- commands/search/bulbapedia.js | 3 ++- commands/search/manga.js | 6 +++--- commands/search/movie.js | 3 ++- commands/search/npm.js | 3 ++- commands/search/tv-show.js | 3 ++- commands/search/urban.js | 5 +++-- commands/search/vocaloid.js | 3 ++- commands/search/wattpad.js | 3 ++- commands/search/wikia.js | 3 ++- commands/search/wikipedia.js | 3 ++- commands/util/upvote.js | 6 +----- package.json | 2 +- structures/Util.js | 4 ++++ 14 files changed, 31 insertions(+), 22 deletions(-) diff --git a/commands/search/anime.js b/commands/search/anime.js index 9165f737..5d63ae06 100644 --- a/commands/search/anime.js +++ b/commands/search/anime.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); -const { cleanXML } = require('../../structures/Util'); +const { cleanXML, shorten } = require('../../structures/Util'); const { promisify } = require('util'); const xml = promisify(require('xml2js').parseString); const { ANIMELIST_LOGIN } = process.env; @@ -31,14 +31,14 @@ module.exports = class AnimeCommand extends Command { .get(`https://${ANIMELIST_LOGIN}@myanimelist.net/api/anime/search.xml`) .query({ q: query }); const { anime } = await xml(text); - const synopsis = cleanXML(anime.entry[0].synopsis[0].substr(0, 2048)); + const synopsis = cleanXML(anime.entry[0].synopsis[0]); const embed = new MessageEmbed() .setColor(0x2D54A2) .setAuthor('My Anime List', 'https://i.imgur.com/R4bmNFz.png') .setURL(`https://myanimelist.net/anime/${anime.entry[0].id[0]}`) .setThumbnail(anime.entry[0].image[0]) .setTitle(`${anime.entry[0].title[0]} (English: ${anime.entry[0].english[0] || 'N/A'})`) - .setDescription(synopsis) + .setDescription(shorten(synopsis)) .addField('❯ Type', `${anime.entry[0].type[0]} - ${anime.entry[0].status[0]}`, true) .addField('❯ Episodes', diff --git a/commands/search/bulbapedia.js b/commands/search/bulbapedia.js index 2836e6a7..e21b9834 100644 --- a/commands/search/bulbapedia.js +++ b/commands/search/bulbapedia.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const { shorten } = require('../../structures/Util'); module.exports = class BulbapediaCommand extends Command { constructor(client) { @@ -40,7 +41,7 @@ module.exports = class BulbapediaCommand extends Command { .setColor(0x3E7614) .setTitle(body.query.pages[0].title) .setAuthor('Bulbapedia', 'https://i.imgur.com/09eYo5T.png') - .setDescription(body.query.pages[0].extract.replace(/\n/g, '\n\n').substr(0, 2048)); + .setDescription(shorten(body.query.pages[0].extract.replace(/\n/g, '\n\n'))); return msg.embed(embed); } }; diff --git a/commands/search/manga.js b/commands/search/manga.js index 2e43e6ea..c39e8d68 100644 --- a/commands/search/manga.js +++ b/commands/search/manga.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); -const { cleanXML } = require('../../structures/Util'); +const { cleanXML, shorten } = require('../../structures/Util'); const { promisify } = require('util'); const xml = promisify(require('xml2js').parseString); const { ANIMELIST_LOGIN } = process.env; @@ -31,14 +31,14 @@ module.exports = class MangaCommand extends Command { .get(`https://${ANIMELIST_LOGIN}@myanimelist.net/api/manga/search.xml`) .query({ q: query }); const { manga } = await xml(text); - const synopsis = cleanXML(manga.entry[0].synopsis[0].substr(0, 2048)); + const synopsis = cleanXML(manga.entry[0].synopsis[0]); const embed = new MessageEmbed() .setColor(0x2D54A2) .setAuthor('My Anime List', 'https://i.imgur.com/R4bmNFz.png') .setURL(`https://myanimelist.net/manga/${manga.entry[0].id[0]}`) .setThumbnail(manga.entry[0].image[0]) .setTitle(`${manga.entry[0].title[0]} (English: ${manga.entry[0].english[0] || 'N/A'})`) - .setDescription(synopsis) + .setDescription(shorten(synopsis)) .addField('❯ Type', `${manga.entry[0].type[0]} - ${manga.entry[0].status[0]}`, true) .addField('❯ Volumes / Chapters', diff --git a/commands/search/movie.js b/commands/search/movie.js index ef5c4915..11406981 100644 --- a/commands/search/movie.js +++ b/commands/search/movie.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const { shorten } = require('../../structures/Util'); const { TMDB_KEY } = process.env; module.exports = class MovieCommand extends Command { @@ -40,7 +41,7 @@ module.exports = class MovieCommand extends Command { .setTitle(body.title) .setURL(`https://www.themoviedb.org/movie/${body.id}`) .setAuthor('TMDB', 'https://i.imgur.com/G9q4DF1.png') - .setDescription(body.overview ? body.overview.substr(0, 2048) : 'No description available.') + .setDescription(body.overview ? shorten(body.overview) : 'No description available.') .setThumbnail(body.poster_path ? `https://image.tmdb.org/t/p/w500${body.poster_path}` : null) .addField('❯ Runtime', body.runtime ? `${body.runtime} mins.` : 'N/A', true) diff --git a/commands/search/npm.js b/commands/search/npm.js index d2c1f21b..3988a753 100644 --- a/commands/search/npm.js +++ b/commands/search/npm.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); const moment = require('moment'); +const { shorten } = require('../../structures/Util'); module.exports = class NPMCommand extends Command { constructor(client) { @@ -45,7 +46,7 @@ module.exports = class NPMCommand extends Command { .addField('❯ Main File', body.versions[body['dist-tags'].latest].main, true) .addField('❯ Keywords', - body.keywords && body.keywords.length ? body.keywords.join(', ').substr(0, 1024) : 'None') + body.keywords && body.keywords.length ? shorten(body.keywords.join(', '), 1000) : 'None') .addField('❯ Maintainers', body.maintainers.map(user => user.name).join(', ')); return msg.embed(embed); diff --git a/commands/search/tv-show.js b/commands/search/tv-show.js index b72b45a1..44e1095d 100644 --- a/commands/search/tv-show.js +++ b/commands/search/tv-show.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const { shorten } = require('../../structures/Util'); const { TMDB_KEY } = process.env; module.exports = class TVShowCommand extends Command { @@ -40,7 +41,7 @@ module.exports = class TVShowCommand extends Command { .setTitle(body.name) .setURL(`https://www.themoviedb.org/tv/${body.id}`) .setAuthor('TMDB', 'https://i.imgur.com/G9q4DF1.png') - .setDescription(body.overview ? body.overview.substr(0, 2048) : 'No description available.') + .setDescription(body.overview ? shorten(body.overview) : 'No description available.') .setThumbnail(body.poster_path ? `https://image.tmdb.org/t/p/w500${body.poster_path}` : null) .addField('❯ First Air Date', body.first_air_date || 'N/A', true) diff --git a/commands/search/urban.js b/commands/search/urban.js index 6670f8e1..fe2f886e 100644 --- a/commands/search/urban.js +++ b/commands/search/urban.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const { shorten } = require('../../structures/Util'); module.exports = class UrbanCommand extends Command { constructor(client) { @@ -31,9 +32,9 @@ module.exports = class UrbanCommand extends Command { .setAuthor('Urban Dictionary', 'https://i.imgur.com/fzFuuL7.png') .setURL(body.list[0].permalink) .setTitle(body.list[0].word) - .setDescription(body.list[0].definition.substr(0, 2048)) + .setDescription(shorten(body.list[0].definition)) .addField('❯ Example', - body.list[0].example.substr(0, 1024) || 'None'); + shorten(body.list[0].example, 1000) || 'None'); return msg.embed(embed); } }; diff --git a/commands/search/vocaloid.js b/commands/search/vocaloid.js index da1bb5aa..11a06ad3 100644 --- a/commands/search/vocaloid.js +++ b/commands/search/vocaloid.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); const moment = require('moment'); +const { shorten } = require('../../structures/Util'); module.exports = class VocaloidCommand extends Command { constructor(client) { @@ -40,7 +41,7 @@ module.exports = class VocaloidCommand extends Command { .setAuthor('VocaDB', 'https://i.imgur.com/9Tx9UIc.jpg') .setTitle(body.items[0].name) .setURL(`http://vocadb.net/S/${body.items[0].id}`) - .setDescription(body.items[0].lyrics[0] ? body.items[0].lyrics[0].value.substr(0, 2048) : 'No lyrics available.') + .setDescription(body.items[0].lyrics.length ? shorten(body.items[0].lyrics[0].value) : 'No lyrics available.') .setThumbnail(body.items[0].thumbUrl) .addField('❯ Artist', body.items[0].artistString) diff --git a/commands/search/wattpad.js b/commands/search/wattpad.js index 5b8e6a95..df220356 100644 --- a/commands/search/wattpad.js +++ b/commands/search/wattpad.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); const moment = require('moment'); +const { shorten } = require('../../structures/Util'); const { WATTPAD_KEY } = process.env; module.exports = class WattpadCommand extends Command { @@ -37,7 +38,7 @@ module.exports = class WattpadCommand extends Command { .setAuthor('Wattpad', 'https://i.imgur.com/Rw9vRQB.png') .setURL(body.stories[0].url) .setTitle(body.stories[0].title) - .setDescription(body.stories[0].description.substr(0, 2048)) + .setDescription(shorten(body.stories[0].description)) .setThumbnail(body.stories[0].cover) .addField('❯ Created On', moment(body.stories[0].createDate).format('MMMM Do YYYY'), true) diff --git a/commands/search/wikia.js b/commands/search/wikia.js index 44072d12..86806e96 100644 --- a/commands/search/wikia.js +++ b/commands/search/wikia.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const { shorten } = require('../../structures/Util'); module.exports = class WikiaCommand extends Command { constructor(client) { @@ -44,7 +45,7 @@ module.exports = class WikiaCommand extends Command { .setTitle(body.sections[0].title) .setURL(search.body.items[0].url) .setAuthor('Wikia', 'https://i.imgur.com/WzXWJka.png') - .setDescription(body.sections[0].content.map(i => i.text).join('\n\n').substr(0, 2048)) + .setDescription(shorten(body.sections[0].content.map(section => section.text).join('\n\n'))) .setThumbnail(body.sections[0].images.length ? body.sections[0].images[0].src : null); return msg.embed(embed); } catch (err) { diff --git a/commands/search/wikipedia.js b/commands/search/wikipedia.js index 6d2728ba..67b85dd4 100644 --- a/commands/search/wikipedia.js +++ b/commands/search/wikipedia.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const { shorten } = require('../../structures/Util'); module.exports = class WikipediaCommand extends Command { constructor(client) { @@ -39,7 +40,7 @@ module.exports = class WikipediaCommand extends Command { .setColor(0xE7E7E7) .setTitle(body.query.pages[0].title) .setAuthor('Wikipedia', 'https://i.imgur.com/a4eeEhh.png') - .setDescription(body.query.pages[0].extract.replace(/\n/g, '\n\n').substr(0, 2048)); + .setDescription(shorten(body.query.pages[0].extract.replace(/\n/g, '\n\n'))); return msg.embed(embed); } }; diff --git a/commands/util/upvote.js b/commands/util/upvote.js index 094675f3..0fcd3616 100644 --- a/commands/util/upvote.js +++ b/commands/util/upvote.js @@ -19,13 +19,9 @@ module.exports = class UpvoteCommand extends Command { const { body } = await snekfetch .get(`https://discordbots.org/api/bots/${this.client.user.id}/votes`) .set({ Authorization: DBOTSORG_KEY }); - const haste = await snekfetch - .post('https://hastebin.com/documents') - .send(body.map(user => `${user.username}#${user.discriminator}`).join('\n')); return msg.say(stripIndents` Upvote Xiao and get rewards while joining ${body.length} others! - - List of Upvoters: + `); } }; diff --git a/package.json b/package.json index 6e17b130..22ce93a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "31.0.1", + "version": "31.0.2", "description": "Your personal server companion.", "main": "Shard.js", "scripts": { diff --git a/structures/Util.js b/structures/Util.js index 943788b7..cdd45d4a 100644 --- a/structures/Util.js +++ b/structures/Util.js @@ -75,6 +75,10 @@ class Util { static list(arr, conj = 'and') { return `${arr.slice(0, -1).join(', ')}${arr.length > 1 ? `${arr.length > 2 ? ',' : ''} ${conj} ` : ''}${arr.slice(-1)}`; // eslint-disable-line max-len } + + static shorten(text, maxLen = 2000) { + return text.length > maxLen ? `${text.substr(0, maxLen - 3)}...` : text; + } } module.exports = Util;