-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (84 loc) · 2.49 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
package main
import (
"log"
"os"
"os/signal"
"github.com/Meonako/PhoenixManager/commands"
"github.com/Meonako/PhoenixManager/components"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var (
Token string
AppID string
session *discordgo.Session
)
func init() {
err := godotenv.Load("config.env")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
Token = os.Getenv("BotToken")
AppID = os.Getenv("AppID")
if Token == "" {
log.Fatal("No bot token provided")
}
if AppID == "" {
log.Fatal("No app ID provided")
}
session, err = discordgo.New("Bot " + Token)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
}
func main() {
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
var commander *discordgo.User // IGNORE THIS VARIABLE NAME. ITS FOR THE MEMES
if i.User != nil {
commander = i.User
} else {
commander = i.Member.User
}
log.Printf("%v#%v issued : %v", commander.Username, commander.Discriminator, i.ApplicationCommandData().Name)
if h, ok := commands.CommandsHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
case discordgo.InteractionMessageComponent:
if h, ok := components.ComponentsHandler[i.MessageComponentData().CustomID]; ok {
h(s, i)
}
}
})
for _, command := range commands.CommandsList {
_, err := session.ApplicationCommandCreate(AppID, "", command)
if err != nil {
log.Panicf("Cannot create slash command: %v", err)
}
log.Printf("Registered command: %v\n", command.Name)
}
err := session.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
log.Println("Ready! Press CTRL + C to exit properly!")
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Cleaning up...")
registeredCommands, err := session.ApplicationCommands(AppID, "")
if err != nil {
log.Fatalf("Could not fetch registered commands: %v\n", err)
}
for _, v := range registeredCommands {
err := session.ApplicationCommandDelete(AppID, "", v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
log.Printf("Deleted '%v' command\n", v.Name)
}
}