add discord posting

master
lza_menace 4 weeks ago
parent a73f98cd24
commit 8bc7029922

@ -5,6 +5,7 @@ const { Database } = require('sqlite3');
const fs = require('fs'); const fs = require('fs');
const assetsBase = 'https://art101-assets.s3.us-west-2.amazonaws.com';
const db = new Database('./state/sqlite.db'); const db = new Database('./state/sqlite.db');
const config = { const config = {
apiKey: process.env.MORALIS_KEY 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() { async scrape() {
const cursor = this.getCursor() const cursor = this.getCursor()
if (cursor === '') { 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 () => { (async () => {
const tableExists = await new Promise((resolve) => { const tableExists = await new Promise((resolve) => {
db.get('SELECT name FROM sqlite_master WHERE type="table" AND name="events"', [], (err, row) => { 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); const c = new Scrape(contract);
try { try {
await c.scrape(); await c.scrape();
await c.postDiscord();
} catch(e) { } catch(e) {
console.log(e); console.log(e);
} }

Loading…
Cancel
Save