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 mongo;
|
||||
let discordBot;
|
||||
let moment = require("moment");
|
||||
let request = require("request");
|
||||
let sleep = require("sleep");
|
||||
let config = require("config");
|
||||
let channels = config.get("claimbot").channels;
|
||||
const Discord = require("discord.js");
|
||||
const rp = require("request-promise");
|
||||
const jsonfile = require("jsonfile");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const appRoot = require("app-root-path");
|
||||
const fileExists = require("file-exists");
|
||||
let moment = require('moment');
|
||||
let request = require('request');
|
||||
let sleep = require('sleep');
|
||||
let config = require('config');
|
||||
let channels = config.get('claimbot').channels;
|
||||
const Discord = require('discord.js');
|
||||
const rp = require('request-promise');
|
||||
const jsonfile = require('jsonfile');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const appRoot = require('app-root-path');
|
||||
const fileExists = require('file-exists');
|
||||
module.exports = {
|
||||
init: init
|
||||
};
|
||||
|
||||
function init(discordBot_) {
|
||||
if (lbry) {
|
||||
throw new Error("init was already called once");
|
||||
throw new Error('init was already called once');
|
||||
}
|
||||
|
||||
discordBot = discordBot_;
|
||||
|
||||
const MongoClient = require("mongodb").MongoClient;
|
||||
MongoClient.connect(config.get("mongodb").url, function(err, db) {
|
||||
const MongoClient = require('mongodb').MongoClient;
|
||||
MongoClient.connect(config.get('mongodb').url, function(err, db) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
mongo = db;
|
||||
|
||||
console.log("Activating claimbot ");
|
||||
discordBot.channels.get(channels[0]).send("activating claimbot");
|
||||
console.log('Activating claimbot ');
|
||||
discordBot.channels.get(channels[0]).send('activating claimbot');
|
||||
|
||||
// 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) {
|
||||
throw err;
|
||||
}
|
||||
if (!exists) {
|
||||
fs.writeFileSync(path.join(appRoot.path, "syncState.json"), "{}");
|
||||
fs.writeFileSync(path.join(appRoot.path, 'syncState.json'), '{}');
|
||||
}
|
||||
});
|
||||
setInterval(function() {
|
||||
|
@ -54,19 +54,16 @@ function init(discordBot_) {
|
|||
|
||||
async function announceClaims() {
|
||||
// get last block form the explorer API.
|
||||
let lastBlockHeight = JSON.parse(
|
||||
await rp("https://explorer.lbry.io/api/v1/status")
|
||||
).status.height;
|
||||
let lastBlockHeight = JSON.parse(await rp('https://explorer.lbry.io/api/v1/status')).status.height;
|
||||
// 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) {
|
||||
syncState.LastSyncTime = new Date()
|
||||
.toISOString()
|
||||
.slice(0, 19)
|
||||
.replace("T", " ");
|
||||
.replace('T', ' ');
|
||||
}
|
||||
let claimsSince = JSON.parse(await getClaimsSince(syncState.LastSyncTime))
|
||||
.data;
|
||||
let claimsSince = JSON.parse(await getClaimsSince(syncState.LastSyncTime)).data;
|
||||
// filter out the claims that we should add to discord
|
||||
let claims = [];
|
||||
for (let claim of claimsSince) {
|
||||
|
@ -76,7 +73,7 @@ async function announceClaims() {
|
|||
} else {
|
||||
claim.metadata = null;
|
||||
}
|
||||
if (claim.bid_state !== "Spent" || claim.bid_state !== "Expired") {
|
||||
if (claim.bid_state !== 'Spent' || claim.bid_state !== 'Expired') {
|
||||
claims.push(claim);
|
||||
}
|
||||
}
|
||||
|
@ -90,87 +87,38 @@ async function announceClaims() {
|
|||
// If its a claim, make a claimEmbed
|
||||
let claimEmbed = new Discord.RichEmbed()
|
||||
.setAuthor(
|
||||
claim.channel
|
||||
? `New claim from ${claim.channel}`
|
||||
: "New claim from Anonymous",
|
||||
"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"]}`
|
||||
}`
|
||||
claim.channel ? `New claim from ${claim.channel}` : 'New claim from Anonymous',
|
||||
'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']}`}`)
|
||||
.setColor(1399626)
|
||||
.setFooter(
|
||||
`Block ${claim.height} • Claim ID ${
|
||||
claim.claimId
|
||||
} • Data from Chainquery`
|
||||
);
|
||||
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)
|
||||
);
|
||||
.setFooter(`Block ${claim.height} • Claim ID ${claim.claimId} • Data from Chainquery`);
|
||||
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"])
|
||||
claimEmbed.addField(
|
||||
"Fee",
|
||||
claim.metadata["fee"].amount + " " + claim.metadata["fee"].currency
|
||||
);
|
||||
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
|
||||
) {
|
||||
if (claim.metadata['fee']) claimEmbed.addField('Fee', claim.metadata['fee'].amount + ' ' + claim.metadata['fee'].currency);
|
||||
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.
|
||||
let takeoverTime =
|
||||
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}`
|
||||
);
|
||||
let takeoverTime = 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(
|
||||
"Claimed for",
|
||||
`${Number.parseFloat(claim.outputValue)} LBC`
|
||||
);
|
||||
claimEmbed.addField('Claimed for', `${Number.parseFloat(claim.outputValue)} LBC`);
|
||||
discordPost(claimEmbed);
|
||||
} else if (claim.name.charAt(0) === "@") {
|
||||
} else if (claim.name.charAt(0) === '@') {
|
||||
// This is a channel claim
|
||||
let channelEmbed = new Discord.RichEmbed()
|
||||
.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"]}`
|
||||
)
|
||||
.setTitle(
|
||||
"lbry://" + (claim.channel ? claim.channel + "/" : "") + claim["name"]
|
||||
)
|
||||
.setURL(`http://open.lbry.io/${claim["name"]}#${claim["claimId"]}`)
|
||||
.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']}`)
|
||||
.setTitle('lbry://' + (claim.channel ? claim.channel + '/' : '') + claim['name'])
|
||||
.setURL(`http://open.lbry.io/${claim['name']}#${claim['claimId']}`)
|
||||
.setColor(1399626)
|
||||
.setFooter(
|
||||
`Block ${claim.height} • Claim ID ${
|
||||
claim.claimId
|
||||
} • Data from Chainquery`
|
||||
)
|
||||
.addField("Channel Name", claim["name"]);
|
||||
.setFooter(`Block ${claim.height} • Claim ID ${claim.claimId} • Data from Chainquery`)
|
||||
.addField('Channel Name', claim['name']);
|
||||
discordPost(channelEmbed);
|
||||
}
|
||||
}
|
||||
|
@ -178,8 +126,8 @@ async function announceClaims() {
|
|||
syncState.LastSyncTime = new Date()
|
||||
.toISOString()
|
||||
.slice(0, 19)
|
||||
.replace("T", " ");
|
||||
await saveJSON(path.join(appRoot.path, "syncState.json"), syncState);
|
||||
.replace('T', ' ');
|
||||
await saveJSON(path.join(appRoot.path, 'syncState.json'), syncState);
|
||||
}
|
||||
|
||||
function getJSON(path) {
|
||||
|
@ -209,7 +157,7 @@ function discordPost(embed) {
|
|||
channels.forEach(channel => {
|
||||
discordBot.channels
|
||||
.get(channel)
|
||||
.send("", embed)
|
||||
.send('', embed)
|
||||
.catch(console.error);
|
||||
});
|
||||
}
|
||||
|
@ -244,7 +192,7 @@ function getClaimsSince(time) {
|
|||
resolve(htmlString);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log("error", "[Importer] Error getting updated claims. " + err);
|
||||
console.log('error', '[Importer] Error getting updated claims. ' + err);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -13,13 +13,13 @@ exports.lbrylink = async function(bot, msg, suffix) {
|
|||
try {
|
||||
// Extract the URL(s).
|
||||
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)
|
||||
.filter(url => url !== "https://open.lbry.io/");
|
||||
.filter(url => url !== 'https://open.lbry.io/');
|
||||
const cleanURLs = [];
|
||||
for (const i in urls) {
|
||||
// Check if Username Was Supplied
|
||||
const user = urls[i].match("<@.*>");
|
||||
const user = urls[i].match('<@.*>');
|
||||
if (user) {
|
||||
const { username } = msg.mentions.users.get(user[0].slice(2, -1));
|
||||
urls[i] = urls[i].replace(user[0], `@${username}`);
|
||||
|
@ -30,14 +30,14 @@ exports.lbrylink = async function(bot, msg, suffix) {
|
|||
if (cleanURLs.length < 1) return;
|
||||
const linkEmbed = new RichEmbed();
|
||||
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:")
|
||||
.setColor(7976557);
|
||||
cleanURLs.forEach(url => linkEmbed.addField("Open with LBRY:", url, true));
|
||||
return msg.channel.send({embed: linkEmbed});
|
||||
cleanURLs.forEach(url => linkEmbed.addField('Open with LBRY:', url, true));
|
||||
return msg.channel.send({ embed: linkEmbed });
|
||||
} catch (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