/
discord.go
205 lines (181 loc) · 5.11 KB
/
discord.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package discord
import (
"fmt"
"os"
"sync"
"time"
"github.com/bigopenworld/discord-bot/cmd"
"github.com/bigopenworld/discord-bot/config"
"github.com/bigopenworld/discord-bot/database"
"github.com/bigopenworld/discord-bot/structure"
"github.com/bwmarrin/discordgo"
)
var BotVar *BotStruct
type BotStruct struct {
sync.RWMutex
restarted int
session *discordgo.Session
cache BotCache
}
// constructor
func CreateAndStart() bool {
BotVar = new(BotStruct)
BotVar.start()
return true
}
// start func
func (bot *BotStruct) start() bool {
fmt.Println(cmd.NewFlag(cmd.OK), "Bot Starting ... Locking Bot struct")
bot.Lock()
fmt.Println(cmd.NewFlag(cmd.OK),"Bot Starting ... 1 of 3 : Bot init and connection")
err := bot.connect()
if err != 0 {
fmt.Println(cmd.NewFlag(cmd.FATAL),"Bot Starting failed ... Unlocking Bot struct")
bot.Unlock()
bot.tryrestartorkill(err, false)
return true
}
fmt.Println(cmd.NewFlag(cmd.OK),"Bot Starting ... 2 of 3 : Bot cache starting")
if config.Cache {
bot.cache = NewBotCache()
fmt.Println(cmd.NewFlag(cmd.OK),"Bot Starting ... 1 of 2 : Internal cache starting")
internalcacheresult := bot.cache.init()
if !internalcacheresult {
fmt.Println(cmd.NewFlag(cmd.FATAL),"Bot Starting failed ... Unlocking Bot struct")
bot.Unlock()
bot.tryrestartorkill(5, false)
return true
}
fmt.Println(cmd.NewFlag(cmd.OK),"Bot Starting ... 2 of 2 : Cooldown cache starting")
cooldowncacheresult := bot.cache.initcooldown()
if !cooldowncacheresult {
fmt.Println(cmd.NewFlag(cmd.FATAL),"Bot Starting failed ... Unlocking Bot struct")
bot.Unlock()
bot.tryrestartorkill(5, false)
return true
}
} else {
fmt.Println(cmd.NewFlag(cmd.WARNING),"Cache disabled ... skiping")
}
fmt.Println(cmd.NewFlag(cmd.OK),"Bot Starting ... 3 of 3 : DataBase connection init")
if config.DBenabled {
dbstatus := database.Connect()
if !dbstatus {
fmt.Println(cmd.NewFlag(cmd.FATAL),"Bot Starting failed ... Unlocking Bot struct")
bot.Unlock()
bot.tryrestartorkill(7, false)
return true
}
dbcheck := database.Test()
if !dbcheck {
fmt.Println(cmd.NewFlag(cmd.FATAL),"Bot Starting failed ... Unlocking Bot struct")
bot.Unlock()
bot.tryrestartorkill(7, false)
return true
}
} else {
fmt.Println(cmd.NewFlag(cmd.WARNING),"Database disabled ... skiping")
}
fmt.Println(cmd.NewFlag(cmd.INFO),"Bot Starting ... Unlocking Bot struct")
bot.Unlock()
fmt.Println(cmd.NewFlag(cmd.SUCCESS),"All done !")
bot.fillcache()
if config.RestartReset {
bot.restarted = 0
}
return true
}
func (bot *BotStruct) Shutdown() {
bot.tryrestartorkill(0, true)
}
// restarter or killer
func (bot *BotStruct) tryrestartorkill(code int, kill bool) {
if kill {
bot.session.Close()
fmt.Println(cmd.NewFlag(cmd.SUCCESS),"Bot killed nicely & peacefully")
os.Exit(code)
}
bot.restarted++
fmt.Println(cmd.NewFlag(cmd.ERROR),"Bot Error Occured ... Restart ", bot.restarted, " of ", config.MaxRestart)
if bot.restarted < config.MaxRestart {
bot.session.Close()
time.Sleep(config.RestartWait)
bot.start()
return
} else {
os.Exit(code)
}
}
// connect
func (bot *BotStruct) connect() int {
discord, err := discordgo.New("Bot " + config.Token)
if err != nil {
return 3
}
bot.session = discord
bot.session.Identify.Intents = discordgo.IntentsAllWithoutPrivileged
// Handlers
discord.AddHandler(MessageCreate)
// End Handlers
err = bot.session.Open()
if err != nil {
return 4
}
return 0
}
/*
Cache Part
*/
// Get the cache struct
func (bot *BotStruct) GetCache() *BotCache {
return &bot.cache
}
// fill the bot cache to maximum => Begin with Guilds / Config / Users
func (bot *BotStruct) fillcache() bool {
bot.Lock()
fmt.Println(cmd.NewFlag(cmd.OK),"Init 1 of 1 ... Filling cache")
listoftest := splitdata(bot.session.State.Guilds)
var wg sync.WaitGroup
for pid, list := range listoftest {
wg.Add(1)
go fillcacheprocess(pid, list, bot, &wg)
}
wg.Wait()
fmt.Println(cmd.NewFlag(cmd.SUCCESS),"Init Done ... All done")
bot.Unlock()
//client.session.State.RUnlock()
return true
}
//
func fillcacheprocess(pid int, list []*discordgo.Guild, client *BotStruct, wg *sync.WaitGroup) {
defer wg.Done()
var total = len(list)
var done int
for _, guild := range list {
done++
//fmt.Println(guild.ID)
//guild_fetch, err := client.session.Guild(guild.ID)
BotVar.cache.SetGuild(structure.Guild{
ID : guild.ID,
DiscGuild: *guild,
})
/*if err != nil {
log.Panic("Error when filling cache")
}*/
//fmt.Println(guild.ID)
//client.cache.GuildCache.Set(guild.ID, guild.ApproximateMemberCount, cache.DefaultExpiration )
//client.cache.guildmembersonline.Set(guild.ID, guild.ApproximateMemberCount, cache.DefaultExpiration )
//log.Println("Process",pid ," : guild ", done, " / ",total)
}
fmt.Println(cmd.NewFlag(cmd.INFO),"Process", pid, " : guild ", done, " / ", total)
}
// DataSpliter
func splitdata(list []*discordgo.Guild) [config.Chunk + 1][]*discordgo.Guild {
var listlen = len(list)
var part int = listlen / config.Chunk
var res [config.Chunk + 1][]*discordgo.Guild
for i, elem := range list {
res[i/part] = append(res[i/part], elem)
}
return res
}