const Eris = require("eris"); const steem = require('steem'); const bot = new Eris("TOKEN"); const channelId = 'CHANNEL ID'; const account = 'STEEM ID';
function start() { steem.api.streamTransactions("head", function (err, result) { if (!err) { if (result) { let op = result.operations[0]; const time = new Date(); const type = op[0]; const params = op[1]; switch (type) { case 'comment': { const isRootPost = !params.parent_author; //Find reply if (!isRootPost && params.parent_author === account) { let content = `@${params.parent_author}:\n@${params.author} has commented on your post`; let title = `${params.permlink}`; let comment = params.body.substring(0, 100); let url = `https://steem.buzz/@${params.author}/${params.permlink}`; createMessge(content, title, comment, url, time); }
//Find Mention if (params.body.includes(`@${account}`)) { let content = ''; let title = ''; let comment = ''; let url = ''; if (isRootPost) { content = `@${account}:\n"@${account}" was mentioned by @${params.author} in a post`; title = `${params.title}`; comment = params.body.substring(0, 100); } else { content = `@${account}:\n"@${account}" was mentioned by @${params.author} in a comment`; title = `${params.permlink}`; comment = params.body.substring(0, 100); } url = `https://steem.buzz/@${params.author}/${params.permlink}`; createMessge(content, title, comment, url, time); } break; }
case 'custom_json': { let json = {}; try { json = JSON.parse(params.json);
} catch (err) { console.log('Wrong json format on custom_json', err); } switch (params.id) { case 'follow': { /** Find follow */ if (json[0] === 'follow' && json[1].following === account) { let content = `@${json[1].follower} now follows you`; let title = ''; let comment = ''; let url = ''; createMessge(content, title, comment, url, time); } } } break; }
case 'transfer': { if (params.to === account) { let content = `@${params.from} transferred ${params.amount} to you`; let title = ''; let comment = params.memo; let url = ''; createMessge(content, title, comment, url, time); } break; } case 'vote': { //Find downvote if (params.weight < 0 && params.author === account) { let content = `@${params.voter} downvoted your post`; let title = params.permlink; let comment = ''; let url = `https://steem.buzz/@${params.author}/${params.permlink}`; createMessge(content, title, comment, url, time); } } } } } else { sleep(2000).then(() => { console.error(err); start(); }); } }); }
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
bot.once("ready", () => { // When the bot is ready console.log("Ready!"); // Log "Ready!" console.log(new Date()); start();
});
function createMessge(content, title, comment, url, time) { bot.createMessage(channelId, { content: content, embed: { title: title, color: 0x008000, url: url, description: comment, footer: { text: `Time:${time}` } } }) }
bot.connect(); // Get the bot to connect to Discord
|