forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.go
160 lines (136 loc) · 4.02 KB
/
reddit.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
package reddit
//go:generate esc -o assets_gen.go -pkg reddit -ignore ".go" assets/
import (
"encoding/json"
"fmt"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/mqueue"
"github.com/mediocregopher/radix.v2/redis"
log "github.com/sirupsen/logrus"
"strconv"
"strings"
"sync"
)
type Plugin struct {
stopFeedChan chan *sync.WaitGroup
}
func (p *Plugin) Name() string {
return "Reddit"
}
// Remove feeds if they don't point to a proper channel
func (p *Plugin) HandleMQueueError(elem *mqueue.QueuedElement, err error) {
code, _ := common.DiscordError(err)
if code != discordgo.ErrCodeUnknownChannel {
l := log.WithError(err).WithField("channel", elem.Channel)
if code != discordgo.ErrCodeMissingPermissions && code != discordgo.ErrCodeMissingAccess {
l = l.WithField("s_msg", elem.MessageEmbed)
}
l.Warn("Error posting reddit message")
return
}
log.WithError(err).WithField("channel", elem.Channel).Info("Removing reddit feed to nonexistant discord channel")
// channelid:feed-id
split := strings.Split(elem.SourceID, ":")
if len(split) < 2 {
log.Error("Invalid queued item: ", elem.ID)
return
}
guildID := split[0]
itemID := split[1]
client, err := common.RedisPool.Get()
if err != nil {
log.WithError(err).Error("Failed retrieving redis client from pool")
return
}
defer common.RedisPool.Put(client)
currentConfig, err := GetConfig(client, "guild_subreddit_watch:"+guildID)
if err != nil {
log.WithError(err).Error("Failed fetching config to remove")
return
}
parsed, err := strconv.Atoi(itemID)
if err != nil {
log.WithError(err).WithField("mq_id", elem.ID).Error("Failed parsing item id")
}
parsedGID, _ := strconv.ParseInt(guildID, 10, 64)
for _, v := range currentConfig {
if v.ID == parsed {
v.Remove(client)
common.AddCPLogEntry(common.BotUser, parsedGID, "Removed reddit feed from "+v.Sub+", Channel does not exist")
break
}
}
}
func RegisterPlugin() {
plugin := &Plugin{
stopFeedChan: make(chan *sync.WaitGroup),
}
common.RegisterPlugin(plugin)
mqueue.RegisterSource("reddit", plugin)
}
type SubredditWatchItem struct {
Sub string `json:"sub"`
Guild string `json:"guild"`
Channel string `json:"channel"`
ID int `json:"id"`
}
func FindWatchItem(source []*SubredditWatchItem, id int) *SubredditWatchItem {
for _, c := range source {
if c.ID == id {
return c
break
}
}
return nil
}
func (item *SubredditWatchItem) Set(client *redis.Client) error {
serialized, err := json.Marshal(item)
if err != nil {
return err
}
guild := item.Guild
cmds := []*common.RedisCmd{
&common.RedisCmd{Name: "HSET", Args: []interface{}{"guild_subreddit_watch:" + guild, item.ID, serialized}},
&common.RedisCmd{Name: "HSET", Args: []interface{}{"global_subreddit_watch:" + strings.ToLower(item.Sub), fmt.Sprintf("%s:%d", guild, item.ID), serialized}},
}
_, err = common.SafeRedisCommands(client, cmds)
return err
}
func (item *SubredditWatchItem) Remove(client *redis.Client) error {
guild := item.Guild
cmds := []*common.RedisCmd{
&common.RedisCmd{Name: "HDEL", Args: []interface{}{"guild_subreddit_watch:" + guild, item.ID}},
&common.RedisCmd{Name: "HDEL", Args: []interface{}{"global_subreddit_watch:" + strings.ToLower(item.Sub), fmt.Sprintf("%s:%d", guild, item.ID)}},
}
_, err := common.SafeRedisCommands(client, cmds)
return err
}
func GetConfig(client *redis.Client, key string) ([]*SubredditWatchItem, error) {
rawItems, err := client.Cmd("HGETALL", key).Map()
if err != nil {
return nil, err
}
out := make([]*SubredditWatchItem, len(rawItems))
i := 0
for k, raw := range rawItems {
var decoded *SubredditWatchItem
err := json.Unmarshal([]byte(raw), &decoded)
if err != nil {
return nil, err
}
if err != nil {
id, _ := strconv.ParseInt(k, 10, 32)
out[i] = &SubredditWatchItem{
Sub: "ERROR",
Channel: "ERROR DECODING",
ID: int(id),
}
log.WithError(err).Error("Failed decoding reddit watch item")
} else {
out[i] = decoded
}
i++
}
return out, nil
}