猿问

带有 meme subreddit 的 Discord.js meme 命令将图像返回为

我正在制作一个从 meme subreddit 发送随机 meme 的 meme 命令,在发现除了嵌入中的实际图像之外的所有内容后,我将图像记录到控制台并返回403错误。如果有人能够提供帮助,将不胜感激。


代码:


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)

                }

     

神不在的星期二
浏览 103回答 3
3回答

至尊宝的传说

我已经尝试了您的代码,并且看到&图像中的 URL 被编码为&(例如https://preview.redd.it/z8jsjzvkhq051.jpg?auto=webp&s=bb492d4861f48b62da806584f26bcc15f4d6663a:) Redd.it 无法理解它并返回 403 错误。只需将图像 URL 中的&to替换为&它对我有用。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)            })        })    },}

LEATH

我调整了代码以使其适用于任何 subreddit。所以你只需做(前缀)meme funny,它会在前 100 个或更少的帖子中搜索 subreddit “funny”。我还添加了一个 nsfw 检查。如果您不在 discord 的 nsfw 频道中,那么它将遍历前 100 个帖子中的每一个,并检查它是否是 nsfw。如果是,那么它将从我将每个帖子推入的数组中删除。如果它找不到非 nsfw 帖子,那么它会在聊天中告诉你。但是,如果您在 nsfw 聊天中,那么它只会从它找到的前 100 个帖子中随机选择一个帖子。旁注:我找不到检查 subreddit 是否存在的方法,所以我只是检查结果是否超过 1000 个字符。任何存在的 subreddit 都应该有超过 1000 个字符。任何不存在的字符都应少于 1000 个字符。const prefix = '.'const args = message.content.substring(1).split(" ")if (message.content.startsWith(prefix){&nbsp; &nbsp; if (args[0] == 'meme'){&nbsp; &nbsp; &nbsp; &nbsp; if (args[1] != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var url = `https://www.reddit.com/r/${args[1]}/hot/.json?limit=100`&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var url = `https://www.reddit.com/r/meme/hot/.json?limit=100`&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; https.get(url, (result) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var body = ''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var chunked = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.on('data', (chunk) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body += chunk&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (chunked == false){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunked = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.on('end', () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (body.length > 1000){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var response = JSON.parse(body)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var postChildren = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (message.channel.nsfw == false){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var postsNumber = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var number = 0; number < response.data.children.length; number++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postChildren.push(number)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var found = false; found == false; postsNumber ++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (postChildren.length > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var index1 = Math.floor(Math.random() * (postChildren.length))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var index2 = postChildren[index1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.data.children[index2].data.over_18 == true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postChildren.splice(index1, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var index = response.data.children[index2].data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var found = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var found = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var index = response.data.children[Math.floor(Math.random() * (response.data.children.length-1)) + 1].data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (postChildren.length > 0 || message.channel.nsfw){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var title = index.title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var link = 'https://reddit.com' + index.permalink&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var subRedditName = index.subreddit_name_prefixed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index.post_hint !== 'image') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var text = index.selftext&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (title.length > 256) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title = (title.substring(0, 253) + "...")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (text.length > 2048) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = (text.substring(0, 2045) + "...")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const textembed = new Discord.MessageEmbed()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setTitle(title)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setColor('#ff0000')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setDescription(text)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setURL(`https://reddit.com/${subRedditName}`)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.channel.send(textembed)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index.post_hint == 'image'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var image = index.preview.images[0].source.url.replace('&amp;', '&')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (title.length > 256) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title = (title.substring(0, 253) + "...")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const imageembed = new Discord.MessageEmbed()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setTitle(title)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setImage(image)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setColor('#ff0000')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setURL(link)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.channel.send(imageembed)&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.channel.send('Could not find a meme that was not nsfw')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.channel.send('Could not find subreddit!')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).on('error', function (e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('Got an error: ', e)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }}

潇湘沐

这有点难以阅读,因为您重复了两次代码块(两个文本嵌入和 img 嵌入)第一个[Math.floor(Math.random() * 99) + 1]=>[Math.floor(Math.random() * 100)] 第二个使它可以选择任何值,第一个永远不能为0。试试这个(在索引变量后插入代码)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()&nbsp; &nbsp; .setTitle(subRedditName)&nbsp; &nbsp; .setColor(9384170)&nbsp; &nbsp; .setDescription(desc + (text ? `\n\n${text}` : ""))&nbsp; &nbsp; .setURL(`https://reddit.com/${subRedditName}`);if (isImage) {&nbsp; &nbsp; const img = index.preview.images[0].source.url;&nbsp; &nbsp; embed.setImage(img);}messsage.channel.send(embed);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答