This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (75 loc) · 1.83 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
package main
import (
"dawn-bot/src/commands"
"dawn-bot/src/config"
"dawn-bot/src/db/postgres"
"dawn-bot/src/event"
"dawn-bot/src/utils"
"embed"
"fmt"
"github.com/bwmarrin/discordgo"
"os"
"os/signal"
"syscall"
)
//go:embed resources/config
var configs embed.FS
const (
intents = discordgo.IntentsAll
)
func main() {
var err error
utils.GlobalPath, err = os.Executable()
utils.PanicError(err)
// generate static configs file
generateConfigs()
loadConfigs()
// Migrate the database
postgres.Migrate()
// Generate the websocket
if len(os.Args) != 2 {
println("You must provide only the token")
return
}
token := os.Args[1]
client, err := discordgo.New("Bot " + token)
utils.PanicError(err)
// Handle classical event
client.AddHandler(event.HandleJoin)
// Add used intents
client.Identify.Intents = discordgo.MakeIntent(intents)
// Connect the bot
err = client.Open()
commands.Init(client)
client.AddHandler(commands.GlobalHandler)
utils.PanicError(err)
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close down the Discord session.* and delete the slash commands
commands.Remove(client)
client.Close()
}
func generateConfigs() {
err := os.Mkdir("config", 0777)
if err != nil {
println(err.Error())
}
if !utils.FileExist("config/databases.toml") {
println("the file do not exist")
file, err := os.Create("config/databases.toml")
utils.PanicError(err)
content, err := configs.ReadFile("resources/config/databases.toml")
utils.PanicError(err)
_, err = file.Write(content)
utils.PanicError(err)
err = file.Close()
utils.PanicError(err)
}
}
func loadConfigs() {
databases := config.LoadAndImportDatabaseConfig()
databases.Postgres.Connect()
databases.Redis.Connect()
}