-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
246 lines (203 loc) · 5.3 KB
/
utils.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const numeral = require('numeral');
const _ = require('underscore');
const Sequelize = require('sequelize');
const CryptoJS = require('crypto-js');
const Telegram = require('./services/telegram');
const user_model = require('./models').user.model;
const coin_model = require('./models').coin.model;
const config = require('./config');
function format_number(number) {
// Format number to 2 decimal places if float
return number % 1 === 0
? numeral(number).format('0,0')
: numeral(number).format('0,0.00');
}
function decrypt(key = '', ciphertext = '') {
let result = '';
try {
const bytes = CryptoJS.AES.decrypt(ciphertext, key);
result = bytes.toString(CryptoJS.enc.Utf8);
} catch (e) {
console.log('Error decrypting message', e, ciphertext);
}
return result;
}
async function convert_to_usd(amount) {
const webdollar = await coin_model.findOne({
where: {
ticker: 'WEBD',
},
});
if (!webdollar) {
throw new Error('Coin not found');
}
return parseFloat(amount * webdollar.price_usd);
}
async function transfer_reward(username, amount) {
const telegram = new Telegram();
try {
const user = await user_model.findOne({
where: {
telegram_username: username,
},
});
if (!user) {
throw new Error(`username not found: ${username}`);
}
if (user.game_rewards + amount > config.game.max_user_rewards) {
throw new Error(`rewards limit reached: ${config.game.max_user_rewards}`);
}
const new_balance = user.balance + amount;
await user_model.update(
{
balance: new_balance,
game_rewards: user.game_rewards + amount,
},
{
where: {
id: user.id,
},
}
);
const message =
'🎮 You were rewarded with *' +
format_number(amount) +
'* WEBD from playing 👻 Haunted Tower. Funds in your /tipbalance are receiving /staking rewards.';
telegram
.send_message(user.telegram_id, message, Telegram.PARSE_MODE.MARKDOWN)
.catch(console.error);
return {
user,
new_balance,
balance: user.balance,
};
} catch (error) {
console.error(error);
}
}
// Telegram users can change username
// Make sure we keep it up to date, otherwise accounts can get split
async function update_username(from) {
if (from && from.username) {
await user_model.update(
{
telegram_username: from.username,
},
{
where: {
telegram_id: from.id,
},
logging: false,
}
);
console.log(`Updated username: ${from.username} (${from.id})`);
}
}
async function check_private_message(msg) {
const telegram = new Telegram();
if (
msg.chat.type !== 'private' &&
!config.public_channels.includes(msg.chat.id)
) {
await telegram.send_message(
msg.chat.id,
'ℹ️ Private command. Please message @webdollar_tip_bot to use the command.',
Telegram.PARSE_MODE.HTML,
true
);
throw new Error('Private command');
}
}
async function check_public_message(msg) {
const telegram = new Telegram();
if (msg.chat.type === 'private') {
await telegram.send_message(
msg.chat.id,
'ℹ️ Public command. Please use the command in a channel or group.',
Telegram.PARSE_MODE.HTML,
true
);
throw new Error('Public command');
}
}
async function check_telegram_username(msg) {
const telegram = new Telegram();
if (!msg.from.username) {
await telegram.send_message(
msg.chat.id,
'ℹ️ Please set an username for your telegram account to use the bot.',
Telegram.PARSE_MODE.HTML,
true
);
throw new Error('No username');
}
}
function extract_amount(msg, index = 0) {
const amount_match = msg.text.match(/ [0-9]+/g);
if (amount_match === null) {
return null;
}
let amount = amount_match.length >= index ? amount_match[index] : null;
if (_.isString(amount)) {
amount = amount.trim();
}
amount = parseInt(amount);
return amount;
}
async function check_and_extract_amount(msg, extra_msg) {
const telegram = new Telegram();
const amount = extract_amount(msg);
let message = 'ℹ️ You need to specify the amount.';
// TODO: Check this logic
if (amount === null) {
if (extra_msg) {
message += ' Example: `' + extra_msg + ' 100`';
}
await telegram.send_message(
msg.chat.id,
message,
Telegram.PARSE_MODE.MARKDOWN,
true
);
throw new Error('No amount');
}
return amount;
}
function find_user_by_id_or_username(id, username) {
return user_model.findOne({
where: {
[Sequelize.Op.or]: [
{
telegram_id: id,
},
{
telegram_username: username,
},
],
},
});
}
function array_chunks(array, chunk_size) {
return Array(Math.ceil(array.length / chunk_size))
.fill()
.map((_, index) => index * chunk_size)
.map((begin) => array.slice(begin, begin + chunk_size));
}
module.exports = {
get_amount_for_price,
get_package_for_amount,
transfer_reward,
transfer_funds,
transfer_funds_locked,
update_username,
format_number,
convert_to_usd,
check_private_message,
check_public_message,
check_telegram_username,
check_and_extract_amount,
extract_amount,
find_user_by_id_or_username,
array_chunks,
decrypt,
};