From 8bc7029922ea47921607e3d4d488fbb32187c1c5 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 26 Dec 2024 00:46:28 -0800 Subject: [PATCH] add discord posting --- src/index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/index.js b/src/index.js index 38eef26..480e4dc 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ const { Database } = require('sqlite3'); const fs = require('fs'); +const assetsBase = 'https://art101-assets.s3.us-west-2.amazonaws.com'; const db = new Database('./state/sqlite.db'); const config = { apiKey: process.env.MORALIS_KEY @@ -40,6 +41,49 @@ class Scrape { }; } + async postDiscord() { + if (! process.env.DISCORD_URL) return + db.all('SELECT * FROM events WHERE discord_sent = 0 AND contract = ?',[this.contractAddress], async (err, r) => { + r.map(async row => { + try { + const title = `Sale of token ${row.token_id} for ${this.contractName}!`; + const desc = `Purchased by ${row.buyer} in block ${row.block_number} for ${Number(row.sale_price) / 1000000000000000000.0}Ξ. [Etherscan](https://etherscan.io/tx/${row.tx_hash})`; + const url = `${assetsBase}/${this.contractAddress}/${row.token_id.toString()}.json`; + const metadata = await fetch(url) + .then((m) => m.json()); + const imageURL = metadata.image.replace('ipfs://', `${assetsBase}/${this.contractAddress}/`) + '.fullsize.png'; + await sleep(2); + await fetch(process.env.DISCORD_URL, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + embeds: [ + { + title: title, + description: desc, + image: { + url: imageURL + }, + url: `https://gallery.art101.io/collection/${this.contractName}/${row.token_id}` + } + ] + }) + }); + db.run(`UPDATE events SET discord_sent = 1 WHERE contract = ? AND token_id = ? and block_number = ?`, [ + row.contract, row.token_id, row.block_number + ]); + return `posted sale info to Discord: ${title} - ${desc} - ${imageURL}`; + } catch(err) { + throw new Error(`[!] Failed to post to Discord: ${err}`); + } + }); + }); + + } + async scrape() { const cursor = this.getCursor() if (cursor === '') { @@ -99,6 +143,12 @@ class Scrape { } +function shortenAddress(address) { + const shortAddress = `${address.slice(0, 6)}...${address.slice(address.length - 4, address.length)}`; + if (address.startsWith('0x')) return shortAddress; + return address; +} + (async () => { const tableExists = await new Promise((resolve) => { db.get('SELECT name FROM sqlite_master WHERE type="table" AND name="events"', [], (err, row) => { @@ -136,6 +186,7 @@ class Scrape { const c = new Scrape(contract); try { await c.scrape(); + await c.postDiscord(); } catch(e) { console.log(e); }