-
Notifications
You must be signed in to change notification settings - Fork 0
/
points-event.go.old
51 lines (41 loc) · 1.19 KB
/
points-event.go.old
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
package twitchpubsub
// Helper functions and structures for twitch bits events
import (
"encoding/json"
"errors"
"fmt"
"strings"
// "time"
)
const pointsEventTopicPrefix = "channel-points-channel-v1."
// BitsEvent describes an incoming "Bit" action coming from Twitch's PubSub servers
type PointsEvent struct {
Channel_id string `json:"channel_id"`
Redeemed_at string `json:"redeemed_at"`
Reward string `json"reward"`
Id string `json:"id"`
User string `json:"user"`
}
type outerPointsEvent struct {
Data PointsEvent `json:"data"`
}
func parsePointsEvent(bytes []byte) (*PointsEvent, error) {
data := &outerPointsEvent{}
err := json.Unmarshal(bytes, data)
if err != nil {
return nil, err
}
return &data.Data, nil
}
func parseChannelIDFromPointsTopic(topic string) (string, error) {
parts := strings.Split(topic, ".")
if len(parts) != 2 {
return "", errors.New("Unable to parse channel ID from bits topic")
}
return parts[1], nil
}
// BitsEventTopic returns a properly formatted bits event topic string with the given channel ID argument
func PointsEventTopic(channelID string) string {
const f = `channel-points-channel-v1.%s`
return fmt.Sprintf(f, channelID)
}