Skip to content

Commit

Permalink
feat(integrations): add qiwi donate integration (#2703)
Browse files Browse the repository at this point in the history
  • Loading branch information
Banonevar authored and sogehige committed Oct 4, 2019
1 parent eb0a18e commit 4cd0312
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions locales/cs/ui.menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"streamlabs": "Streamlabs",
"streamelements": "StreamElements",
"donationalerts": "DonationAlerts.ru",
"qiwi": "Qiwi Donate",
"twitter": "Twitter",
"phillipshue": "Phillips Hue",
"checklist": "Checklist",
Expand Down
9 changes: 9 additions & 0 deletions locales/cs/ui/integrations/qiwi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"settings": {
"enabled": "Status",
"jwtToken": {
"title": "Secret token",
"help": "Získej secret token na Qiwi Donate dashboardu settings->click show secret token"
}
}
}
1 change: 1 addition & 0 deletions locales/en/ui.menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"streamlabs": "Streamlabs",
"streamelements": "StreamElements",
"donationalerts": "DonationAlerts.ru",
"qiwi": "Qiwi Donate",
"twitter": "Twitter",
"phillipshue": "Phillips Hue",
"checklist": "Checklist",
Expand Down
9 changes: 9 additions & 0 deletions locales/en/ui/integrations/qiwi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"settings": {
"enabled": "Status",
"jwtToken": {
"title": "Secret token",
"help": "Get secret token at Qiwi Donate dashboard settings->click show secret token"
}
}
}
118 changes: 118 additions & 0 deletions src/bot/integrations/qiwi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import axios from 'axios';
import { onChange, onStartup } from '../decorators/on.js';
import { settings } from '../decorators';
import { ui } from '../decorators.js';
import { triggerInterfaceOnTip } from '../helpers/interface/triggers';
import { error, tip } from '../helpers/log';
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 = DONATION_SENDER ? DONATION_SENDER.toLowerCase() : null;
const message = DONATION_MESSAGE ? DONATION_MESSAGE : '';
const amount = Number(DONATION_AMOUNT);
const currency = DONATION_CURRENCY;

const id = username ? await global.users.getIdByName(username, false) : null;
if (id) {
global.db.engine.insert('users.tips', { id, amount, message, currency, timestamp: Date.now() });
}
if (await global.cache.isOnline()) {
await global.db.engine.increment('api.current', { key: 'tips' }, { value: parseFloat(global.currency.exchange(amount, currency, global.currency.mainCurrency)) });
}

if (await global.cache.isOnline()) {
await global.db.engine.increment('api.current', { key: 'tips' }, { value: parseFloat(global.currency.exchange(amount, currency, global.currency.mainCurrency)) });
}

global.overlays.eventlist.add({
type: 'tip',
amount,
currency,
username: username || 'Anonymous',
message,
timestamp: Date.now(),
});

tip(`${username ? username : 'Anonymous'}${id ? '#' + id : ''}, amount: ${amount}${DONATION_CURRENCY}, ${message ? 'message: ' + message : ''}`);

global.events.fire('tip', {
username: username || 'Anonymous',
amount,
currency,
amountInBotCurrency: parseFloat(global.currency.exchange(amount, currency, global.currency.mainCurrency)).toFixed(2),
currencyInBot: global.currency.mainCurrency,
message,
});

global.registries.alerts.trigger({
event: 'tips',
name: username || 'Anonymous',
amount,
currency,
monthsName: '',
message,
autohost: false,
});

triggerInterfaceOnTip({
username: username || 'Anonymous',
amount: amount,
message: message,
currency: currency,
timestamp: Date.now(),
});

}

}
}

export default Qiwi;
export { Qiwi };

0 comments on commit 4cd0312

Please sign in to comment.