-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (124 loc) · 3.03 KB
/
main.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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/kelseyhightower/envconfig"
"github.com/joho/godotenv"
"github.com/konafx/natalya/loop"
log "github.com/sirupsen/logrus"
)
type Command *discordgo.ApplicationCommand
type CommandHandler func(*discordgo.Session, *discordgo.InteractionCreate)
var (
s *discordgo.Session
commands []Command
commandHandlers map[string]CommandHandler = map[string]CommandHandler{}
handlers []interface{}
)
func addCommand(c Command, ch CommandHandler) {
commands = append(commands, c)
commandHandlers[c.Name] = ch
}
func addHandler(h ...interface{}){
handlers = append(handlers, h...)
}
type Env struct {
Guilds []string
BotToken string `split_words:"true"`
RemoveCommand bool `split_word:"true" default:"true"`
}
var env Env
func init() {
log.SetLevel(log.DebugLevel)
}
func init() {
if err := godotenv.Load(); err != nil {
log.Infoln(".env file not found")
}
}
func init() {
if err := envconfig.Process("discord", &env); err != nil {
log.Fatal(err.Error())
}
}
func init() {
var err error
s, err = discordgo.New("Bot " + env.BotToken)
if err != nil {
log.Fatal(err.Error())
}
}
func main() {
s.AddHandler(ready)
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.Data.Name]; ok {
h(s, i)
}
})
if err := s.Open(); err != nil {
fmt.Println(err)
return
}
defer s.Close()
log.Printf("%#v", s.State)
if len(env.Guilds) == 0 {
log.Debugf("Create global command")
for _, v := range commands {
log.Println(v)
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "", v)
if err != nil {
log.Fatalf("Cannot create '%+v' command: %v", err, v.Name)
}
v.ID = cmd.ID
}
}
type key struct {
k1, k2 string
}
appcmds := make(map[key]string)
for _, x := range env.Guilds {
log.Debugf("Create guild command")
for _, v := range commands {
log.Println(x, v)
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, x, v)
if err != nil {
log.Fatalf("Cannot create '%+v' command: %v", err, v.Name)
}
v.ID = cmd.ID
appcmds[key{x, v.Name}] = cmd.ID
}
}
for _, v := range handlers {
s.AddHandler(v)
}
fmt.Println("bot is running now. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
fmt.Println("Goodbye!")
if !env.RemoveCommand { return }
if len(env.Guilds) == 0 {
for _, v := range commands {
if err := s.ApplicationCommandDelete(s.State.User.ID, "", v.ID); err != nil {
log.Errorf("Skip delete cmd: %s (ID: %s)", v.Name, v.ID)
log.Error(err)
}
}
}
for _, x := range env.Guilds {
for _, v := range commands {
cmdID := appcmds[key{x, v.Name}]
if err := s.ApplicationCommandDelete(s.State.User.ID, x, cmdID); err != nil {
log.Errorf("Skip delete cmd: %s (ID: %s) on guild %s", v.Name, cmdID, x)
log.Error(err)
}
}
}
return
}
func ready(s *discordgo.Session, e *discordgo.Ready) {
// s.UpdateGameStatus(0, "Dancing!")
}