3 回答

TA貢獻1789條經驗 獲得超10個贊
我已經嘗試了您的代碼,并且看到&圖像中的 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)
})
})
},
}

TA貢獻1936條經驗 獲得超7個贊
我調整了代碼以使其適用于任何 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){
if (args[0] == 'meme'){
if (args[1] != null){
var url = `https://www.reddit.com/r/${args[1]}/hot/.json?limit=100`
} else {
var url = `https://www.reddit.com/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.com' + 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.com/${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)
})
})
}
}

TA貢獻1816條經驗 獲得超6個贊
這有點難以閱讀,因為您重復了兩次代碼塊(兩個文本嵌入和 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()
.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);
添加回答
舉報