From 2f348e20511385efa6122a7063be8605854652bf Mon Sep 17 00:00:00 2001 From: filipnyquist Date: Fri, 6 Jul 2018 12:03:28 +0200 Subject: [PATCH] Started work on the next version of tipbot. --- .gitignore | 4 ++- README.md | 6 +++- index.js | 15 +++++++- move_helper.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 move_helper.js diff --git a/.gitignore b/.gitignore index 76345ff..5807a7b 100644 --- a/.gitignore +++ b/.gitignore @@ -73,4 +73,6 @@ typings/ .serverless # Configurations -default.json \ No newline at end of file +default.json + +.idea/* \ No newline at end of file diff --git a/README.md b/README.md index d01d311..7f3b693 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,11 @@ yarn install ``` node index.js ``` - +>If you want to move over accounts from the old tipbot format which used usernames as identifier, run move_helper.js: +``` +node move_helper.js +``` +>It will automatically move over the old accounts to the new id based system. ## Contributing diff --git a/index.js b/index.js index 12e27dd..8d88110 100644 --- a/index.js +++ b/index.js @@ -67,6 +67,10 @@ function checkTweet(tweet, msg) { case "terms": doTerms(tweet, msg); break; + case "lbryian": + logger.info("Got a command with the old format, handling it..."); + checkTweet(tweet, msg.splice(1)); + break; } } @@ -75,7 +79,7 @@ async function doHelp(tweet, msg) { let post = await T.post("statuses/update", { status: `@${tweet.user.screen_name} `+ - "All commands should be called with @ + subcommand \n" + + `All commands should be called with ${config.get("bot.handle")} + subcommand \n` + "help - Shows this command. \n" + "balance - Get your balance. \n" + "deposit - Get address for your deposits. \n" + @@ -92,6 +96,13 @@ async function doHelp(tweet, msg) { } async function doTerms(tweet, msg){ // ADD terms + await T.post("statuses/update", { + status: + `@${tweet.user.screen_name} `+ + "There are no fees to use this bot except the automatic daemon fee. \n"+ + "In no event shall LBRY Inc be responsible in the event of lost, stolen or misdirected funds.", + in_reply_to_status_id: tweet.id_str + }); } async function doBalance(tweet, msg) { try { @@ -162,6 +173,7 @@ async function doTip(tweet, msg) { }); } const userToTip = tweet.entities.user_mentions.find(u => `@${u.screen_name}` === msg[2]).id_str; + await getAddress(id(userToTip)) // Call this to ensure user has an account. if (userToTip === null) { return await T.post("statuses/update", { status: `@${tweet.user.screen_name} I could not find that user...`, @@ -206,6 +218,7 @@ async function getAddress(userId) { logger.error(e); } } + function getValidatedAmount(amount) { amount = amount.trim(); if (amount.toLowerCase().endsWith("lbc")) { diff --git a/move_helper.js b/move_helper.js new file mode 100644 index 0000000..20b0437 --- /dev/null +++ b/move_helper.js @@ -0,0 +1,97 @@ +// This file helps with moving over accounts from the old username system to the id system. +// It uses the same configuration files as index.js +// Checks for the old format, gets their id from twitter, creates new acc, moves balance. +const Twit = require("twit"); +const config = require("config"); +const winston = require("winston"); +require("winston-daily-rotate-file"); +const Client = require("bitcoin-core"); +const lbry = new Client({ + version: "0.12.0", + username: config.get("lbrycrd.username"), + password: config.get("lbrycrd.password"), + port: config.get("lbrycrd.port") +}); +const logger = winston.createLogger({ + level: "info", + format: winston.format.json(), + transports: [ + new winston.transports.DailyRotateFile({ + filename: "move-helper-%DATE%.log", + dirname: "./logs", + datePattern: "YYYY-MM-DD-HH", + zippedArchive: true, + maxSize: "20m", + maxFiles: "14d" + }), + new winston.transports.Console({ + format: winston.format.simple(), + level: "debug" + }) + ] +}); +let notSynced = []; + +const T = new Twit({ + consumer_key: config.get("twitter.consumer_key"), + consumer_secret: config.get("twitter.consumer_secret"), + access_token: config.get("twitter.access_token"), + access_token_secret: config.get("twitter.access_token_secret"), + timeout_ms: 60 * 1000, // optional HTTP request timeout to apply to all requests. + strictSSL: true // optional - requires SSL certificates to be valid. +}); + +async function main(){ + let accs = await getAccounts(); + logger.info(`Trying to move ${accs.length} accounts...`) + for (let i in accs){ + try { + //Get user details from twitter. + let data = await T.get('users/show', { screen_name: accs[i] }); + //Create a account for the user by id. + let usr = data.data.id_str; + await getAddress(id(usr)); + //Move over from old account to the new account + const balanceFromOld = await lbry.getBalance(`twttr-${accs[i]}`); + if (balanceFromOld !== 0) { + let res = await lbry.move( + `twttr-${accs[i]}`, + id(usr), + Number(balanceFromOld) + ); + // If move is successful, log it! + if (res) logger.info(`Transferred ${balanceFromOld} LBC from twttr-${accs[i]} to ${id(usr)}!`); + } + }catch(e){ + logger.info(`Could not sync ${accs[i]}, error occured:`, e.allErrors); + notSynced.push({ user: accs[i], error: e.allErrors}); + logger.info("Could not sync these:"+JSON.stringify(notSynced)); + } + } +} +// Get a list of all twitter accounts on lbrycrd. +async function getAccounts(){ + let accs = await lbry.listAccounts(); + accs = Object.entries(accs); + let accsArr = []; + for (let i in accs){ + if(accs[i][0].startsWith('twttr-')) accsArr.push(accs[i][0].substring(6)); + } + return accsArr; +} + +async function getAddress(userId) { + try { + let uAddresses = await lbry.getAddressesByAccount(userId); + if (uAddresses.length > 0) return; + await lbry.getNewAddress(userId); + return; + } catch (e) { + logger.error(e); + } +} + +function id(usrId){ + return `t-${usrId}`; +} +main();