I'm making a meme mand that sends a random meme from the meme subreddit and after discovering everything except the actual image in embed was working I logged the image to console and it returned a 403
error. If anyone is able to help it would be greatly appreciated.
Code:
const https = require('https');
const Discord = require('discord.js');
const url = '/.json?limit=100'
module.exports = {
name: 'meme',
description: 'sends meme',
execute(message, args) {
https.get(url, (result) => {
var body = ''
result.on('data', (chunk) => {
body += chunk
})
result.on('end', () => {
var response = JSON.parse(body)
var index = response.data.children[Math.floor(Math.random() * 99) + 1].data
if (index.post_hint !== 'image') {
var text = index.selftext
const textembed = new Discord.MessageEmbed()
.setTitle(subRedditName)
.setColor(9384170)
.setDescription(`[${title}](${link})\n\n${text}`)
.setURL(`/${subRedditName}`)
message.channel.send(textembed)
}
var image = index.preview.images[0].source.url
var title = index.title
var link = '' + index.permalink
var subRedditName = index.subreddit_name_prefixed
if (index.post_hint !== 'image') {
const textembed = new Discord.RichEmbed()
.setTitle(subRedditName)
.setColor(9384170)
.setDescription(`[${title}](${link})\n\n${text}`)
.setURL(`/${subRedditName}`)
message.channel.send(textembed)
}
console.log(image);
const imageembed = new Discord.MessageEmbed()
.setTitle(subRedditName)
.setImage(image)
.setColor(9384170)
.setDescription(`[${title}](${link})`)
.setURL(`/${subRedditName}`)
message.channel.send(imageembed)
}).on('error', function (e) {
console.log('Got an error: ', e)
})
})
},
}
I'm making a meme mand that sends a random meme from the meme subreddit and after discovering everything except the actual image in embed was working I logged the image to console and it returned a 403
error. If anyone is able to help it would be greatly appreciated.
Code:
const https = require('https');
const Discord = require('discord.js');
const url = 'https://www.reddit./r/meme/hot/.json?limit=100'
module.exports = {
name: 'meme',
description: 'sends meme',
execute(message, args) {
https.get(url, (result) => {
var body = ''
result.on('data', (chunk) => {
body += chunk
})
result.on('end', () => {
var response = JSON.parse(body)
var index = response.data.children[Math.floor(Math.random() * 99) + 1].data
if (index.post_hint !== 'image') {
var text = index.selftext
const textembed = new Discord.MessageEmbed()
.setTitle(subRedditName)
.setColor(9384170)
.setDescription(`[${title}](${link})\n\n${text}`)
.setURL(`https://reddit./${subRedditName}`)
message.channel.send(textembed)
}
var image = index.preview.images[0].source.url
var title = index.title
var link = 'https://reddit.' + index.permalink
var subRedditName = index.subreddit_name_prefixed
if (index.post_hint !== 'image') {
const textembed = new Discord.RichEmbed()
.setTitle(subRedditName)
.setColor(9384170)
.setDescription(`[${title}](${link})\n\n${text}`)
.setURL(`https://reddit./${subRedditName}`)
message.channel.send(textembed)
}
console.log(image);
const imageembed = new Discord.MessageEmbed()
.setTitle(subRedditName)
.setImage(image)
.setColor(9384170)
.setDescription(`[${title}](${link})`)
.setURL(`https://reddit./${subRedditName}`)
message.channel.send(imageembed)
}).on('error', function (e) {
console.log('Got an error: ', e)
})
})
},
}
Share
Improve this question
edited Apr 21, 2020 at 19:37
Syntle
5,1743 gold badges15 silver badges34 bronze badges
asked Apr 21, 2020 at 17:59
GoldentgGoldentg
471 gold badge2 silver badges12 bronze badges
3 Answers
Reset to default 3I have tried your code and have seen that the &
in the image URL is encoded to &
(example: https://preview.redd.it/z8jsjzvkhq051.jpg?auto=webp&s=bb492d4861f48b62da806584f26bcc15f4d6663a
) that Redd.it can't understand it and returns a 403 error.
Just replace the &
to &
in the image URL and it worked for me.
const https = require('https');
const Discord = require('discord.js');
const url = 'https://www.reddit./r/meme/hot/.json?limit=100'
module.exports = {
name: 'meme',
description: 'sends meme',
execute(message, args) {
https.get(url, (result) => {
var body = ''
result.on('data', (chunk) => {
body += chunk
})
result.on('end', () => {
var response = JSON.parse(body)
var index = response.data.children[Math.floor(Math.random() * 99) + 1].data
if (index.post_hint !== 'image') {
var text = index.selftext
const textembed = new Discord.MessageEmbed()
.setTitle(subRedditName)
.setColor(9384170)
.setDescription(`[${title}](${link})\n\n${text}`)
.setURL(`https://reddit./${subRedditName}`)
message.channel.send(textembed)
}
var image = index.preview.images[0].source.url.replace('&', '&')
var title = index.title
var link = 'https://reddit.' + index.permalink
var subRedditName = index.subreddit_name_prefixed
if (index.post_hint !== 'image') {
const textembed = new Discord.RichEmbed()
.setTitle(subRedditName)
.setColor(9384170)
.setDescription(`[${title}](${link})\n\n${text}`)
.setURL(`https://reddit./${subRedditName}`)
message.channel.send(textembed)
}
console.log(image);
const imageembed = new Discord.MessageEmbed()
.setTitle(subRedditName)
.setImage(image)
.setColor(9384170)
.setDescription(`[${title}](${link})`)
.setURL(`https://reddit./${subRedditName}`)
message.channel.send(imageembed)
}).on('error', function (e) {
console.log('Got an error: ', e)
})
})
},
}
I tweaked the code to make it work with any subreddit. So you would just do (prefix)meme funny and it would search the top 100 or less posts for the subreddit "funny". I also added a nsfw check. If you are not in a nsfw channel on discord then it will go through each one of the top 100 posts and check if it is nsfw or not. If it is then it will remove it from the array that I pushed each of the posts into. If it cannot find a non-nsfw post then it will tell you that in chat. However if you are in a nsfw chat, then it will just pick a random post from the top 100 it found.
Side note: I could not find a way to check if the subreddit exists or not, so I just made it check if the result had more than 1000 characters. Any subreddit that exists should have more than 1000 characters. And any that does not exist should have less than 1000 characters.
const prefix = '.'
const args = message.content.substring(1).split(" ")
if (message.content.startsWith(prefix){
if (args[0] == 'meme'){
if (args[1] != null){
var url = `https://www.reddit./r/${args[1]}/hot/.json?limit=100`
} else {
var url = `https://www.reddit./r/meme/hot/.json?limit=100`
}
https.get(url, (result) => {
var body = ''
var chunked = false
result.on('data', (chunk) => {
body += chunk
if (chunked == false){
chunked = true
}
})
result.on('end', () => {
if (body.length > 1000){
var response = JSON.parse(body)
var postChildren = []
if (message.channel.nsfw == false){
var postsNumber = 0
for (var number = 0; number < response.data.children.length; number++){
postChildren.push(number)
}
for (var found = false; found == false; postsNumber ++){
if (postChildren.length > 0){
var index1 = Math.floor(Math.random() * (postChildren.length))
var index2 = postChildren[index1]
if (response.data.children[index2].data.over_18 == true){
postChildren.splice(index1, 1)
} else {
var index = response.data.children[index2].data
var found = true
}
} else {
var found = true
}
}
} else {
var index = response.data.children[Math.floor(Math.random() * (response.data.children.length-1)) + 1].data
}
if (postChildren.length > 0 || message.channel.nsfw){
var title = index.title
var link = 'https://reddit.' + index.permalink
var subRedditName = index.subreddit_name_prefixed
if (index.post_hint !== 'image') {
var text = index.selftext
if (title.length > 256) {
title = (title.substring(0, 253) + "...")
}
if (text.length > 2048) {
text = (text.substring(0, 2045) + "...")
}
const textembed = new Discord.MessageEmbed()
.setTitle(title)
.setColor('#ff0000')
.setDescription(text)
.setURL(`https://reddit./${subRedditName}`)
message.channel.send(textembed)
}
if (index.post_hint == 'image'){
var image = index.preview.images[0].source.url.replace('&', '&')
if (title.length > 256) {
title = (title.substring(0, 253) + "...")
}
const imageembed = new Discord.MessageEmbed()
.setTitle(title)
.setImage(image)
.setColor('#ff0000')
.setURL(link)
message.channel.send(imageembed)
}
} else {
message.channel.send('Could not find a meme that was not nsfw')
}
} else {
message.channel.send('Could not find subreddit!')
}
}).on('error', function (e) {
console.log('Got an error: ', e)
})
})
}
}
This was kinda hard to read since you repeated blocks of code twice, (two text embeds and img embeds)
first [Math.floor(Math.random() * 99) + 1]
=> [Math.floor(Math.random() * 100)]
the second one makes it so any value can be chosen, the first one can never be 0.
try this (insert code after index variable)
const isImage = index.post_hint === "image";
const subRedditName = index.subreddit_name_prefixed;
const title = index.title;
const link = 'https://reddit.' + index.permalink;
const text = !isImage && index.selfText;
const desc = `[${title}](${link})`;
const embed = new Discord.MessageEmbed()
.setTitle(subRedditName)
.setColor(9384170)
.setDescription(desc + (text ? `\n\n${text}` : ""))
.setURL(`https://reddit./${subRedditName}`);
if (isImage) {
const img = index.preview.images[0].source.url;
embed.setImage(img);
}
messsage.channel.send(embed);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744381653a4571459.html
评论列表(0条)