forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
129 lines (107 loc) · 2.93 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
package reddit
import (
"context"
"fmt"
"strings"
"github.com/jonas747/dcmd"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/reddit/models"
"github.com/jonas747/yagpdb/stdcommands/util"
"github.com/pkg/errors"
)
var _ bot.RemoveGuildHandler = (*Plugin)(nil)
func (p *Plugin) RemoveGuild(g int64) error {
_, err := models.RedditFeeds(models.RedditFeedWhere.GuildID.EQ(g)).DeleteAll(context.Background(), common.PQ)
if err != nil {
return errors.Wrap(err, "failed removing reddit feeds")
}
return nil
}
func (p *Plugin) AddCommands() {
commands.AddRootCommands(&commands.YAGCommand{
CmdCategory: commands.CategoryDebug,
HideFromCommandsPage: true,
Name: "testreddit",
Description: "Tests the reddit feeds in this server by checking the specified post",
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.GS.ID)
err2 := handlerFast.handlePost(resp[0], data.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)
// }