-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (136 loc) · 3.72 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"database/sql"
libroberto "github.com/TheTipo01/libRoberto"
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/lit"
"github.com/kkyr/fig"
_ "modernc.org/sqlite"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
)
type config struct {
Token string `fig:"token" validate:"required"`
LogLevel string `fig:"loglevel" validate:"required"`
Voice string `fig:"voice" validate:"required"`
}
var (
// Discord bot token
token string
// Server
server = make(map[string]*Server)
// DB connection
db *sql.DB
// Discord bot session
s *discordgo.Session
)
func init() {
lit.LogLevel = lit.LogError
var cfg config
err := fig.Load(&cfg, fig.File("config.yml"))
if err != nil {
lit.Error(err.Error())
return
}
libroberto.Voice = cfg.Voice
token = cfg.Token
// Set lit.LogLevel to the given value
switch strings.ToLower(cfg.LogLevel) {
case "logwarning", "warning":
lit.LogLevel = lit.LogWarning
case "loginformational", "informational":
lit.LogLevel = lit.LogInformational
case "logdebug", "debug":
lit.LogLevel = lit.LogDebug
}
// Database
db, err = sql.Open(driverName, dataSourceName)
if err != nil {
lit.Error("Error opening database connection, %s", err)
return
}
execQuery(tblCustomCommands, db)
loadCustomCommands(db)
}
func main() {
if token == "" {
lit.Error("No token provided. Please modify config.yml")
return
}
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + token)
if err != nil {
lit.Error("Error creating Discord session: %s", err)
return
}
// Add events handler
dg.AddHandler(ready)
dg.AddHandler(guildCreate)
dg.AddHandler(guildDelete)
dg.AddHandler(voiceStateUpdate)
// Add commands handler
dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.User == nil {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
})
// We set the intents that we use
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuilds | discordgo.IntentsGuildVoiceStates)
// Open the websocket and begin listening.
err = dg.Open()
if err != nil {
lit.Error("Error opening Discord session: %s", err)
return
}
// Save the session
s = dg
// Register commands
_, err = dg.ApplicationCommandBulkOverwrite(dg.State.User.ID, "", commands)
if err != nil {
lit.Error("Can't register commands, %s", err)
}
// Wait here until CTRL-C or another term signal is received.
lit.Info("roberto 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.
_ = dg.Close()
_ = db.Close()
}
func ready(s *discordgo.Session, _ *discordgo.Ready) {
// Set the playing status.
err := s.UpdateGameStatus(0, "Serving "+strconv.Itoa(len(s.State.Guilds))+" guilds!")
if err != nil {
lit.Error("Can't set status, %s", err)
}
}
func guildCreate(s *discordgo.Session, e *discordgo.GuildCreate) {
initializeServer(e.Guild.ID)
ready(s, nil)
}
func guildDelete(s *discordgo.Session, _ *discordgo.GuildDelete) {
ready(s, nil)
}
// Update the voice channel when the bot is moved
func voiceStateUpdate(s *discordgo.Session, v *discordgo.VoiceStateUpdate) {
// If the bot is moved to another channel
if v.UserID == s.State.User.ID && server[v.GuildID].IsPlaying() {
if v.ChannelID == "" {
// If the bot has been disconnected from the voice channel, reconnect it
var err error
server[v.GuildID].vc, err = s.ChannelVoiceJoin(v.GuildID, server[v.GuildID].voiceChannel, false, true)
if err != nil {
lit.Error("Can't join voice channel, %s", err)
}
} else {
// Update the voice channel
server[v.GuildID].voiceChannel = v.ChannelID
}
}
}