-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
78 lines (63 loc) · 1.81 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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/ajay340/Discord-ChatGPT/discord"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var (
botSession *discordgo.Session
registeredCommands []*discordgo.ApplicationCommand
)
func init() {
err := godotenv.Load()
if err != nil {
log.Println("Error loading .env file")
}
var bot_token string = os.Getenv("BOT_TOKEN")
botSession, err = discordgo.New("Bot " + bot_token)
if err != nil {
log.Fatalln("error creating Discord session,", err)
return
}
}
func addCommands(session *discordgo.Session) {
log.Println("Adding commands...")
registeredCommands = make([]*discordgo.ApplicationCommand, len(discord.COMMANDS))
for i, command := range discord.COMMANDS {
cmd, err := botSession.ApplicationCommandCreate(botSession.State.User.ID, "", command)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", command.Name, err)
}
registeredCommands[i] = cmd
}
}
func removeCommands(session *discordgo.Session) {
for _, command := range registeredCommands {
err := session.ApplicationCommandDelete(session.State.User.ID, "", command.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", command.Name, err)
}
}
}
func main() {
botSession.AddHandler(discord.CommandInteractions)
err := botSession.Open()
if err != nil {
log.Fatalln("error opening connection,", err)
}
botSession.UpdateListeningStatus("/chat")
addCommands(botSession)
botSession.Identify.Intents = discordgo.IntentsGuildMessages
defer botSession.Close()
// Wait here until CTRL-C or other term signal is received.
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, os.Kill)
<-stop
removeCommands(botSession)
}