/
instruction_handler.go
104 lines (95 loc) · 2.71 KB
/
instruction_handler.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
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
package handler
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/mustansirzia/simcord/discord"
"github.com/mustansirzia/simcord/parser"
)
var discordClient discord.Client
func init() {
discordConfig, err := discord.NewConfig()
if err != nil {
log.Fatal(err)
}
discordClient, err = discord.NewClient(discordConfig)
if err != nil {
log.Fatal(err)
}
}
// HandleInstruction - Function which does the actual handling of the instruction
// and resolves an instruction to a string response which can be sent to the client.
func HandleInstruction(instruction *parser.Instruction) (string, error) {
if instruction.Type == parser.GET {
return handleGetInstruction(instruction)
}
if instruction.Type == parser.SEND {
return "", handleSendInstruction(instruction)
}
return "", nil
}
func handleGetInstruction(instruction *parser.Instruction) (string, error) {
switch instruction.Name {
case parser.GUILDS:
if len(instruction.ResourceID) > 0 {
channels, err := discordClient.Channels(instruction.ResourceID)
if err != nil {
return "", err
}
return convertResourceSliceToString(channels), nil
}
guilds, err := discordClient.Guilds()
if err != nil {
return "", err
}
return convertResourceSliceToString(guilds), nil
case parser.CHANNELS:
count := 10
if len(instruction.Arg) > 0 {
countInt, err := strconv.Atoi(instruction.Arg)
if err == nil {
count = countInt
}
}
messages, err := discordClient.Messages(instruction.ResourceID, count)
if err != nil {
return "", err
}
return convertResourceSliceToString(messages), nil
}
return "", nil
}
func handleSendInstruction(instruction *parser.Instruction) error {
switch instruction.Name {
case parser.CHANNELS:
err := discordClient.SendMessage(instruction.ResourceID, instruction.Arg)
if err != nil {
return err
}
}
return nil
}
func convertResourceSliceToString(resources interface{}) string {
var resourcesAsString []string
switch resourcesSlice := resources.(type) {
case []discord.Guild:
resourcesAsString = make([]string, 0, len(resourcesSlice))
for _, guild := range resourcesSlice {
resourcesAsString = append(resourcesAsString, fmt.Sprintf("%s", guild))
}
case []discord.Channel:
resourcesAsString = make([]string, 0, len(resourcesSlice))
for _, channel := range resourcesSlice {
resourcesAsString = append(resourcesAsString, fmt.Sprintf("%s", channel))
}
case []discord.Message:
resourcesAsString = make([]string, 0, len(resourcesSlice))
for _, message := range resourcesSlice {
resourcesAsString = append(resourcesAsString, fmt.Sprintf("%s", message))
}
default:
log.Fatal("Unknown argument given to convertResourceSliceToString")
}
return strings.Join(resourcesAsString, "\n")
}