Haha-Yes/commands/fun/4chan.js

89 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-06-26 02:52:05 +02:00
const { Command } = require('discord-akairo');
const { MessageEmbed } = require('discord.js');
const fetch = require('node-fetch');
const boards = require('4chan-boards');
2019-06-26 02:52:05 +02:00
class FourchanCommand extends Command {
constructor() {
super('4chan', {
aliases: ['4chan'],
category: 'fun',
args: [
{
id: 'board',
type: 'string',
prompt: {
start: 'Wich board do you want to browse?',
},
match: 'rest'
}
],
description: {
2019-06-26 03:53:06 +02:00
content: 'Send random images from a 4chan board of your choosing',
2019-06-26 02:52:05 +02:00
usage: '[board]',
examples: ['vg']
}
});
}
async exec(message, args) {
if (boards.getType(args.board) === boards.NSFW && !message.channel.nsfw) return message.channel.send('Sorry, this board only work in nsfw channel!');
2019-06-26 02:52:05 +02:00
if (!args.board) return;
2019-06-26 15:15:26 +02:00
let i = Math.floor((Math.random() * 5) + 1);
fetch(`https://a.4cdn.org/${args.board}/${i}.json`).then((response) => {
2019-06-26 02:52:05 +02:00
return response.json();
}).then((response) => {
if (!response.threads)
return message.channel.send('Not a valid board');
2019-06-26 15:15:26 +02:00
i = Math.floor((Math.random() * response.threads.length) + 1);
2019-06-26 02:52:05 +02:00
2019-06-26 15:09:29 +02:00
// If post is sticky search again
2019-06-26 04:32:56 +02:00
while(response.threads[i].posts[0].sticky == 1) {
i = Math.floor((Math.random() * response.threads.length));
}
2019-06-26 15:09:29 +02:00
let title = response.threads[i].posts[0].sub;
2019-06-26 04:46:56 +02:00
let description = response.threads[i].posts[0].com;
2019-06-26 15:09:29 +02:00
// Remove HTML element and encoding
description = decodeURI(description);
2019-06-26 04:46:56 +02:00
let regex = /(<([^>]+)>)/ig;
if (regex.test(description)) {
description = response.threads[i].posts[0].com.replace(/(<([^>]+)>)/ig,'');
}
2019-06-26 15:09:29 +02:00
// If title or description is undefined, change it to "no title/description"
if (!title) {
title = 'No title';
}
if (!description) {
description = 'No description';
}
2019-06-26 02:52:05 +02:00
const FourchanEmbed = new MessageEmbed()
.setColor('#ff9900')
2019-06-26 15:09:29 +02:00
.setTitle(title)
2019-06-26 04:46:56 +02:00
.setDescription(description)
2019-06-26 02:52:05 +02:00
.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}`)
2019-06-26 04:46:56 +02:00
.setFooter(`${boards.getName(args.board)} | ${response.threads[i].posts[0].name} | ${response.threads[i].posts[0].no} | ${response.threads[i].posts[0].now}`);
2019-06-26 02:52:05 +02:00
2019-06-26 15:09:29 +02:00
// 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') {
2019-06-26 04:13:24 +02:00
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}`);
} else {
message.channel.send(FourchanEmbed);
}
2019-06-26 02:52:05 +02:00
});
}
}
module.exports = FourchanCommand;