-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency.js
152 lines (130 loc) · 3.95 KB
/
currency.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
const request = require('request');
const currencyInfo = require('./currencyInfo.json');
const supportedCurrencies = [
'EUR',
'AUD',
'BGN',
'BRL',
'CAD',
'CHF',
'CNY',
'CZK',
'DKK',
'GBP',
'HKD',
'HRK',
'HUF',
'IDR',
'ILS',
'INR',
'JPY',
'KRW',
'MXN',
'MYR',
'NOK',
'NZD',
'PHP',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'THB',
'TRY',
'USD',
'ZAR'
];
const re = (() => {
const number = '(-?[0-9](,|[0-9])*(.[0-9]+)?) ?';
const aliases = [];
currencyInfo.aliasRegexes = {}
for (let key in currencyInfo.aliases) {
// Save the RegExp so we only make it once.
currencyInfo.aliasRegexes[key] = new RegExp(key, 'i');
aliases.push(key);
}
const currencies = `(${supportedCurrencies.join('|')}|${aliases.join('|')})`;
const regex = `${number} ?${currencies}( in ${currencies})?`;
return new RegExp(regex, 'gi');
})();
const findCurrency = (cur) => {
if (!cur) return null;
cur = cur.toUpperCase();
// If cur is already a currency, return it.
if (currencyInfo.names[cur]) return cur;
// Find the currency that corresponds to our alias.
return currencyInfo.aliases[Object.keys(currencyInfo.aliasRegexes).find(a => cur.match(currencyInfo.aliasRegexes[a]))] || null;
};
exports.match = (event, commandPrefix) => {
let match = re.exec(event.body);
re.lastIndex = 0;
return match !== null;
};
exports.run = (api, event) => {
// If no default currency has been specified, use NZD, 'cause it's cool.
if (!exports.config.defaultToCurrency) {
exports.config.defaultToCurrency = "NZD";
}
let match = re.exec(event.body),
amount = match[1],
fromCurrency = findCurrency(match[4]),
toCurrency = findCurrency(match[29]) || exports.config.defaultToCurrency;
re.lastIndex = 0;
// If we don't have a to currency in the config or message, yell at the user
// (it's always their fault).
if (!toCurrency) {
api.sendMessage("You don't seem to have a default to currency specified (either set one in the config.defaultToCurrency or specify it in the message)", event.thread_id);
return;
}
// This should never happen but I'm paranoid.
if (!amount || !fromCurrency) {
api.sendMessage("That looks wrong. You should try harder.", event.thread_id);
return;
}
getExchange()
.then(result => {
result = convert(fromCurrency, toCurrency, parseFloat(amount), result.rates);
//If we couldn't convert, give up
if (result.error) {
api.sendMessage(result.error, event.thread_id);
return;
}
api.sendMessage(`${amount} ${fromCurrency} is about ${result.result} ${toCurrency}`, event.thread_id);
}, error => {
//If we couldn't get the latest data, give up.
api.sendMessage(error, event.thread_id);
});
};
const convert = (f, to, amount, conversions) => {
f = f.toUpperCase();
to = to.toUpperCase();
if (!conversions[f]) {
return {
error: "Unsupported currency '" + f + "'"
};
}
if (!conversions[to]) {
return {
error: "Unsupported currency '" + to + "'"
};
}
let a = amount / conversions[f] * conversions[to];
a = Math.round(a * 100) / 100;
return {
result: a
};
};
const getExchange = () => {
return new Promise((accept, reject) => {
request.get('http://api.fixer.io/latest', function (error, response, body) {
if (response.statusCode === 200 && response.body) {
const result = JSON.parse(response.body);
// Add an entry for the Euro.
result.rates.EUR = 1;
accept(result);
} else {
reject("Couldn't talk to fixer.io for the exchange rate...");
}
});
});
}