This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
198 lines (192 loc) · 6.49 KB
/
commands.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// commands.go
// Maya commands and handlers
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
var commands = []*discordgo.ApplicationCommand{
{
Name: "hello-maya",
Description: "Says hello to Maya.",
},
{
Name: "describe-instances",
Description: "Get a list of all instances manageable by Maya.",
},
{
Name: "describe-instance-status",
Description: "Get an instance's reachability and system health status.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "instance-id",
Description: "Instance ID",
Required: true,
},
},
},
{
Name: "start-instance",
Description: "Start a specified instance.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "instance-id",
Description: "Instance ID",
Required: true,
},
},
},
{
Name: "stop-instance",
Description: "Stop a specified instance.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "instance-id",
Description: "Instance ID",
Required: true,
},
},
},
}
var commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"hello-maya": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hello, I'm Maya. 👋",
},
})
},
"describe-instances": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
describeInstancesOutput, err := describeInstances()
if err != nil {
fmt.Println(err)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Could not describe instances.",
},
})
return
}
instanceList := describeInstancesOutput.Reservations
var results string
results += "```"
for _, j := range instanceList {
for _, k := range j.Instances[0].Tags {
if *k.Key == "Name" {
results += *k.Value
}
}
results += "\t"
results += *j.Instances[0].InstanceId
results += "\t"
results += *j.Instances[0].State.Name
results += "\n"
}
results += "```"
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: results,
},
})
},
"describe-instance-status": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
describeInstanceStatusOutput, err := describeInstanceStatus(optionMap["instance-id"].StringValue())
if err != nil {
fmt.Println("error calling describeInstanceStatus")
return
}
if len(describeInstanceStatusOutput.InstanceStatuses) != 1 {
fmt.Println("No instance found matching the provided ID")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "No status available for `" + optionMap["instance-id"].StringValue() + "`.",
},
})
return
}
instanceStatus := describeInstanceStatusOutput.InstanceStatuses[0]
var results string
results += "Status for `" + *instanceStatus.InstanceId + "`:"
results += "```"
results += "State: " + *instanceStatus.InstanceState.Name
results += "\n"
results += "Instance Reachability: " + *instanceStatus.InstanceStatus.Status
results += "\n"
if instanceStatus.InstanceStatus.Details[0].ImpairedSince != nil {
results += "\tImpaired: " + instanceStatus.InstanceStatus.Details[0].ImpairedSince.GoString()
results += "\n"
}
results += "System Health: " + *instanceStatus.SystemStatus.Status
if instanceStatus.SystemStatus.Details[0].ImpairedSince != nil {
results += "\n"
results += "\tImpaired: " + instanceStatus.SystemStatus.Details[0].ImpairedSince.GoString()
}
results += "```"
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: results,
},
})
},
"start-instance": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
_, err := startInstance(optionMap["instance-id"].StringValue())
if err != nil {
fmt.Println("error calling startInstance")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Could not start `" + optionMap["instance-id"].StringValue() + "`.",
},
})
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Starting instance `" + optionMap["instance-id"].StringValue() + "`...",
},
})
},
"stop-instance": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
_, err := stopInstance(optionMap["instance-id"].StringValue())
if err != nil {
fmt.Println("error calling stopInstance")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Could not stop `" + optionMap["instance-id"].StringValue() + "`.",
},
})
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Stopping instance `" + optionMap["instance-id"].StringValue() + "`...",
},
})
},
}