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

97 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2019-06-26 02:52:05 +02:00
const { Command } = require('discord-akairo');
const fetch = require('node-fetch');
const boards = require('4chan-boards');
2019-12-24 19:55:38 +01:00
const Turndown = require('turndown');
let turndown = new Turndown();
2019-06-26 02:52:05 +02:00
class FourchanCommand extends Command {
constructor() {
super('4chan', {
aliases: ['4chan'],
2019-11-09 12:04:01 +01:00
clientPermissions: ['EMBED_LINKS', 'SEND_MESSAGES'],
2019-06-26 02:52:05 +02:00
category: 'fun',
args: [
{
id: 'board',
type: 'string',
prompt: {
2020-06-15 17:56:51 +02:00
start: 'Which board do you want to browse?',
2019-06-26 02:52:05 +02:00
},
match: 'rest'
}
],
description: {
2020-06-15 17:56:51 +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) {
2020-06-15 17:56:51 +02:00
if (boards.getType(args.board) === boards.NSFW && !message.channel.nsfw) return message.channel.send('Sorry, this board only works in nsfw channels!');
2019-06-26 02:52:05 +02:00
if (!args.board) return;
2019-12-24 21:27:46 +01:00
args.board = args.board.replace(/\//g, '');
2019-06-26 02:52:05 +02:00
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)
2020-06-15 17:56:51 +02:00
return message.channel.send('Not a valid board! Try again!');
2019-06-26 02:52:05 +02:00
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-12-24 19:09:06 +01:00
// Loop until it found a threads
while(!response.threads[i]) {
i = Math.floor((Math.random() * response.threads.length) + 1);
}
2019-06-26 15:09:29 +02:00
// If post is sticky search again
2019-07-09 05:26:17 +02:00
while(response.threads[i].posts[0].sticky == 1 || !response.threads[i].posts) {
2019-06-26 04:32:56 +02:00
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-12-24 19:02:28 +01:00
let boardName = boards.getName(args.board);
if (boardName == undefined) boardName = args.board;
2019-06-26 04:46:56 +02:00
2019-07-09 05:15:04 +02:00
// If title or description is undefined, change it to "no title/description"
if (!description) {
description = 'No description';
2019-06-26 04:46:56 +02:00
}
2019-06-26 15:09:29 +02:00
if (!title) {
title = 'No title';
}
2019-11-22 12:30:20 +01:00
const FourchanEmbed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
2019-12-24 19:55:38 +01:00
.setTitle(turndown.turndown(title))
.setDescription(turndown.turndown(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-12-24 19:02:28 +01:00
.setFooter(`${boardName} | ${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-12-24 19:02:28 +01:00
})
.catch((err) => {
2020-06-15 17:56:51 +02:00
if (err.type == 'invalid-json') return message.channel.send('Could not find the board! Try again!');
2019-12-24 19:02:28 +01:00
console.error(err);
return message.channel.send('Uh-oh, an error has occurred! Try again! If this keeps happening, tell the developers!');
2019-12-24 19:02:28 +01:00
});
2019-06-26 02:52:05 +02:00
}
}
module.exports = FourchanCommand;