-
Notifications
You must be signed in to change notification settings - Fork 927
/
bot.go
149 lines (123 loc) · 3.62 KB
/
bot.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
package reddit
import (
"context"
"fmt"
"strings"
"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/v2/bot"
"github.com/botlabs-gg/yagpdb/v2/commands"
"github.com/botlabs-gg/yagpdb/v2/lib/dcmd"
"github.com/botlabs-gg/yagpdb/v2/reddit/models"
"github.com/botlabs-gg/yagpdb/v2/stdcommands/util"
"github.com/volatiletech/sqlboiler/queries/qm"
)
var _ bot.RemoveGuildHandler = (*Plugin)(nil)
func (p *Plugin) RemoveGuild(g int64) error {
_, err := models.RedditFeeds(models.RedditFeedWhere.GuildID.EQ(g)).UpdateAllG(context.Background(), models.M{
"disabled": true,
})
if err != nil {
return errors.WrapIf(err, "failed removing reddit feeds")
}
return nil
}
func (p *Plugin) OnRemovedPremiumGuild(guildID int64) error {
logger.WithField("guild_id", guildID).Infof("Removed Excess Reddit Feeds")
feeds, err := models.RedditFeeds(qm.Where("guild_id = ? and disabled = ?", guildID, false), qm.Offset(GuildMaxFeedsNormal)).AllG(context.Background())
if err != nil {
return errors.WrapIf(err, "failed getting reddit feeds")
}
if len(feeds) > 0 {
_, err = feeds.UpdateAllG(context.Background(), models.M{"disabled": true})
if err != nil {
return errors.WrapIf(err, "failed disabling reddit feeds on premium removal")
}
}
return nil
}
func (p *Plugin) AddCommands() {
commands.AddRootCommands(p, &commands.YAGCommand{
CmdCategory: commands.CategoryDebug,
HideFromCommandsPage: true,
Name: "testreddit",
Description: "Tests the reddit feeds in this server by checking the specified post. Bot Owner Only",
HideFromHelp: true,
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
{Name: "post-id", Type: dcmd.String},
},
RunFunc: util.RequireOwner(func(data *dcmd.Data) (interface{}, error) {
pID := data.Args[0].Str()
if !strings.HasPrefix(pID, "t3_") {
pID = "t3_" + pID
}
resp, err := p.redditClient.LinksInfo([]string{pID})
if err != nil {
return nil, err
}
if len(resp) < 1 {
return "Unknown post", nil
}
handlerSlow := &PostHandlerImpl{
Slow: true,
ratelimiter: NewRatelimiter(),
}
handlerFast := &PostHandlerImpl{
Slow: false,
ratelimiter: NewRatelimiter(),
}
err1 := handlerSlow.handlePost(resp[0], data.GuildData.GS.ID)
err2 := handlerFast.handlePost(resp[0], data.GuildData.GS.ID)
return fmt.Sprintf("SlowErr: `%v`, fastErr: `%v`", err1, err2), nil
}),
})
}
// func (p *Plugin) Status() (string, string) {
// subs := 0
// channels := 0
// cursor := "0"
// common.
// for {
// reply := client.Cmd("SCAN", cursor, "MATCH", "global_subreddit_watch:*")
// if reply.Err != nil {
// logrus.WithError(reply.Err).Error("Error scanning")
// break
// }
// elems, err := reply.Array()
// if err != nil {
// logrus.WithError(err).Error("Error reading reply")
// break
// }
// if len(elems) < 2 {
// logrus.Error("Invalid scan")
// break
// }
// newCursor, err := elems[0].Str()
// if err != nil {
// logrus.WithError(err).Error("Failed retrieving new cursor")
// break
// }
// cursor = newCursor
// list, err := elems[1].List()
// if err != nil {
// logrus.WithError(err).Error("Failed retrieving list")
// break
// }
// for _, key := range list {
// config, err := GetConfig(key)
// if err != nil {
// logrus.WithError(err).Error("Failed reading global config")
// continue
// }
// if len(config) < 1 {
// continue
// }
// subs++
// channels += len(config)
// }
// if cursor == "" || cursor == "0" {
// break
// }
// }
// return "Subs/Channels", fmt.Sprintf("%d/%d", subs, channels)
// }