/
qiwi.ts
135 lines (118 loc) · 3.86 KB
/
qiwi.ts
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
import axios from 'axios';
import { getRepository } from 'typeorm';
import api from '../api.js';
import currency from '../currency';
import { User, UserTipInterface } from '../database/entity/user';
import { settings } from '../decorators';
import { ui } from '../decorators.js';
import { onChange, onStartup } from '../decorators/on.js';
import events from '../events.js';
import { triggerInterfaceOnTip } from '../helpers/interface/triggers';
import { error, tip } from '../helpers/log';
import eventlist from '../overlays/eventlist.js';
import alerts from '../registries/alerts.js';
import users from '../users.js';
import Integration from './_interface';
class Qiwi extends Integration {
interval: any = null;
@settings()
@ui({ type: 'text-input', secret: true })
secretToken = '';
@onStartup()
@onChange('enabled')
onEnabledChange (key: string, val: boolean) {
if (val) {
this.start();
} else {
clearInterval(this.interval);
}
}
@onChange('secretToken')
onTokenChange (key: string, val: string) {
if (val) {
this.start();
} else {
clearInterval(this.interval);
}
}
async start () {
clearInterval(this.interval);
if (this.secretToken.trim() === '' || !this.enabled) {
return;
} else {
this.interval = setInterval(() => this.check(), 3000);
}
}
async check () {
let request: any;
try {
request = await axios(`https://donate.qiwi.com/api/stream/v1/widgets/${this.secretToken}/events?limit=50`);
} catch (e) {
error(`Qiwi: error on api request: ${e.message}`);
return;
}
const data = request.data;
if (data.events.length === 0) {
return;
}
for (const event of data.events) {
const { DONATION_SENDER, DONATION_AMOUNT, DONATION_CURRENCY, DONATION_MESSAGE } = event.attributes;
const username: string | null = DONATION_SENDER ? DONATION_SENDER.toLowerCase() : null;
const message = DONATION_MESSAGE ? DONATION_MESSAGE : '';
const amount = Number(DONATION_AMOUNT);
let id: number | null = null;
if (username) {
const user = await users.getUserByUsername(username);
id = user.userId;
const newTip: UserTipInterface = {
amount: Number(amount),
currency: DONATION_CURRENCY,
sortAmount: currency.exchange(Number(amount), DONATION_CURRENCY, currency.mainCurrency),
message: message,
tippedAt: Date.now(),
exchangeRates: currency.rates,
};
user.tips.push(newTip);
getRepository(User).save(user);
}
if (api.isStreamOnline) {
api.stats.currentTips += currency.exchange(amount, DONATION_CURRENCY, currency.mainCurrency);
}
eventlist.add({
event: 'tip',
amount,
currency: DONATION_CURRENCY,
userId: String(username ? await users.getIdByName(username.toLowerCase()) ?? '0' : '0'),
message,
timestamp: Date.now(),
});
tip(`${username ? username : 'Anonymous'}${id ? '#' + id : ''}, amount: ${Number(amount).toFixed(2)}${DONATION_CURRENCY}, ${message ? 'message: ' + message : ''}`);
events.fire('tip', {
username: username || 'Anonymous',
amount,
currency: DONATION_CURRENCY,
amountInBotCurrency: Number(currency.exchange(amount, DONATION_CURRENCY, currency.mainCurrency)).toFixed(2),
currencyInBot: currency.mainCurrency,
message,
});
alerts.trigger({
event: 'tips',
name: username || 'Anonymous',
amount,
tier: 0,
currency: DONATION_CURRENCY,
monthsName: '',
message,
autohost: false,
});
triggerInterfaceOnTip({
username: username || 'Anonymous',
amount,
message,
currency: DONATION_CURRENCY,
timestamp: Date.now(),
});
}
}
}
export default new Qiwi();