From 6ec6e59d8d82e508cbda701f9f6bde49407c3c3c Mon Sep 17 00:00:00 2001 From: loicbersier Date: Tue, 9 Jul 2019 05:15:04 +0200 Subject: [PATCH] cleaner output --- commands/fun/4chan.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/commands/fun/4chan.js b/commands/fun/4chan.js index 2b610cb8..8e13e0ac 100644 --- a/commands/fun/4chan.js +++ b/commands/fun/4chan.js @@ -2,6 +2,7 @@ const { Command } = require('discord-akairo'); const { MessageEmbed } = require('discord.js'); const fetch = require('node-fetch'); const boards = require('4chan-boards'); +const htmlToText = require('html-to-text'); class FourchanCommand extends Command { constructor() { @@ -44,33 +45,26 @@ class FourchanCommand extends Command { i = Math.floor((Math.random() * response.threads.length) + 1); // If post is sticky search again - while(response.threads[i].posts[0].sticky == 1) { + while(response.threads[i].posts[0].sticky == 1 || !response.threads[i].posts[0]) { i = Math.floor((Math.random() * response.threads.length)); } let title = response.threads[i].posts[0].sub; let description = response.threads[i].posts[0].com; - // Remove HTML element and encoding - description = decodeURI(description); - let regex = /(<([^>]+)>)/ig; - if (regex.test(description)) { - description = response.threads[i].posts[0].com.replace(/(<([^>]+)>)/ig,''); + // If title or description is undefined, change it to "no title/description" + if (!description) { + description = 'No description'; } - // If title or description is undefined, change it to "no title/description" if (!title) { title = 'No title'; } - if (!description) { - description = 'No description'; - } - const FourchanEmbed = new MessageEmbed() .setColor('#ff9900') .setTitle(title) - .setDescription(description) + .setDescription(htmlToText.fromString(description)) .setImage(`https://i.4cdn.org/${args.board}/${response.threads[i].posts[0].tim}${response.threads[i].posts[0].ext}`) .setURL(`https://boards.4chan.org/${args.board}/thread/${response.threads[i].posts[0].no}/${response.threads[i].posts[0].semantic_url}`) .setFooter(`${boards.getName(args.board)} | ${response.threads[i].posts[0].name} | ${response.threads[i].posts[0].no} | ${response.threads[i].posts[0].now}`);