-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (55 loc) · 3.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const path = require('path');
const { fork } = require('child_process');
const {User} = require('./src/db');
global.srcRoot = path.resolve(__dirname);
global.env = (process.argv[2] === '--production') ? process.env.NODE_ENV : "development";
const Snoowrap = require('snoowrap');
const dotenv = require('dotenv');
dotenv.config({ path: './src/data/config.env' });
const setupDatabase = require('./src/db/setup');
const runPoll = require('./src/handlers/handle_DMs.js');
const client = new Snoowrap({
userAgent : process.env.USER_AGENT,
clientId : process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
username : process.env.USERNAME,
password : process.env.PASSWORD
});
global.welcomeText = `Hello there! I'm /u/pivxtipbot, the official PIVX Reddit Tip Bot! You have interacted with me for the first time, so here's some information about my functionalities and commands. Should any problem arise please contact my maker /u/Bueris.` +
`\n\nTo begin using me, take a look at the following commands:` +
`\n\n !history - Your history of tips.\n\n !transactions - Your transactions (deposits/withdrawals)\n\n !balance - Check your account balance.\n\n !deposit - Get a new one-time deposit address\n\n !withdraw [amount] [address] - Withdraw funds from your account` +
`\n\nIf you have existing balance, you can tip others by replying to a post/comment by them with \`/u/pivxtipbot tip [amount]\`. My code is fully open source! You can review it at http://tip.pivx.events . Have fun!`;
global.welcomeMessage = async function (username) {
return client.composeMessage({ to: username, subject: "Welcome to PIVX Tip Bot!", text: global.welcomeText });
};
global.toFixed = function (num, fixed) {
var re = new RegExp('^-?\\d+(?:.\\d{0,' + (fixed || -1) + '})?');
return num.toString().match(re)[0];
};
setupDatabase().then((result) => {
global.agenda = result.agenda;
console.log(`PIVX Tip Bot starting up...`);
runPoll(client);
let worker;
if (process.argv[2] !== '--no-daemon') { worker = fork('./src/worker'); }
worker.on('message', async (data) => {
// TODO handle user ID and send message accordingly
if (data.deposit) await depositMessage(data);
else await withdrawMessage(data);
});
});
async function depositMessage (data) {
const user = await User.findById(data._id);
if (!user) {
return console.log('ERR: User not found');
}
return client.composeMessage({ to: user.username, subject: "Deposit Success", text: `Your deposit of **${data.amount}** PIVX has been credited.`});
}
async function withdrawMessage (data) {
const user = await User.findById(data._id);
if (!user) {
return console.log('ERR: User not found');
}
if (!data.error && data.txid) return client.composeMessage({ to: user.username, subject: "Withdraw Success", text: `Your withdraw of **${data.amount}** PIVX has been sent. Your TXID is: ${data.txid}`});
else return client.composeMessage({ to: user.username, subject: "Withdraw Failed", text: `Your withdraw of **${data.amount}** PIVX has encountered an error. The error was: ${data.error}`});
}