-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdoom.go
58 lines (53 loc) · 1.32 KB
/
doom.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
package module
import (
"bytes"
"encoding/json"
"fmt"
"github.com/davidscholberg/irkbot/lib/configure"
"github.com/davidscholberg/irkbot/lib/message"
"os"
"strings"
)
type doomStruct struct {
Type string `json:"type"`
}
var doomValids = []string{"shoot", "forward", "backward", "left", "right", "use"}
func helpDoom() []string {
s := "doom <command> - play doom!"
return []string{s}
}
func doom(cfg *configure.Config, in *message.InboundMsg, actions *actions) {
if len(in.MsgArgs[1:]) == 0 {
actions.say("enter a command plz")
return
}
doomCommand := strings.Join(in.MsgArgs[1:], " ")
doomValid := false
for _, v := range doomValids {
if doomCommand == v {
doomValid = true
break
}
}
if !doomValid {
actions.say(fmt.Sprintf("invalid command, commands are "+"%s", strings.Join(doomValids, ", ")))
return
}
doomToPost := doomStruct{Type: doomCommand}
jsonValue, err := json.Marshal(doomToPost)
if err != nil {
// handle err
fmt.Fprintln(os.Stderr, err)
actions.say("something borked, try again")
return
}
doomHost := cfg.Modules["doom"]["doom_host"]
response, err := actions.httpPost(doomHost, "application/json", bytes.NewReader(jsonValue))
if err != nil {
// handle err
fmt.Fprintln(os.Stderr, err)
actions.say("something borked, try again")
return
}
defer response.Body.Close()
}