This repository has been archived by the owner on Mar 6, 2021. It is now read-only.
forked from starshine-sys/covebotnt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blacklist.go
97 lines (81 loc) · 2.14 KB
/
blacklist.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
package main
import (
"context"
"fmt"
)
func getBlacklistAll() (channelBlacklist map[string][]string) {
var guilds []string
guildRows, err := db.Query(context.Background(), "select distinct server_id from starboard_blacklisted_channels")
if err != nil {
panic(err)
}
defer guildRows.Close()
for guildRows.Next() {
var guild string
err = guildRows.Scan(&guild)
guilds = append(guilds, guild)
}
for _, guild := range guilds {
var channels []string
channelRows, err := db.Query(context.Background(), "select channel_id from starboard_blacklisted_channels where server_id=$1", guild)
if err != nil {
panic(err)
}
defer channelRows.Close()
for channelRows.Next() {
var channel string
err = channelRows.Scan(&channel)
if err != nil {
panic(err)
}
channels = append(channels, channel)
}
channelBlacklist[guild] = channels
}
return channelBlacklist
}
func getBlacklistForGuild(guildID string) error {
var channels []string
channelRows, err := db.Query(context.Background(), "select channel_id from starboard_blacklisted_channels where server_id=$1", guildID)
if err != nil {
return err
}
defer channelRows.Close()
for channelRows.Next() {
var channel string
err = channelRows.Scan(&channel)
if err != nil {
return err
}
channels = append(channels, channel)
}
channelBlacklist[guildID] = channels
return nil
}
func addChannelsToBlacklist(guildID string, channels []string) error {
for _, channel := range channelBlacklist[guildID] {
for i, newChannel := range channels {
if channel == newChannel {
channels = removeFromSlice(channels, i)
}
}
}
for _, channel := range channels {
commandTag, err := db.Exec(context.Background(), "insert into starboard_blacklisted_channels (channel_id, server_id) values ($1, $2)", channel, guildID)
if err != nil {
return err
}
if commandTag.RowsAffected() == 0 {
return fmt.Errorf("Channel %v (Guilld %v) was not added to the blacklist", channel, guildID)
}
}
err := getBlacklistForGuild(guildID)
if err != nil {
return err
}
return nil
}
func removeFromSlice(s []string, i int) []string {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}