-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency.go
75 lines (59 loc) · 1.9 KB
/
currency.go
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
package plugins
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
base "github.com/jriddick/geoffrey/bot"
"github.com/jriddick/geoffrey/irc"
"github.com/jriddick/geoffrey/msg"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
func init() {
base.RegisterHandler(CurrencyHandler)
}
// CurrencyHandler extracts title from posted links and sends
// them to the channel.
var CurrencyHandler = base.Handler{
Name: "Currency",
Description: "Currency converter.",
Event: irc.Message,
Run: func(bot *base.Bot, msg *msg.Message) (bool, error) {
// Get configuration
config := bot.Config()
client := http.Client{}
returnData := "Currency error, usage: !c <amount> <from> <to>"
// Check if channel message
if msg.Params[0] == config.Identification.Nick || msg.Prefix.Name == "nibbler" || msg.Prefix.Name == "geoffrey-bot" {
return false, nil
}
go func(bot *base.Bot, channel string) {
if strings.HasPrefix(msg.Trailing, "!c") {
if len(msg.Trailing) > 2 {
cData := strings.Split(msg.Trailing, " ")
if len(cData) == 4 {
url := fmt.Sprintf("https://api.exchangerate.host/latest?base=%s&amount=%s", cData[2], cData[1])
request, reqErr := http.NewRequest(http.MethodGet, url, nil)
if reqErr != nil {
log.Errorf("[currency] Failed NewRequest: %s", reqErr)
}
resp, respErr := client.Do(request)
if respErr != nil {
log.Errorf("[currency] Failed client.Do: %s", respErr)
}
cConvertTo := fmt.Sprintf("rates.%s", strings.ToUpper(cData[3]))
body, bodyErr := ioutil.ReadAll(resp.Body)
if bodyErr != nil {
log.Errorf("[currency] Failed ReadAll: %s", bodyErr)
}
cValue := gjson.GetBytes(body, cConvertTo)
returnData = fmt.Sprintf("%s %s in %s: %v", cData[1], cData[2], cData[3], cValue)
}
}
bot.Send(channel, returnData)
}
}(bot, msg.Params[0])
return true, nil
},
}