-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsidebar.go
105 lines (88 loc) · 2.24 KB
/
sidebar.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
package main
import (
"context"
"fmt"
"time"
"github.com/Sadzeih/valcompbot/config"
"github.com/Sadzeih/valcompbot/matches"
"github.com/vartanbeno/go-reddit/v2/reddit"
)
func startSidebar(rClient *reddit.Client) {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
subSettings, _, err := rClient.Subreddit.GetSettings(context.Background(), config.Get().RedditSubreddit)
if err != nil {
fmt.Println(err)
return
}
m, err := matches.GetUpcoming()
if err != nil {
fmt.Println(err)
}
if err := BuildSidebar(rClient, subSettings, m); err != nil {
fmt.Println(err)
}
if err := BuildWidget(rClient, m); err != nil {
fmt.Println(err)
}
for range ticker.C {
m, err = matches.GetUpcoming()
if err != nil {
fmt.Println(err)
}
subSettings, _, err = rClient.Subreddit.GetSettings(context.Background(), config.Get().RedditSubreddit)
if err != nil {
fmt.Println(err)
return
}
if err := BuildSidebar(rClient, subSettings, m); err != nil {
fmt.Println(err)
}
if err := BuildWidget(rClient, m); err != nil {
fmt.Println(err)
}
}
}
func BuildSidebar(client *reddit.Client, subSettings *reddit.SubredditSettings, m []matches.Upcoming) error {
sidebarMd, err := format(true, m)
if err != nil {
return err
}
subSettings.Sidebar = &sidebarMd
_, err = client.Subreddit.Edit(context.Background(), subSettings.ID, subSettings)
if err != nil {
return err
}
return nil
}
func BuildWidget(client *reddit.Client, m []matches.Upcoming) error {
sidebarMd, err := format(false, m)
if err != nil {
return err
}
widgets, _, err := client.Widget.Get(context.Background(), config.Get().RedditSubreddit)
if err != nil {
return err
}
var tickerWidget *reddit.TextAreaWidget
for _, widget := range widgets {
w, ok := widget.(*reddit.TextAreaWidget)
if ok && w.Name == "Match Ticker" {
tickerWidget = w
break
}
}
if tickerWidget == nil {
return fmt.Errorf("BuildWidget: could not find ticker widget")
}
widgetReq := reddit.TextAreaWidgetRequest{
Style: tickerWidget.Style,
Text: sidebarMd,
Name: tickerWidget.Name,
}
_, _, err = client.Widget.Update(context.Background(), config.Get().RedditSubreddit, tickerWidget.GetID(), &widgetReq)
if err != nil {
fmt.Println(err)
}
return nil
}