-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.go
135 lines (111 loc) · 3.83 KB
/
actor.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
// Package modchannel contains an actor to modify title / category of
// a channel
package modchannel
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
"gopkg.in/irc.v4"
"github.com/Luzifer/go_helpers/v2/fieldcollection"
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
const actorName = "modchannel"
var (
formatMessage plugins.MsgFormatter
tcGetter func(string) (*twitch.Client, error)
)
// Register provides the plugins.RegisterFunc
func Register(args plugins.RegistrationArguments) error {
formatMessage = args.FormatMessage
tcGetter = args.GetTwitchClientForChannel
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
args.RegisterActorDocumentation(plugins.ActionDocumentation{
Description: "Update stream information",
Name: "Modify Stream",
Type: "modchannel",
Fields: []plugins.ActionDocumentationField{
{
Default: "",
Description: "Channel to update",
Key: "channel",
Name: "Channel",
Optional: false,
SupportTemplate: true,
Type: plugins.ActionDocumentationFieldTypeString,
},
{
Default: "",
Description: "Category / Game to set (use `@1234` format to pass an explicit ID)",
Key: "game",
Name: "Game",
Optional: true,
SupportTemplate: true,
Type: plugins.ActionDocumentationFieldTypeString,
},
{
Default: "",
Description: "Stream title to set",
Key: "title",
Name: "Title",
Optional: true,
SupportTemplate: true,
Type: plugins.ActionDocumentationFieldTypeString,
},
},
})
return nil
}
type actor struct{}
func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
var (
game = attrs.MustString("game", helpers.Ptr(""))
title = attrs.MustString("title", helpers.Ptr(""))
)
if game == "" && title == "" {
return false, nil
}
var updGame, updTitle *string
channel, err := formatMessage(attrs.MustString("channel", nil), m, r, eventData)
if err != nil {
return false, errors.Wrap(err, "parsing channel")
}
if game != "" {
parsedGame, err := formatMessage(game, m, r, eventData)
if err != nil {
return false, errors.Wrap(err, "parsing game")
}
updGame = &parsedGame
}
if title != "" {
parsedTitle, err := formatMessage(title, m, r, eventData)
if err != nil {
return false, errors.Wrap(err, "parsing title")
}
updTitle = &parsedTitle
}
twitchClient, err := tcGetter(strings.TrimLeft(channel, "#"))
if err != nil {
return false, errors.Wrap(err, "getting Twitch client")
}
return false, errors.Wrap(
twitchClient.ModifyChannelInformation(context.Background(), strings.TrimLeft(channel, "#"), updGame, updTitle),
"updating channel info",
)
}
func (actor) IsAsync() bool { return false }
func (actor) Name() string { return actorName }
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
if err = attrs.ValidateSchema(
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "channel", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "game", Type: fieldcollection.SchemaFieldTypeString}),
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "title", Type: fieldcollection.SchemaFieldTypeString}),
fieldcollection.MustHaveNoUnknowFields,
helpers.SchemaValidateTemplateField(tplValidator, "channel", "game", "title"),
); err != nil {
return fmt.Errorf("validating attributes: %w", err)
}
return nil
}