-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
91 lines (66 loc) · 1.56 KB
/
main.go
File metadata and controls
91 lines (66 loc) · 1.56 KB
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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"time"
dg "github.com/bwmarrin/discordgo"
)
var bot_token string
var remove_commands bool = false
var start_time time.Time
func init() {
bot_token = os.Getenv("BOT_TOKEN")
start_time = time.Now()
flag.BoolVar(&remove_commands, "rmcmd", false, "Remove commands")
flag.Parse()
}
// Just init the bot.
func main() {
// Bot auth and login
if bot_token == "" {
fmt.Println("No token provided")
os.Exit(1)
}
bot, err := dg.New("Bot " + bot_token)
if err != nil {
panic(err)
}
bot.Identify.Intents = 1536
err = bot.Open()
check_error(err)
defer bot.Close()
// Handlers
bot.AddHandler(receive_message)
// Create commands
log.Println("Adding commands...")
registeredCommands := make([]*dg.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := bot.ApplicationCommandCreate(bot.State.User.ID, "", v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}
bot.AddHandler(func(s *dg.Session, i *dg.InteractionCreate) {
if h, ok := command_handlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
go clear_sessions()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Println("Press Ctrl+C to exit")
<-stop
if remove_commands {
log.Println("Removing commands...")
for _, v := range registeredCommands {
err := bot.ApplicationCommandDelete(bot.State.User.ID, "", v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
}
}