From 49055ca2a0acd011f34eaea1aded5716379b3abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bersier?= Date: Wed, 26 Jun 2019 15:09:29 +0200 Subject: [PATCH] fixed "undefined" --- commands/fun/4chan.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/commands/fun/4chan.js b/commands/fun/4chan.js index c6adeb76..a8b81dd3 100644 --- a/commands/fun/4chan.js +++ b/commands/fun/4chan.js @@ -40,26 +40,39 @@ class FourchanCommand extends Command { let i = Math.floor((Math.random() * response.threads.length)); + // If post is sticky search again while(response.threads[i].posts[0].sticky == 1) { i = Math.floor((Math.random() * response.threads.length)); } + let title = response.threads[i].posts[0].sub; let description = response.threads[i].posts[0].com; - description = decodeURI(description); + // 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 (!title) { + title = 'No title'; + } + + if (!description) { + description = 'No description'; + } + const FourchanEmbed = new MessageEmbed() .setColor('#ff9900') - .setTitle(response.threads[i].posts[0].sub) + .setTitle(title) .setDescription(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}`); + // If file type dosen't work on embed, send it as a link if (response.threads[i].posts[0].ext == '.webm' || response.threads[i].posts[0].ext == '.pdf' || response.threads[i].posts[0].ext == '.swf') { message.channel.send(FourchanEmbed); message.channel.send(`https://i.4cdn.org/${args.board}/${response.threads[i].posts[0].tim}${response.threads[i].posts[0].ext}`);