This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
58 lines (49 loc) · 1.69 KB
/
main.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
'use strict';
const ConvertBase = require('./ConvertBase');
const ConvertTemp = require('./ConvertTemp');
const Qty = require('js-quantities');
class Conversion extends global.AKP48.pluginTypes.MessageHandler {
constructor(AKP48) {
super(AKP48, 'Conversion');
}
}
Conversion.prototype.handleCommand = function (context) {
global.logger.silly(`${this.name}: Received command.`);
var text = context.rawArgs();
var command = context.command();
var responses = [];
if(typeof ConvertBase[command] === 'function') {
global.logger.silly(`${this.name}: Responding to ${command} command.`);
for (var i = 0; i < text.length; i++) {
responses.push(`${text[i]} => ${ConvertBase[command](text[i])}`);
}
return context.reply(responses.join(', '));
}
if(typeof ConvertTemp[command] === 'function') {
global.logger.silly(`${this.name}: Responding to ${command} command.`);
for (var i = 0; i < text.length; i++) {
responses.push(`${ConvertTemp[command](text[i])}`);
}
return context.reply(responses.join(', '));
}
//all-in-one solution time.
if(command === 'convert') {
global.logger.silly(`${this.name}: Responding to convert command.`);
var to = text[0];
text.shift();
try {
for (var i = 0; i < text.length; i++) {
var q = new Qty(text[i]);
if(q.isCompatible(to)) {
responses.push(`${q.toPrec(0.01).toString()} => ${q.to(to).toPrec(0.01).toString()}`);
} else {
responses.push(`Incompatible units: ${q.toPrec(0.01).toString()}`);
}
}
} catch(e) {
responses.push(`Error! ${e.message}.`);
}
return context.reply(responses.join(', '));
}
};
module.exports = Conversion;