-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (85 loc) · 1.95 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/yapms/yapbot/poll"
"github.com/yapms/yapbot/yesno"
)
var (
Token string
GuildID string
Discord *discordgo.Session
commands = []*discordgo.ApplicationCommand{
poll.GetCommand(),
yesno.GetCommand(),
}
commandHandlers = map[string]func(session *discordgo.Session, interaction *discordgo.InteractionCreate){
"poll": poll.GetHandler(),
"yesno": yesno.GetHandler(),
}
)
func parseArguments() {
flag.StringVar(&Token, "t", "", "Bot Token")
flag.StringVar(&GuildID, "g", "", "Guild Token")
flag.Parse()
}
func initializeDiscord() {
var err error
Discord, err = discordgo.New("Bot " + Token)
if err != nil {
log.Panic(err)
return
}
Discord.Open()
Discord.Identify.Intents = discordgo.IntentGuildMessages
}
func createCommands() {
for _, command := range commands {
fmt.Printf("creating command: %+v\n", command)
_, err := Discord.ApplicationCommandCreate(Discord.State.User.ID, GuildID, command)
if err != nil {
log.Panic(err)
}
}
}
func addHandler() {
Discord.AddHandler(func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
name := interaction.ApplicationCommandData().Name
command, okay := commandHandlers[name]
if okay {
command(session, interaction)
}
})
}
func cleanupCommands() {
commands, err := Discord.ApplicationCommands(Discord.State.User.ID, GuildID)
if err != nil {
log.Panic(err)
}
for _, command := range commands {
err = Discord.ApplicationCommandDelete(Discord.State.User.ID, GuildID, command.ID)
if err != nil {
log.Panic(err)
}
}
}
func cleanup() {
cleanupCommands()
Discord.Close()
}
func main() {
parseArguments()
initializeDiscord()
createCommands()
addHandler()
fmt.Println("Bot is now running. Press CTRL-C to exit.")
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-stop
cleanup()
}