-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
106 lines (90 loc) · 2.3 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
package main
import (
"errors"
"io/fs"
"os"
"os/signal"
"strings"
"syscall"
"github.com/LightningDev1/LightningBot-Free/commands"
"github.com/LightningDev1/LightningBot-Free/config"
"github.com/LightningDev1/LightningBot-Free/events"
"github.com/LightningDev1/LightningBot-Free/utils"
"github.com/LightningDev1/dgc"
"github.com/LightningDev1/discordgo"
)
func main() {
utils.Console.PrintBanner()
err := utils.File.CreateDirectories()
if err != nil {
utils.Logging.Error("Error creating directories:", err)
os.Exit(1)
}
cfg, err := config.Load()
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
utils.Logging.Info("Config doesn't exist, starting setup...")
cfg = setup()
} else {
utils.Logging.Error("Error loading config:", err)
os.Exit(1)
}
}
session, _ := discordgo.New(cfg.Token)
session.SetIdentify(discordgo.IdentifyWeb)
router := dgc.Create(&dgc.Router{
PrefixFunc: func() []string {
cfg, err := config.Load()
if err != nil {
return []string{"!"}
}
return []string{cfg.CommandPrefix}
},
IgnorePrefixCase: true,
IsUserAllowedFunc: func(ctx *dgc.Ctx) bool {
return ctx.Event.Author.ID == session.State.User.ID
},
Commands: []*dgc.Command{},
Middlewares: []dgc.Middleware{},
})
// Register commands and events
events.Events.Session = session
events.Events.Router = router
events.Events.Register()
commands.Commands.Session = session
commands.Commands.Router = router
commands.Commands.Register()
router.Initialize(session)
err = session.Open()
if err != nil {
if strings.Contains(err.Error(), "Authentication failed") {
utils.Logging.Error("Your token is invalid! Starting setup...")
setup()
main()
return
} else {
utils.Logging.Error(err)
}
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
<-sc
}
func setup() config.Config {
token := utils.Input.GetString("Please enter your Discord token")
commandPrefix := utils.Input.GetString("Please enter your desired command prefix")
cfg := config.Config{
Token: token,
CommandPrefix: commandPrefix,
Embed: config.EmbedConfig{
Title: "LightningBot Free",
Footer: "LightningBot Free $VERSION",
},
}
err := cfg.Save()
if err != nil {
utils.Logging.Error("Error saving config:", err)
os.Exit(1)
}
return cfg
}