首页 > 解决方案 > Image Scraper 将所有附件从单个消息推送到数组

问题描述

我一直在这里使用这个脚本https://github.com/17teen/Discord-Image-Scraper但偶然发现了一个问题,它从包含 5 个附件的单个消息中抓取并将它们作为一个整体拉到 JSON 数组中而不是将它们分成单独的链接,就好像它是每条消息的图像一样。

我已经尝试在两者中使用.replace(',','')and并且 仍然面临这个问题。.join(',')post.jsscraper.js

[
"https://media.discordapp.net/attachments/831804312538841088/893067795154231337/IMG_20210930_113200.jpg,https://media.discordapp.net/attachments/831804312538841088/893067795322011668/IMG_20210930_113039.jpg,https://media.discordapp.net/attachments/831804312538841088/893067795535900702/IMG_20210930_112940.jpg,https://media.discordapp.net/attachments/831804312538841088/893067795741437952/IMG_20210930_113006.jpg","EXTRA","EXTRA"
]

标签: javascriptnode.jsarraysgithubdiscord.js

解决方案


感谢@MrMythical解决了这个问题。

对于使用此代码的任何人,修复如下:

原始代码是

webhookCli.send(link.split(",")).then((msg) => { 
     spinner.succeed(greenBright(`[${index}] Link Posted: ${yellowBright(msg.content)}`))
}).catch((err) => { 
   spinner.fail(red(`[${index}] Link failed to post | ${err}`)) 
})

改进的代码 添加了解决方案link

// Settings
const { webHook_id, webHook_token } = require("./settings.json");
const { greenBright, red, grey, yellowBright } = require("chalk");
const ora = require("ora");
const readline = require("readline").createInterface({ input: process.stdin, output: process.stdout });

// Modules
const { WebhookClient } = require("discord.js");

// Webhook
const webhookCli = new WebhookClient(webHook_id, webHook_token);

readline.question(grey("[?] Do you wish to post these links? (Y/N) "), (answr) => {
    if (answr === "Y" || answr === "y" || answr === "Yes" || answr === "yes" || answr === "YES") return Post()
    if (answr === "N" || answr === "n" || answr === "No" || answr === "no" || answr === "NO") return process.exit(1);
});

/**
 * Posts links to a specifed channel
 */
function Post() {
    const spinner = ora("Preparing to post").start();
    const fetchLinks = require("./links.json")
    fetchLinks.forEach((link, index) => {
        webhookCli.send(link.split(",")).then((msg) => { spinner.succeed(greenBright(`[${index}] Link Posted: ${yellowBright(msg.content)}`)) }).catch((err) => { spinner.fail(red(`[${index}] Link failed to post | ${err}`)) })
    });
}

推荐阅读