-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.go
140 lines (121 loc) · 4.26 KB
/
update.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
package slash
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/commands/slash/permission"
"github.com/ritsec/ops-bot-iii/helpers"
"github.com/ritsec/ops-bot-iii/logging"
"github.com/sirupsen/logrus"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
func Update() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *discordgo.InteractionCreate)) {
return &discordgo.ApplicationCommand{
Name: "update",
Description: "Update the bot",
DefaultMemberPermissions: &permission.Admin,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "force",
Description: "Force the bot to reboot",
Type: discordgo.ApplicationCommandOptionBoolean,
Required: false,
},
},
},
func(s *discordgo.Session, i *discordgo.InteractionCreate) {
span := tracer.StartSpan(
"commands.slash.update:Update",
tracer.ResourceName("/update"),
)
defer span.Finish()
logging.Debug(s, "Update command received", i.Member.User, span)
force := false
if len(i.ApplicationCommandData().Options) != 0 {
force = i.ApplicationCommandData().Options[0].BoolValue()
}
update, err := helpers.UpdateMainBranch()
if err != nil {
logging.Error(s, "Error updating main branch", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
if !update {
logging.Debug(s, "No update available", i.Member.User, span)
if !force {
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "No update available\nIf you want to force an update, use `/update force`",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "Error responding to interaction", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
return
} else {
logging.Debug(s, "Forcing update", i.Member.User, span)
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "No update available; forcing update\nBot will be up temporarily once done updating",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "Error responding to interaction", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
err = helpers.BuildOBIII()
if err != nil {
logging.Error(s, "Error building OBIII", i.Member.User, span, logrus.Fields{"err": err.Error()})
content := fmt.Sprintf("Error building OBIII\n\nError:\n%s", err.Error())
_, err = s.InteractionResponseEdit(
i.Interaction,
&discordgo.WebhookEdit{
Content: &content,
},
)
if err != nil {
logging.Error(s, "Error editing interaction response", i.Member.User, span, logrus.Fields{"err": err.Error()})
}
return
}
err = helpers.Exit()
if err != nil {
logging.Error(s, "Error exiting", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
return
}
}
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Update available; restarting now\nBot will be up temporarily once done updating",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "Error responding to interaction", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
err = helpers.BuildOBIII()
if err != nil {
content := fmt.Sprintf("Error building OBIII\n\nError:\n%s", err.Error())
_, err = s.InteractionResponseEdit(
i.Interaction,
&discordgo.WebhookEdit{
Content: &content,
},
)
logging.Error(s, "Error building OBIII", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
err = helpers.Exit()
if err != nil {
logging.Error(s, "Error exiting", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
}
}