-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserveralive.go
146 lines (127 loc) · 3.49 KB
/
serveralive.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"github.com/bwmarrin/discordgo"
_ "github.com/joho/godotenv/autoload"
)
// Bot parameters
var (
GuildID = ""
ChannelID = ""
BotToken = ""
RemoveCommands = true
)
var s *discordgo.Session
var (
dmPermission = true
commands = []*discordgo.ApplicationCommand{
{
Name: "ip",
Description: "Get server public IP",
DMPermission: &dmPermission,
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"ip": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: cuteIpResponse(),
},
})
},
}
)
func getIPAddress() (string, error) {
resp, err := http.Get("http://ifconfig.me/ip")
if err != nil {
return "", err
}
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Convert the response body to a string and return it
return string(body), nil
}
func cuteIpResponse() string {
ip, err := getIPAddress()
if err != nil {
return fmt.Sprintf("Cannot determine server IP:\n```\n%s\n```", err)
} else {
return fmt.Sprintf("My IP is `%s`", ip)
}
}
func init() {
// Init env
GuildID = os.Getenv("DC_GUILD_ID")
ChannelID = os.Getenv("DC_CHANNEL_ID")
BotToken = os.Getenv("DC_TOKEN")
if BotToken == "" {
log.Fatalln("Bot token cannot be empty")
}
}
func main() {
var err error
s, err = discordgo.New("Bot " + BotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
if ChannelID != "" {
_, err := s.ChannelMessageSend(ChannelID, "Server is up\n"+cuteIpResponse())
if err != nil {
log.Println("Cannot sent start up message: ", err)
}
}
})
err = s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}
defer s.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Println("Press Ctrl+C to exit")
<-stop
if RemoveCommands {
log.Println("Removing commands...")
// // We need to fetch the commands, since deleting requires the command ID.
// // We are doing this from the returned commands on line 375, because using
// // this will delete all the commands, which might not be desirable, so we
// // are deleting only the commands that we added.
// registeredCommands, err := s.ApplicationCommands(s.State.User.ID, *GuildID)
// if err != nil {
// log.Fatalf("Could not fetch registered commands: %v", err)
// }
for _, v := range registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, GuildID, v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
}
log.Println("Gracefully shutting down.")
}