-
Notifications
You must be signed in to change notification settings - Fork 927
/
bot.go
100 lines (82 loc) · 2.71 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
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) {
feeds, err := models.RedditFeeds(models.RedditFeedWhere.Disabled.EQ(false)).CountG(context.Background())
if err != nil {
logger.WithError(err).Error("Failed Checking Reddit feeds")
return "Total Feeds", "error"
}
return "Total Feeds", fmt.Sprintf("%d", feeds)
}