forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.go
169 lines (141 loc) · 3.97 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
161
162
163
164
165
166
167
168
169
package reddit
//go:generate esc -o assets_gen.go -pkg reddit -ignore ".go" assets/
import (
"context"
"encoding/json"
"fmt"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/mqueue"
"github.com/jonas747/yagpdb/premium"
"github.com/mediocregopher/radix.v3"
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 && code != discordgo.ErrCodeMissingAccess && code != discordgo.ErrCodeMissingPermissions {
l := log.WithError(err).WithField("channel", elem.Channel)
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]
currentConfig, err := GetConfig("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()
common.AddCPLogEntry(common.BotUser, parsedGID, "Removed reddit feed from "+v.Sub+", Channel does not exist or no perms")
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"`
UseEmbeds bool `json:"use_embeds"`
}
func FindWatchItem(source []*SubredditWatchItem, id int) *SubredditWatchItem {
for _, c := range source {
if c.ID == id {
return c
break
}
}
return nil
}
func (item *SubredditWatchItem) Set() error {
serialized, err := json.Marshal(item)
if err != nil {
return err
}
guild := item.Guild
err = common.RedisPool.Do(radix.Pipeline(
radix.FlatCmd(nil, "HSET", "guild_subreddit_watch:"+guild, item.ID, serialized),
radix.FlatCmd(nil, "HSET", "global_subreddit_watch:"+strings.ToLower(item.Sub), fmt.Sprintf("%s:%d", guild, item.ID), serialized),
))
return err
}
func (item *SubredditWatchItem) Remove() error {
guild := item.Guild
err := common.RedisPool.Do(radix.Pipeline(
radix.FlatCmd(nil, "HDEL", "guild_subreddit_watch:"+guild, item.ID),
radix.FlatCmd(nil, "HDEL", "global_subreddit_watch:"+strings.ToLower(item.Sub), fmt.Sprintf("%s:%d", guild, item.ID)),
))
return err
}
func GetConfig(key string) ([]*SubredditWatchItem, error) {
var rawItems map[string]string
err := common.RedisPool.Do(radix.Cmd(&rawItems, "HGETALL", key))
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
}
const (
// Max feeds per guild
GuildMaxFeedsNormal = 100
GuildMaxFeedsPremium = 1000
)
func MaxFeedForCtx(ctx context.Context) int {
if premium.ContextPremium(ctx) {
return GuildMaxFeedsPremium
}
return GuildMaxFeedsNormal
}