-
Notifications
You must be signed in to change notification settings - Fork 7
/
old_main.go
431 lines (372 loc) · 12.6 KB
/
old_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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"gopkg.in/yaml.v2"
"image"
"image/color"
"image/draw"
"image/png"
"io/ioutil"
"log"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
type Config struct {
BotToken string `yaml:"BotToken"`
InviteLink string `yaml:"InviteLink"`
AutoKickOnServer map[string]string `yaml:"AutoKickOnServer"`
SpamChannel string `yaml:"SpamChannel"`
Admins map[string]string `yaml:"Admins"`
Colors map[string]int `yaml:"Colors"`
}
type Roles struct {
ID string
Name string
}
var (
config Config
RoleNames []string
CreatedRoles = map[string]map[string]Roles{}
FirstTime = true
HelpText = `Help for Color-Bot
<<PrintColors https://nayu.moe/colors
<<NewColor "Assign a random color to the current user"
<<NewColor ColorName "Assign the specified color to the current user"
<<PreviewColor ColorName "Post a preview image of the color"`
)
func init() {
createConfig(&config)
if config.BotToken == "YOUR_BOT_TOKEN" {
panic("Default BotToken, pls change the settings in config.yaml.")
} else if config.SpamChannel == "Channel_ID" {
panic("Default SpamChannel, pls change the settings in config.yaml.")
}
}
func createConfig(conf *Config) {
yamlFile, err := ioutil.ReadFile("config.yaml")
err = yaml.Unmarshal(yamlFile, &conf)
if err != nil {
log.Fatalf("error: %v", err)
}
}
// Main
func main() {
rand.Seed(time.Now().UTC().UnixNano())
for key := range config.Colors {
RoleNames = append(RoleNames, key)
}
discord, err := discordgo.New("Bot " + config.BotToken)
if err != nil {
fmt.Println("Error creating Discord session: ", err)
return
}
discord.AddHandler(OnReady)
discord.AddHandler(OnMessage)
discord.AddHandler(OnMemberJoin)
discord.AddHandler(MemberChunkRequest)
// We need information about guilds (which includes their channels),
// messages and voice states.
discord.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAll)
err = discord.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
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, os.Kill)
<-sc
// Cleanly close down the Discord session.
discord.Close()
}
func OnReady(session *discordgo.Session, Ready *discordgo.Ready) {
session.UpdateGameStatus(0, "Perfect Color!")
if FirstTime {
for _, Guild := range Ready.Guilds {
LoadRoles(session, Guild.ID)
CreateNewRoles(session, Guild.ID)
}
fmt.Printf("Done.\n")
FirstTime = false
}
}
func OnMessage(session *discordgo.Session, msg *discordgo.MessageCreate) {
if msg.Author.ID == session.State.User.ID {
return
}
if !strings.HasPrefix(msg.Content, "<<") {
return
}
Channel, err := session.State.Channel(msg.ChannelID)
if err != nil {
fmt.Printf("Can't get the channel.\n")
fmt.Printf("Error:\n%s", err)
return
}
if CheckChannel(Channel) {
if strings.HasPrefix(msg.Content, "<<NewColor") {
resp := RemoveColorFromMember(session, Channel.GuildID, msg.Author.ID)
if resp {
return
}
SplitContent := strings.Split(msg.Content, " ")
if len(SplitContent) == 1 {
UpdateMemberColorRandom(session, Channel.GuildID, msg.Author.ID)
} else if len(SplitContent) == 2 {
if _, ok := CreatedRoles[Channel.GuildID][SplitContent[1]]; ok {
UpdateMemberColor(session, Channel.GuildID, msg.Author.ID, SplitContent[1])
} else if _, ok := config.Colors[SplitContent[1]]; ok {
UpdateMemberColorRandom(session, Channel.GuildID, msg.Author.ID)
SendMessageAndDeleteAfterTime(session, msg.ChannelID, "Color not found available on this server.")
} else {
UpdateMemberColorRandom(session, Channel.GuildID, msg.Author.ID)
SendMessageAndDeleteAfterTime(session, msg.ChannelID, "Color not found, pls use <<PrintColors.")
}
} else {
UpdateMemberColorRandom(session, Channel.GuildID, msg.Author.ID)
SendMessageAndDeleteAfterTime(session, msg.ChannelID, "Too many arguments, pls use <<Help.")
}
session.ChannelMessageDelete(msg.ChannelID, msg.ID)
} else if strings.HasPrefix(msg.Content, "<<PreviewColor") {
SplitContent := strings.Split(msg.Content, " ")
if len(SplitContent) == 2 {
if _, ok := config.Colors[SplitContent[1]]; ok {
Embed := PreviewRole(session, SplitContent[1])
SendEmbedAndDeleteAfterTime(session, msg.ChannelID, Embed)
} else {
SendMessageAndDeleteAfterTime(session, msg.ChannelID, "Color not found, pls use <<PrintColors.")
}
} else {
SendMessageAndDeleteAfterTime(session, msg.ChannelID, "Too many arguments, pls use <<Help.")
}
session.ChannelMessageDelete(msg.ChannelID, msg.ID)
} else if strings.HasPrefix(msg.Content, "<<PrintColors") {
SendMessageAndDeleteAfterTime(session, msg.ChannelID, "https://nayu.moe/colors")
session.ChannelMessageDelete(msg.ChannelID, msg.ID)
} else if strings.HasPrefix(msg.Content, "<<Help") {
SendMessageAndDeleteAfterTime(session, msg.ChannelID, HelpText)
session.ChannelMessageDelete(msg.ChannelID, msg.ID)
}
}
if CheckAdmin(msg.Author.ID) {
if strings.HasPrefix(msg.Content, "<<NewServer") {
JoinedNewGuild(session, Channel.GuildID)
session.ChannelMessageDelete(msg.ChannelID, msg.ID)
} else if strings.HasPrefix(msg.Content, "<<AddColorToAllMember") {
AddColorToAllMember(session, Channel.GuildID)
session.ChannelMessageDelete(msg.ChannelID, msg.ID)
} else if strings.HasPrefix(msg.Content, "<<RemoveAllColors") {
RemoveAllColors(session, Channel.GuildID)
session.ChannelMessageDelete(msg.ChannelID, msg.ID)
} else if strings.HasPrefix(msg.Content, "<<ReloadColors") {
config = Config{}
createConfig(&config)
CreateNewRoles(session, Channel.GuildID)
session.ChannelMessageDelete(msg.ChannelID, msg.ID)
}
}
}
func OnMemberJoin(session *discordgo.Session, Member *discordgo.GuildMemberAdd) {
UpdateMemberColorRandom(session, Member.GuildID, Member.User.ID)
if _, ok := config.AutoKickOnServer[Member.GuildID]; ok {
go KickMemberAfterTime(session, Member.GuildID, Member.User.ID)
}
}
func CheckAdmin(UserID string) bool {
if _, ok := config.Admins[UserID]; ok {
return true
}
return false
}
func CheckChannel(Channel *discordgo.Channel) bool {
if Channel.GuildID == "221919789017202688" {
if Channel.ID == "300947822956773376" {
return true
} else {
return false
}
} else {
return true
}
}
func MemberChunkRequest(session *discordgo.Session, event *discordgo.GuildMembersChunk) {
for _, Member := range event.Members {
UpdateMemberColorRandom(session, event.GuildID, Member.User.ID)
}
fmt.Printf("Updated all members.\n")
}
func LoadRoles(session *discordgo.Session, GuildID string) {
GuildRoles, err := session.GuildRoles(GuildID)
if err != nil {
fmt.Printf("Error: %s\n", err)
panic("Can't find the server.")
}
// Initialise nested map with GuildID as key
CreatedRoles[GuildID] = map[string]Roles{}
for _, Role := range GuildRoles {
if _, ok := config.Colors[Role.Name]; ok {
CreatedRoles[GuildID][Role.Name] = Roles{Role.ID, Role.Name}
CreatedRoles[GuildID][Role.ID] = Roles{Role.ID, Role.Name}
}
}
}
func JoinedNewGuild(session *discordgo.Session, GuildID string) {
// Initialise nested map with GuildID as key
CreatedRoles[GuildID] = map[string]Roles{}
fmt.Printf("Joined a new server: %s\n", GuildID)
CreateAllRoles(session, GuildID)
}
func AddColorToAllMember(session *discordgo.Session, GuildID string) {
fmt.Printf("Updating all member with new a color.\n")
session.RequestGuildMembers(GuildID, "", 0, true)
}
func RemoveAllColors(session *discordgo.Session, GuildID string) {
GuildRoles, err := session.GuildRoles(GuildID)
if err != nil {
panic("Can't find the server.\n")
}
for _, Role := range GuildRoles {
if _, ok := config.Colors[Role.Name]; ok {
session.GuildRoleDelete(GuildID, Role.ID)
}
}
}
func UpdateMemberColor(session *discordgo.Session, GuildID, MemberID, RoleName string) {
err := session.GuildMemberRoleAdd(GuildID, MemberID, CreatedRoles[GuildID][RoleName].ID)
if err != nil {
LoadRoles(session, GuildID)
UpdateMemberColorRandom(session, GuildID, MemberID)
}
}
func UpdateMemberColorRandom(session *discordgo.Session, GuildID, MemberID string) {
key := rand.Intn(len(config.Colors))
session.GuildMemberRoleAdd(GuildID, MemberID, CreatedRoles[GuildID][RoleNames[key]].ID)
}
func CreateColorRole(session *discordgo.Session, GuildID, Name string, Color int) {
role, err := session.GuildRoleCreate(GuildID)
if err != nil {
panic("Wrong Permissions: Can't create a role.")
}
fmt.Printf("Name: %s Int: %d \n", Name, Color)
Role, _ := session.GuildRoleEdit(GuildID, role.ID, Name, Color, false, 0, false)
CreatedRoles[GuildID][Role.Name] = Roles{Role.ID, Role.Name}
CreatedRoles[GuildID][Role.ID] = Roles{Role.ID, Role.Name}
}
func CreateNewRoles(session *discordgo.Session, GuildID string) {
for Name, Color := range config.Colors {
if _, ok := CreatedRoles[GuildID][Name]; !ok {
CreateColorRole(session, GuildID, Name, Color)
}
}
}
func CreateAllRoles(session *discordgo.Session, GuildID string) {
for Name, Color := range config.Colors {
CreateColorRole(session, GuildID, Name, Color)
}
}
func RemoveColorFromMember(session *discordgo.Session, GuildID, MemberID string) bool {
Member, err := session.GuildMember(GuildID, MemberID)
if err != nil {
fmt.Printf("Can't get the guild.\n")
fmt.Printf("Error:\n%s", err)
return true
}
for _, RoleID := range Member.Roles {
if _, ok := CreatedRoles[GuildID][RoleID]; ok {
session.GuildMemberRoleRemove(GuildID, MemberID, RoleID)
}
}
return false
}
func PreviewRole(session *discordgo.Session, RoleName string) discordgo.MessageEmbed {
CreateImageWithColor(config.Colors[RoleName], RoleName)
Embed := CreateImageEmbed(session, RoleName)
return Embed
}
// Create Preview Image
func CreateImageWithColor(ColorInt int, ColorName string) {
size := image.Rect(0, 0, 200, 100)
rgbaImage := image.NewRGBA(size)
red := uint8((ColorInt >> 16) & 0xff)
green := uint8((ColorInt >> 8) & 0xff)
blue := uint8(ColorInt & 0xff)
c := color.RGBA{R: red, G: green, B: blue, A: 255}
draw.Draw(rgbaImage, rgbaImage.Bounds(), &image.Uniform{c}, image.ZP, draw.Src)
f, err := os.Create(ColorName + ".png")
if err != nil {
log.Fatal(err)
}
if err := png.Encode(f, rgbaImage); err != nil {
f.Close()
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
func CreateImageEmbed(session *discordgo.Session, ColorName string) discordgo.MessageEmbed {
Embed := discordgo.MessageEmbed{Title: ColorName, Color: config.Colors[ColorName]}
FileReader, _ := os.Open(ColorName + ".png")
Msg, err := session.ChannelFileSend(config.SpamChannel, ColorName+".png", FileReader)
if err != nil {
log.Fatal(err)
return Embed
}
Image := discordgo.MessageEmbedImage{URL: Msg.Attachments[0].URL, Height: 100, Width: 200}
Embed.Image = &Image
return Embed
}
func KickMemberAfterTime(session *discordgo.Session, GuildID, MemberID string) {
time.Sleep(30 * time.Minute)
Member, err := session.GuildMember(GuildID, MemberID)
if err != nil {
fmt.Printf("Member already leaved.\n")
return
}
for _, RoleID := range Member.Roles {
if _, ok := config.Colors[CreatedRoles[GuildID][RoleID].Name]; ok {
continue
} else {
return
}
}
err = session.GuildMemberDeleteWithReason(GuildID, MemberID, "Not enough roles after 30min.")
if err != nil {
panic("Wrong Permissions: Can't kick a member.")
}
PrivateChannel, err := session.UserChannelCreate(MemberID)
if err != nil {
fmt.Printf("Can't send the message.\n")
fmt.Printf("Error:\n%s", err)
return
}
session.ChannelMessageSend(PrivateChannel.ID, "You got kicked from the server. Please read the welcome channel.\n"+config.InviteLink)
}
func DeleteMessageAfterTime(session *discordgo.Session, Message *discordgo.Message, Time time.Duration) {
time.Sleep(Time * time.Minute)
session.ChannelMessageDelete(Message.ChannelID, Message.ID)
}
func SendMessageAndDeleteAfterTime(session *discordgo.Session, ChannelID, Content string) {
Message, err := session.ChannelMessageSend(ChannelID, Content)
if err != nil {
fmt.Printf("Can't send the message.\n")
fmt.Printf("Error:\n%s", err)
return
}
go DeleteMessageAfterTime(session, Message, 5)
}
func SendEmbedAndDeleteAfterTime(session *discordgo.Session, ChannelID string, Embed discordgo.MessageEmbed) {
Message, err := session.ChannelMessageSendEmbed(ChannelID, &Embed)
if err != nil {
fmt.Printf("Can't send embed.\n")
fmt.Printf("Error:\n%s", err)
return
}
go DeleteMessageAfterTime(session, Message, 5)
}