问题
I'm making a meme command 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.com/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.com/${subRedditName}`)
message.channel.send(textembed)
}
var image = index.preview.images[0].source.url
var title = index.title
var link = 'https://reddit.com' + 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.com/${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.com/${subRedditName}`)
message.channel.send(imageembed)
}).on('error', function (e) {
console.log('Got an error: ', e)
})
})
},
}
回答1:
I 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.com/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.com/${subRedditName}`)
message.channel.send(textembed)
}
var image = index.preview.images[0].source.url.replace('&', '&')
var title = index.title
var link = 'https://reddit.com' + 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.com/${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.com/${subRedditName}`)
message.channel.send(imageembed)
}).on('error', function (e) {
console.log('Got an error: ', e)
})
})
},
}
回答2:
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.com' + 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.com/${subRedditName}`);
if (isImage) {
const img = index.preview.images[0].source.url;
embed.setImage(img);
}
messsage.channel.send(embed);
来源:https://stackoverflow.com/questions/61350023/discord-js-meme-command-with-meme-subreddit-returns-image-as-403-forbidden