-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (92 loc) · 2.61 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
package main
import (
"encoding/json"
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
const nanceEndpoint = "https://api.nance.app/"
var s *discordgo.Session
type config struct {
Space string `json:"space"`
GuildId string `json:"guildId"`
}
func main() {
initDb()
os.Exit(0)
// Read env variables and create discord session
_, err := os.Stat(".env")
if !os.IsNotExist(err) {
if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file: %v\n", err)
}
}
if discordToken := os.Getenv("DISCORD_TOKEN"); discordToken == "" {
log.Fatalf("DISCORD_TOKEN is not set in environment or .env")
}
s, err = discordgo.New("Bot " + os.Getenv("DISCORD_TOKEN"))
if err != nil {
log.Fatalf("Error creating Discord session: %v\n", err)
}
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
// Add slash command handlers
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
err = s.Open()
if err != nil {
log.Fatalf("Error opening Discord session: %v\n", err)
}
defer s.Close()
// Read config file
f, err := os.Open("config.json")
if err != nil {
log.Fatalf("Error opening config file: %v\n", err)
}
var config []config
json.NewDecoder(f).Decode(&config)
// Add spaces for each item in the config file
for _, conf := range config {
go addSpace(conf.Space, conf.GuildId)
}
// Block until the user interrupts the program
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt, syscall.SIGHUP)
<-sc
log.Println("Shutting down...")
}
// TODO: WIP
func addSpace(space string, guildId string) {
spaceData, err := nanceSpace(space)
if err != nil {
log.Printf("Error fetching space data for %s (skipping guild): %v\n", space, err)
return
}
// Register slash commands
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, command := range commands {
reg, err := s.ApplicationCommandCreate(s.State.User.ID, guildId, command)
if err != nil {
log.Printf("Error registering command %s in guild %s (skipping guild): %v\n", command.Name, guildId, err)
return
}
registeredCommands[i] = reg
}
// Defer command deletion
defer func() {
for _, v := range registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, v.GuildID, v.ID)
if err != nil {
log.Panicf("Error deleting command '%s' from guild %s: %v\n", v.Name, guildId, err)
}
}
}()
log.Println(spaceData)
}