fixed "undefined"

This commit is contained in:
Loïc Bersier 2019-06-26 15:09:29 +02:00
parent 4fb3d4dbdb
commit 49055ca2a0

View file

@ -40,26 +40,39 @@ class FourchanCommand extends Command {
let i = Math.floor((Math.random() * response.threads.length)); let i = Math.floor((Math.random() * response.threads.length));
// If post is sticky search again
while(response.threads[i].posts[0].sticky == 1) { while(response.threads[i].posts[0].sticky == 1) {
i = Math.floor((Math.random() * response.threads.length)); i = Math.floor((Math.random() * response.threads.length));
} }
let title = response.threads[i].posts[0].sub;
let description = response.threads[i].posts[0].com; let description = response.threads[i].posts[0].com;
description = decodeURI(description);
// Remove HTML element and encoding
description = decodeURI(description);
let regex = /(<([^>]+)>)/ig; let regex = /(<([^>]+)>)/ig;
if (regex.test(description)) { if (regex.test(description)) {
description = response.threads[i].posts[0].com.replace(/(<([^>]+)>)/ig,''); 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() const FourchanEmbed = new MessageEmbed()
.setColor('#ff9900') .setColor('#ff9900')
.setTitle(response.threads[i].posts[0].sub) .setTitle(title)
.setDescription(description) .setDescription(description)
.setImage(`https://i.4cdn.org/${args.board}/${response.threads[i].posts[0].tim}${response.threads[i].posts[0].ext}`) .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}`) .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}`); .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') { 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(FourchanEmbed);
message.channel.send(`https://i.4cdn.org/${args.board}/${response.threads[i].posts[0].tim}${response.threads[i].posts[0].ext}`); message.channel.send(`https://i.4cdn.org/${args.board}/${response.threads[i].posts[0].tim}${response.threads[i].posts[0].ext}`);