const { EmbedBuilder, AttachmentBuilder } = require('discord.js') // v14.11.0 const fetchPage = require('../../axios.js') const fs = require('fs') const https = require('https') const Jimp = require('jimp') const channelId = 'redacted' async function getNewsPosts (discordClient) { try { const posts = await fetchPage(url) let postedIds let newIds = [] const channel = await discordClient.channels.fetch(channelId) if (!channel) { console.error('Channel not found!') return } for (const post of posts.reverse()) { const postid = post.id if (newIds.includes(postid)) continue postedIds = await getPostedIds() if (!postedIds.includes(postid)) { // New post if (!post.thumbnail) { console.log("thumbnail null") continue } const jpghash = post.thumbnail.slice(0, -4) const pngOrJpg = post.thumbnail.slice(-4) const attachment = await getThumbnail(jpghash, pngOrJpg, postid) if (!attachment) { console.log("attachment null") continue } try { const prize = post.prizes const embed = new EmbedBuilder() .setTitle(post.name) .setURL(post.url) .setThumbnail(`https://i.gyazo.com/redacted.png`) .setImage(`attachment://${postid}.png`) if (prize) { embed.addFields({ name: 'Prizes', value: `${prize}`, inline: true }) } // Send the message to the channel console.log("Posting new post: ") console.log(embed) await channel.send({ embeds: [embed], files: [attachment] }) newIds.push(postid) postedIds = [...postedIds, ...newIds] await fs.writeFileSync('LocalData/postedNews.json', JSON.stringify({ postedIds })) break } catch (error) { console.error(error) continue } } } } catch (error) { if (error.response) { console.log(`error ${error.response.status}`) return } console.error(error) } } function getPostedIds () { if (fs.existsSync('LocalData/postedNews.json')) { const data = fs.readFileSync('LocalData/postedNews.json') return JSON.parse(data).postedIds } return [] } async function getThumbnail (jpghash, pngOrJpg, postid) { let thumbnailUrl = `https://example.com/${postid}/thumbnail` const imagePromise = new Promise((resolve, reject) => { https.get(thumbnailUrl, (res) => { let buffer = Buffer.alloc(0) res.on('data', (chunk) => { buffer = Buffer.concat([buffer, chunk]) }) res.on('end', async () => { try { const jimpImage = await Jimp.read(buffer) const resizedImage = await jimpImage.resize(1000, 600).getBufferAsync(Jimp.MIME_PNG) // 800, 460 resolve(resizedImage) } catch (error) { reject(error) } }) }).on('error', (err) => { reject(err) }) }) const imageBuffer = await imagePromise const attachment = new AttachmentBuilder(imageBuffer, { name: `${postid}.png` }) if (attachment) { return attachment } else { console.log("no attachment") return null } } 10.08 10:30:53 [Bot] Posting new post: 10.08 10:30:53 [Bot] EmbedBuilder { 10.08 10:30:53 [Bot] data: { 10.08 10:30:53 [Bot] title: 'News posts name', 10.08 10:30:53 [Bot] url: 'https://example.com/4302', 10.08 10:30:53 [Bot] description: '20/8/2023', 10.08 10:30:53 [Bot] thumbnail: { url: 'https://i.gyazo.com/redacted.png' }, 10.08 10:30:53 [Bot] image: { url: 'attachment://4302.png' }, 10.08 10:30:53 [Bot] fields: [ [Object] ] 10.08 10:30:53 [Bot] } 10.08 10:30:53 [Bot] }