mirror of
https://github.com/LBRYFoundation/lbry-wunderbot.git
synced 2025-08-23 17:47:27 +00:00
run precommit before pushing!
This commit is contained in:
parent
2b186b453c
commit
2ed6582dbf
2 changed files with 59 additions and 111 deletions
|
@ -1,48 +1,48 @@
|
||||||
"use strict";
|
'use strict';
|
||||||
|
|
||||||
let lbry;
|
let lbry;
|
||||||
let mongo;
|
let mongo;
|
||||||
let discordBot;
|
let discordBot;
|
||||||
let moment = require("moment");
|
let moment = require('moment');
|
||||||
let request = require("request");
|
let request = require('request');
|
||||||
let sleep = require("sleep");
|
let sleep = require('sleep');
|
||||||
let config = require("config");
|
let config = require('config');
|
||||||
let channels = config.get("claimbot").channels;
|
let channels = config.get('claimbot').channels;
|
||||||
const Discord = require("discord.js");
|
const Discord = require('discord.js');
|
||||||
const rp = require("request-promise");
|
const rp = require('request-promise');
|
||||||
const jsonfile = require("jsonfile");
|
const jsonfile = require('jsonfile');
|
||||||
const path = require("path");
|
const path = require('path');
|
||||||
const fs = require("fs");
|
const fs = require('fs');
|
||||||
const appRoot = require("app-root-path");
|
const appRoot = require('app-root-path');
|
||||||
const fileExists = require("file-exists");
|
const fileExists = require('file-exists');
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: init
|
init: init
|
||||||
};
|
};
|
||||||
|
|
||||||
function init(discordBot_) {
|
function init(discordBot_) {
|
||||||
if (lbry) {
|
if (lbry) {
|
||||||
throw new Error("init was already called once");
|
throw new Error('init was already called once');
|
||||||
}
|
}
|
||||||
|
|
||||||
discordBot = discordBot_;
|
discordBot = discordBot_;
|
||||||
|
|
||||||
const MongoClient = require("mongodb").MongoClient;
|
const MongoClient = require('mongodb').MongoClient;
|
||||||
MongoClient.connect(config.get("mongodb").url, function(err, db) {
|
MongoClient.connect(config.get('mongodb').url, function(err, db) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
mongo = db;
|
mongo = db;
|
||||||
|
|
||||||
console.log("Activating claimbot ");
|
console.log('Activating claimbot ');
|
||||||
discordBot.channels.get(channels[0]).send("activating claimbot");
|
discordBot.channels.get(channels[0]).send('activating claimbot');
|
||||||
|
|
||||||
// Check that our syncState file exist.
|
// Check that our syncState file exist.
|
||||||
fileExists(path.join(appRoot.path, "syncState.json"), (err, exists) => {
|
fileExists(path.join(appRoot.path, 'syncState.json'), (err, exists) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
fs.writeFileSync(path.join(appRoot.path, "syncState.json"), "{}");
|
fs.writeFileSync(path.join(appRoot.path, 'syncState.json'), '{}');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
|
@ -54,19 +54,16 @@ function init(discordBot_) {
|
||||||
|
|
||||||
async function announceClaims() {
|
async function announceClaims() {
|
||||||
// get last block form the explorer API.
|
// get last block form the explorer API.
|
||||||
let lastBlockHeight = JSON.parse(
|
let lastBlockHeight = JSON.parse(await rp('https://explorer.lbry.io/api/v1/status')).status.height;
|
||||||
await rp("https://explorer.lbry.io/api/v1/status")
|
|
||||||
).status.height;
|
|
||||||
// get the latest claims from chainquery since last sync
|
// get the latest claims from chainquery since last sync
|
||||||
let syncState = await getJSON(path.join(appRoot.path, "syncState.json")); // get our persisted state
|
let syncState = await getJSON(path.join(appRoot.path, 'syncState.json')); // get our persisted state
|
||||||
if (!syncState.LastSyncTime) {
|
if (!syncState.LastSyncTime) {
|
||||||
syncState.LastSyncTime = new Date()
|
syncState.LastSyncTime = new Date()
|
||||||
.toISOString()
|
.toISOString()
|
||||||
.slice(0, 19)
|
.slice(0, 19)
|
||||||
.replace("T", " ");
|
.replace('T', ' ');
|
||||||
}
|
}
|
||||||
let claimsSince = JSON.parse(await getClaimsSince(syncState.LastSyncTime))
|
let claimsSince = JSON.parse(await getClaimsSince(syncState.LastSyncTime)).data;
|
||||||
.data;
|
|
||||||
// filter out the claims that we should add to discord
|
// filter out the claims that we should add to discord
|
||||||
let claims = [];
|
let claims = [];
|
||||||
for (let claim of claimsSince) {
|
for (let claim of claimsSince) {
|
||||||
|
@ -76,7 +73,7 @@ async function announceClaims() {
|
||||||
} else {
|
} else {
|
||||||
claim.metadata = null;
|
claim.metadata = null;
|
||||||
}
|
}
|
||||||
if (claim.bid_state !== "Spent" || claim.bid_state !== "Expired") {
|
if (claim.bid_state !== 'Spent' || claim.bid_state !== 'Expired') {
|
||||||
claims.push(claim);
|
claims.push(claim);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,87 +87,38 @@ async function announceClaims() {
|
||||||
// If its a claim, make a claimEmbed
|
// If its a claim, make a claimEmbed
|
||||||
let claimEmbed = new Discord.RichEmbed()
|
let claimEmbed = new Discord.RichEmbed()
|
||||||
.setAuthor(
|
.setAuthor(
|
||||||
claim.channel
|
claim.channel ? `New claim from ${claim.channel}` : 'New claim from Anonymous',
|
||||||
? `New claim from ${claim.channel}`
|
'http://barkpost-assets.s3.amazonaws.com/wp-content/uploads/2013/11/3dDoge.gif',
|
||||||
: "New claim from Anonymous",
|
`http://open.lbry.io/${claim.channel ? `${claim.channel}#${claim.channelId}/${claim['name']}` : `${claim['name']}#${claim['claimId']}`}`
|
||||||
"http://barkpost-assets.s3.amazonaws.com/wp-content/uploads/2013/11/3dDoge.gif",
|
|
||||||
`http://open.lbry.io/${
|
|
||||||
claim.channel
|
|
||||||
? `${claim.channel}#${claim.channelId}/${claim["name"]}`
|
|
||||||
: `${claim["name"]}#${claim["claimId"]}`
|
|
||||||
}`
|
|
||||||
)
|
|
||||||
.setTitle(
|
|
||||||
"lbry://" + (claim.channel ? `${claim.channel}/` : "") + claim["name"]
|
|
||||||
)
|
|
||||||
.setURL(
|
|
||||||
`http://open.lbry.io/${
|
|
||||||
claim.channel
|
|
||||||
? `${claim.channel}#${claim.channelId}/${claim["name"]}`
|
|
||||||
: `${claim["name"]}#${claim["claimId"]}`
|
|
||||||
}`
|
|
||||||
)
|
)
|
||||||
|
.setTitle('lbry://' + (claim.channel ? `${claim.channel}/` : '') + claim['name'])
|
||||||
|
.setURL(`http://open.lbry.io/${claim.channel ? `${claim.channel}#${claim.channelId}/${claim['name']}` : `${claim['name']}#${claim['claimId']}`}`)
|
||||||
.setColor(1399626)
|
.setColor(1399626)
|
||||||
.setFooter(
|
.setFooter(`Block ${claim.height} • Claim ID ${claim.claimId} • Data from Chainquery`);
|
||||||
`Block ${claim.height} • Claim ID ${
|
if (claim.metadata['title']) claimEmbed.addField('Title', claim.metadata['title']);
|
||||||
claim.claimId
|
if (claim.channel) claimEmbed.addField('Channel', claim.channel);
|
||||||
} • Data from Chainquery`
|
if (claim.metadata['description']) {
|
||||||
);
|
claimEmbed.addField('Description', claim.metadata['description'].substring(0, 1020));
|
||||||
if (claim.metadata["title"])
|
|
||||||
claimEmbed.addField("Title", claim.metadata["title"]);
|
|
||||||
if (claim.channel) claimEmbed.addField("Channel", claim.channel);
|
|
||||||
if (claim.metadata["description"]) {
|
|
||||||
claimEmbed.addField(
|
|
||||||
"Description",
|
|
||||||
claim.metadata["description"].substring(0, 1020)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (claim.metadata["fee"])
|
if (claim.metadata['fee']) claimEmbed.addField('Fee', claim.metadata['fee'].amount + ' ' + claim.metadata['fee'].currency);
|
||||||
claimEmbed.addField(
|
if (claim.metadata['license'] && claim.metadata['license'].length > 2) claimEmbed.addField('License', claim.metadata['license']);
|
||||||
"Fee",
|
if (!claim.metadata['nsfw'] && claim.metadata['thumbnail']) claimEmbed.setImage(claim.metadata['thumbnail']);
|
||||||
claim.metadata["fee"].amount + " " + claim.metadata["fee"].currency
|
if (claim.bid_state !== 'Controlling' && claim.height < claim.valid_at_height) {
|
||||||
);
|
|
||||||
if (claim.metadata["license"] && claim.metadata["license"].length > 2)
|
|
||||||
claimEmbed.addField("License", claim.metadata["license"]);
|
|
||||||
if (!claim.metadata["nsfw"] && claim.metadata["thumbnail"])
|
|
||||||
claimEmbed.setImage(claim.metadata["thumbnail"]);
|
|
||||||
if (
|
|
||||||
claim.bid_state !== "Controlling" &&
|
|
||||||
claim.height < claim.valid_at_height
|
|
||||||
) {
|
|
||||||
// Claim have not taken over the old claim, send approx time to event.
|
// Claim have not taken over the old claim, send approx time to event.
|
||||||
let takeoverTime =
|
let takeoverTime = Date.now() + (claim.valid_at_height - lastBlockHeight) * 161 * 1000; // in theory this should be 150, but in practice its closer to 161
|
||||||
Date.now() + (claim.valid_at_height - lastBlockHeight) * 161 * 1000; // in theory this should be 150, but in practice its closer to 161
|
claimEmbed.addField('Takes effect on approx', moment(takeoverTime, 'x').format('MMMM Do [at] HH:mm [UTC]') + ` • at block height ${claim.valid_at_height}`);
|
||||||
claimEmbed.addField(
|
|
||||||
"Takes effect on approx",
|
|
||||||
moment(takeoverTime, "x").format("MMMM Do [at] HH:mm [UTC]") +
|
|
||||||
` • at block height ${claim.valid_at_height}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
claimEmbed.addField(
|
claimEmbed.addField('Claimed for', `${Number.parseFloat(claim.outputValue)} LBC`);
|
||||||
"Claimed for",
|
|
||||||
`${Number.parseFloat(claim.outputValue)} LBC`
|
|
||||||
);
|
|
||||||
discordPost(claimEmbed);
|
discordPost(claimEmbed);
|
||||||
} else if (claim.name.charAt(0) === "@") {
|
} else if (claim.name.charAt(0) === '@') {
|
||||||
// This is a channel claim
|
// This is a channel claim
|
||||||
let channelEmbed = new Discord.RichEmbed()
|
let channelEmbed = new Discord.RichEmbed()
|
||||||
.setAuthor(
|
.setAuthor('New channel claim', 'http://barkpost-assets.s3.amazonaws.com/wp-content/uploads/2013/11/3dDoge.gif', `http://open.lbry.io/${claim['name']}#${claim['claimId']}`)
|
||||||
"New channel claim",
|
.setTitle('lbry://' + (claim.channel ? claim.channel + '/' : '') + claim['name'])
|
||||||
"http://barkpost-assets.s3.amazonaws.com/wp-content/uploads/2013/11/3dDoge.gif",
|
.setURL(`http://open.lbry.io/${claim['name']}#${claim['claimId']}`)
|
||||||
`http://open.lbry.io/${claim["name"]}#${claim["claimId"]}`
|
|
||||||
)
|
|
||||||
.setTitle(
|
|
||||||
"lbry://" + (claim.channel ? claim.channel + "/" : "") + claim["name"]
|
|
||||||
)
|
|
||||||
.setURL(`http://open.lbry.io/${claim["name"]}#${claim["claimId"]}`)
|
|
||||||
.setColor(1399626)
|
.setColor(1399626)
|
||||||
.setFooter(
|
.setFooter(`Block ${claim.height} • Claim ID ${claim.claimId} • Data from Chainquery`)
|
||||||
`Block ${claim.height} • Claim ID ${
|
.addField('Channel Name', claim['name']);
|
||||||
claim.claimId
|
|
||||||
} • Data from Chainquery`
|
|
||||||
)
|
|
||||||
.addField("Channel Name", claim["name"]);
|
|
||||||
discordPost(channelEmbed);
|
discordPost(channelEmbed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -178,8 +126,8 @@ async function announceClaims() {
|
||||||
syncState.LastSyncTime = new Date()
|
syncState.LastSyncTime = new Date()
|
||||||
.toISOString()
|
.toISOString()
|
||||||
.slice(0, 19)
|
.slice(0, 19)
|
||||||
.replace("T", " ");
|
.replace('T', ' ');
|
||||||
await saveJSON(path.join(appRoot.path, "syncState.json"), syncState);
|
await saveJSON(path.join(appRoot.path, 'syncState.json'), syncState);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getJSON(path) {
|
function getJSON(path) {
|
||||||
|
@ -209,7 +157,7 @@ function discordPost(embed) {
|
||||||
channels.forEach(channel => {
|
channels.forEach(channel => {
|
||||||
discordBot.channels
|
discordBot.channels
|
||||||
.get(channel)
|
.get(channel)
|
||||||
.send("", embed)
|
.send('', embed)
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -244,7 +192,7 @@ function getClaimsSince(time) {
|
||||||
resolve(htmlString);
|
resolve(htmlString);
|
||||||
})
|
})
|
||||||
.catch(function(err) {
|
.catch(function(err) {
|
||||||
console.log("error", "[Importer] Error getting updated claims. " + err);
|
console.log('error', '[Importer] Error getting updated claims. ' + err);
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -13,13 +13,13 @@ exports.lbrylink = async function(bot, msg, suffix) {
|
||||||
try {
|
try {
|
||||||
// Extract the URL(s).
|
// Extract the URL(s).
|
||||||
const urls = msg.content
|
const urls = msg.content
|
||||||
.replace(new RegExp("(lbry:\\/\\/)", "g"), "https://open.lbry.io/")
|
.replace(new RegExp('(lbry:\\/\\/)', 'g'), 'https://open.lbry.io/')
|
||||||
.match(/\bhttps?:\/\/\S+/gi)
|
.match(/\bhttps?:\/\/\S+/gi)
|
||||||
.filter(url => url !== "https://open.lbry.io/");
|
.filter(url => url !== 'https://open.lbry.io/');
|
||||||
const cleanURLs = [];
|
const cleanURLs = [];
|
||||||
for (const i in urls) {
|
for (const i in urls) {
|
||||||
// Check if Username Was Supplied
|
// Check if Username Was Supplied
|
||||||
const user = urls[i].match("<@.*>");
|
const user = urls[i].match('<@.*>');
|
||||||
if (user) {
|
if (user) {
|
||||||
const { username } = msg.mentions.users.get(user[0].slice(2, -1));
|
const { username } = msg.mentions.users.get(user[0].slice(2, -1));
|
||||||
urls[i] = urls[i].replace(user[0], `@${username}`);
|
urls[i] = urls[i].replace(user[0], `@${username}`);
|
||||||
|
@ -30,14 +30,14 @@ exports.lbrylink = async function(bot, msg, suffix) {
|
||||||
if (cleanURLs.length < 1) return;
|
if (cleanURLs.length < 1) return;
|
||||||
const linkEmbed = new RichEmbed();
|
const linkEmbed = new RichEmbed();
|
||||||
linkEmbed
|
linkEmbed
|
||||||
.setAuthor("LBRY Linker")
|
.setAuthor('LBRY Linker')
|
||||||
.setDescription("I see you tried to post a LBRY URL, here's a friendly hyperlink to share and for others to access your content with a single click:")
|
.setDescription("I see you tried to post a LBRY URL, here's a friendly hyperlink to share and for others to access your content with a single click:")
|
||||||
.setColor(7976557);
|
.setColor(7976557);
|
||||||
cleanURLs.forEach(url => linkEmbed.addField("Open with LBRY:", url, true));
|
cleanURLs.forEach(url => linkEmbed.addField('Open with LBRY:', url, true));
|
||||||
return msg.channel.send({embed: linkEmbed});
|
return msg.channel.send({ embed: linkEmbed });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
msg.channel.send("Something went wrong when trying to run the lbrylinker, contact a moderator.");
|
msg.channel.send('Something went wrong when trying to run the lbrylinker, contact a moderator.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue