mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-20 11:51:17 +00:00
[marker] Implement actor to create stream markers
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
8819b4031a
commit
5a8459cedc
5 changed files with 167 additions and 1 deletions
|
@ -84,6 +84,23 @@ Triggers the creation of a Clip from the given channel owned by the creator (sub
|
||||||
add_delay: false
|
add_delay: false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Create Marker
|
||||||
|
|
||||||
|
Creates a marker on the currently running stream of the given channel. The marker will be created on behalf of the channel owner and requires matching scope.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- type: marker
|
||||||
|
attributes:
|
||||||
|
# Channel to create the marker in, defaults to the channel of the event / message
|
||||||
|
# Optional: true
|
||||||
|
# Type: string (Supports Templating)
|
||||||
|
channel: ""
|
||||||
|
# Description of the marker to create (up to 140 chars)
|
||||||
|
# Optional: true
|
||||||
|
# Type: string (Supports Templating)
|
||||||
|
description: ""
|
||||||
|
```
|
||||||
|
|
||||||
## Custom Event
|
## Custom Event
|
||||||
|
|
||||||
Create a custom event
|
Create a custom event
|
||||||
|
|
111
internal/actors/marker/actor.go
Normal file
111
internal/actors/marker/actor.go
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
// Package marker contains an actor to create markers on the current
|
||||||
|
// running stream
|
||||||
|
package marker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"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"
|
||||||
|
"gopkg.in/irc.v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
const actorName = "marker"
|
||||||
|
|
||||||
|
var (
|
||||||
|
formatMessage plugins.MsgFormatter
|
||||||
|
hasPerm plugins.ChannelPermissionCheckFunc
|
||||||
|
tcGetter func(string) (*twitch.Client, error)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Register provides the plugins.RegisterFunc
|
||||||
|
func Register(args plugins.RegistrationArguments) error {
|
||||||
|
formatMessage = args.FormatMessage
|
||||||
|
hasPerm = args.HasPermissionForChannel
|
||||||
|
tcGetter = args.GetTwitchClientForChannel
|
||||||
|
|
||||||
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
||||||
|
|
||||||
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
||||||
|
Description: "Creates a marker on the currently running stream of the given channel. The marker will be created on behalf of the channel owner and requires matching scope.",
|
||||||
|
Name: "Create Marker",
|
||||||
|
Type: actorName,
|
||||||
|
Fields: []plugins.ActionDocumentationField{
|
||||||
|
{
|
||||||
|
Description: "Channel to create the marker in, defaults to the channel of the event / message",
|
||||||
|
Key: "channel",
|
||||||
|
Name: "Channel",
|
||||||
|
Optional: true,
|
||||||
|
SupportTemplate: true,
|
||||||
|
Type: plugins.ActionDocumentationFieldTypeString,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Description: "Description of the marker to create (up to 140 chars)",
|
||||||
|
Key: "description",
|
||||||
|
Name: "Description",
|
||||||
|
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) {
|
||||||
|
channel := plugins.DeriveChannel(m, eventData)
|
||||||
|
if channel, err = formatMessage(attrs.MustString("channel", &channel), m, r, eventData); err != nil {
|
||||||
|
return false, fmt.Errorf("parsing channel: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var description string
|
||||||
|
if description, err = formatMessage(attrs.MustString("description", &description), m, r, eventData); err != nil {
|
||||||
|
return false, fmt.Errorf("parsing description: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
channel = strings.TrimLeft(channel, "#")
|
||||||
|
|
||||||
|
canCreate, err := hasPerm(channel, twitch.ScopeChannelManageBroadcast)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("checking for required permission: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !canCreate {
|
||||||
|
return false, fmt.Errorf("creator has not given %s permission", twitch.ScopeChannelManageBroadcast)
|
||||||
|
}
|
||||||
|
|
||||||
|
tc, err := tcGetter(channel)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("getting Twitch client for %q: %w", channel, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = tc.CreateStreamMarker(context.TODO(), description); err != nil {
|
||||||
|
return false, fmt.Errorf("creating marker: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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.CanHaveField(fieldcollection.SchemaField{Name: "channel", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||||
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "description", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||||
|
fieldcollection.MustHaveNoUnknowFields,
|
||||||
|
helpers.SchemaValidateTemplateField(tplValidator, "channel", "description"),
|
||||||
|
); err != nil {
|
||||||
|
return fmt.Errorf("validating attributes: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
package twitch
|
package twitch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
@ -33,6 +35,40 @@ type (
|
||||||
// the fact there just is no stream found
|
// the fact there just is no stream found
|
||||||
var ErrNoStreamsFound = errors.New("no streams found")
|
var ErrNoStreamsFound = errors.New("no streams found")
|
||||||
|
|
||||||
|
// CreateStreamMarker creates a marker for the currently running stream.
|
||||||
|
// The stream must be live, no VoD, no upload and no re-run.
|
||||||
|
// The description may be up to 140 chars and can be omitted.
|
||||||
|
func (c *Client) CreateStreamMarker(ctx context.Context, description string) (err error) {
|
||||||
|
body := new(bytes.Buffer)
|
||||||
|
|
||||||
|
userID, _, err := c.GetAuthorizedUser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("getting ID for current user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.NewEncoder(body).Encode(struct {
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
}{
|
||||||
|
UserID: userID,
|
||||||
|
Description: description,
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("encoding payload: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Request(ctx, ClientRequestOpts{
|
||||||
|
AuthType: AuthTypeBearerToken,
|
||||||
|
Body: body,
|
||||||
|
Method: http.MethodPost,
|
||||||
|
OKStatus: http.StatusOK,
|
||||||
|
URL: "https://api.twitch.tv/helix/streams/markers",
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("creating marker: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetCurrentStreamInfo returns the StreamInfo of the currently running
|
// GetCurrentStreamInfo returns the StreamInfo of the currently running
|
||||||
// stream of the given username
|
// stream of the given username
|
||||||
func (c *Client) GetCurrentStreamInfo(ctx context.Context, username string) (*StreamInfo, error) {
|
func (c *Client) GetCurrentStreamInfo(ctx context.Context, username string) (*StreamInfo, error) {
|
||||||
|
|
|
@ -25,6 +25,7 @@ import (
|
||||||
"github.com/Luzifer/twitch-bot/v3/internal/actors/linkdetector"
|
"github.com/Luzifer/twitch-bot/v3/internal/actors/linkdetector"
|
||||||
"github.com/Luzifer/twitch-bot/v3/internal/actors/linkprotect"
|
"github.com/Luzifer/twitch-bot/v3/internal/actors/linkprotect"
|
||||||
logActor "github.com/Luzifer/twitch-bot/v3/internal/actors/log"
|
logActor "github.com/Luzifer/twitch-bot/v3/internal/actors/log"
|
||||||
|
"github.com/Luzifer/twitch-bot/v3/internal/actors/marker"
|
||||||
"github.com/Luzifer/twitch-bot/v3/internal/actors/messagehook"
|
"github.com/Luzifer/twitch-bot/v3/internal/actors/messagehook"
|
||||||
"github.com/Luzifer/twitch-bot/v3/internal/actors/modchannel"
|
"github.com/Luzifer/twitch-bot/v3/internal/actors/modchannel"
|
||||||
"github.com/Luzifer/twitch-bot/v3/internal/actors/nuke"
|
"github.com/Luzifer/twitch-bot/v3/internal/actors/nuke"
|
||||||
|
@ -78,6 +79,7 @@ var (
|
||||||
linkdetector.Register,
|
linkdetector.Register,
|
||||||
linkprotect.Register,
|
linkprotect.Register,
|
||||||
logActor.Register,
|
logActor.Register,
|
||||||
|
marker.Register,
|
||||||
messagehook.Register,
|
messagehook.Register,
|
||||||
modchannel.Register,
|
modchannel.Register,
|
||||||
nuke.Register,
|
nuke.Register,
|
||||||
|
|
|
@ -5,7 +5,7 @@ import "github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
||||||
var (
|
var (
|
||||||
channelExtendedScopes = map[string]string{
|
channelExtendedScopes = map[string]string{
|
||||||
twitch.ScopeChannelEditCommercial: "run commercial",
|
twitch.ScopeChannelEditCommercial: "run commercial",
|
||||||
twitch.ScopeChannelManageBroadcast: "modify category / title",
|
twitch.ScopeChannelManageBroadcast: "modify category / title, create markers",
|
||||||
twitch.ScopeChannelManagePolls: "manage polls",
|
twitch.ScopeChannelManagePolls: "manage polls",
|
||||||
twitch.ScopeChannelManagePredictions: "manage predictions",
|
twitch.ScopeChannelManagePredictions: "manage predictions",
|
||||||
twitch.ScopeChannelManageRaids: "start raids",
|
twitch.ScopeChannelManageRaids: "start raids",
|
||||||
|
|
Loading…
Reference in a new issue