2024-01-01 16:52:18 +00:00
|
|
|
// Package quotedb contains a quote database and actor / api methods
|
|
|
|
// to manage it
|
2021-10-18 22:22:05 +00:00
|
|
|
package quotedb
|
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"fmt"
|
2021-10-18 22:22:05 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2023-11-25 19:23:34 +00:00
|
|
|
"gorm.io/gorm"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/database"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-10-18 22:22:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-08-25 22:20:10 +00:00
|
|
|
actorName = "quotedb"
|
2021-10-18 22:22:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-09-10 11:39:07 +00:00
|
|
|
db database.Connector
|
2021-10-18 22:22:05 +00:00
|
|
|
formatMessage plugins.MsgFormatter
|
2022-10-25 16:47:30 +00:00
|
|
|
send plugins.SendMessageFunc
|
2021-10-18 22:22:05 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
// ptrStringEmpty = func(v string) *string { return &v }("")
|
|
|
|
// ptrStringOutFormat = func(v string) *string { return &v }("Quote #{{ .index }}: {{ .quote }}")
|
|
|
|
// ptrStringZero = func(v string) *string { return &v }("0")
|
2021-10-18 22:22:05 +00:00
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
|
|
|
func Register(args plugins.RegistrationArguments) (err error) {
|
2022-09-10 11:39:07 +00:00
|
|
|
db = args.GetDatabaseConnector()
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = db.DB().AutoMigrate("e{}); err != nil {
|
2022-09-10 11:39:07 +00:00
|
|
|
return errors.Wrap(err, "applying schema migration")
|
|
|
|
}
|
|
|
|
|
2023-11-25 19:23:34 +00:00
|
|
|
args.RegisterCopyDatabaseFunc("quote", func(src, target *gorm.DB) error {
|
2024-03-27 23:15:14 +00:00
|
|
|
return database.CopyObjects(src, target, "e{})
|
2023-11-25 19:23:34 +00:00
|
|
|
})
|
|
|
|
|
2021-10-18 22:22:05 +00:00
|
|
|
formatMessage = args.FormatMessage
|
2022-10-25 16:47:30 +00:00
|
|
|
send = args.SendMessage
|
2021-10-18 22:22:05 +00:00
|
|
|
|
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Manage a database of quotes in your channel",
|
|
|
|
Name: "Quote Database",
|
|
|
|
Type: actorName,
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Action to execute (one of: add, del, get)",
|
|
|
|
Key: "action",
|
|
|
|
Name: "Action",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "0",
|
|
|
|
Description: "Index of the quote to work with, must yield a number (required on 'del', optional on 'get')",
|
|
|
|
Key: "index",
|
|
|
|
Name: "Index",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Quote to add: Format like you like your quote, nothing is added (required on: add)",
|
|
|
|
Key: "quote",
|
|
|
|
Name: "Quote",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "Quote #{{ .index }}: {{ .quote }}",
|
|
|
|
Description: "Format to use when posting a quote (required on: get)",
|
|
|
|
Key: "format",
|
|
|
|
Name: "Format",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = registerAPI(args.RegisterAPIRoute); err != nil {
|
|
|
|
return fmt.Errorf("registering API: %w", err)
|
|
|
|
}
|
2021-10-21 21:59:53 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
args.RegisterTemplateFunction("lastQuoteIndex", func(m *irc.Message, _ *plugins.Rule, _ *fieldcollection.FieldCollection) interface{} {
|
2022-09-10 11:39:07 +00:00
|
|
|
return func() (int, error) {
|
2024-01-01 16:52:18 +00:00
|
|
|
return getMaxQuoteIdx(db, plugins.DeriveChannel(m, nil))
|
2021-10-18 22:22:05 +00:00
|
|
|
}
|
2023-08-25 21:37:37 +00:00
|
|
|
}, plugins.TemplateFuncDocumentation{
|
|
|
|
Description: "Gets the last quote index in the quote database for the current channel",
|
|
|
|
Syntax: "lastQuoteIndex",
|
|
|
|
Example: &plugins.TemplateFuncDocumentationExample{
|
|
|
|
Template: `Last Quote: #{{ lastQuoteIndex }}`,
|
|
|
|
FakedOutput: "Last Quote: #32",
|
|
|
|
},
|
2021-10-18 22:22:05 +00:00
|
|
|
})
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
return nil
|
2021-10-18 22:22:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
actor struct{}
|
|
|
|
)
|
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
|
2021-10-18 22:22:05 +00:00
|
|
|
var (
|
2024-04-03 19:00:28 +00:00
|
|
|
action = attrs.MustString("action", helpers.Ptr(""))
|
|
|
|
indexStr = attrs.MustString("index", helpers.Ptr("0"))
|
|
|
|
quote = attrs.MustString("quote", helpers.Ptr(""))
|
2021-10-18 22:22:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if indexStr == "" {
|
|
|
|
indexStr = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
if indexStr, err = formatMessage(indexStr, m, r, eventData); err != nil {
|
|
|
|
return false, errors.Wrap(err, "formatting index")
|
|
|
|
}
|
|
|
|
|
|
|
|
index, err := strconv.Atoi(indexStr)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "parsing index to number")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case "add":
|
|
|
|
quote, err = formatMessage(quote, m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "formatting quote")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errors.Wrap(
|
2024-01-01 16:52:18 +00:00
|
|
|
addQuote(db, plugins.DeriveChannel(m, eventData), quote),
|
2022-09-10 11:39:07 +00:00
|
|
|
"adding quote",
|
2021-10-18 22:22:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
case "del":
|
|
|
|
return false, errors.Wrap(
|
2024-01-01 16:52:18 +00:00
|
|
|
delQuote(db, plugins.DeriveChannel(m, eventData), index),
|
2021-10-18 22:22:05 +00:00
|
|
|
"storing quote database",
|
|
|
|
)
|
|
|
|
|
|
|
|
case "get":
|
2024-01-01 16:52:18 +00:00
|
|
|
idx, quote, err := getQuote(db, plugins.DeriveChannel(m, eventData), index)
|
2022-09-10 11:39:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "getting quote")
|
|
|
|
}
|
2021-10-18 22:22:05 +00:00
|
|
|
|
|
|
|
if idx == 0 {
|
|
|
|
// No quote was found for the given idx
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
fields := eventData.Clone()
|
|
|
|
fields.Set("index", idx)
|
|
|
|
fields.Set("quote", quote)
|
2021-10-18 22:22:05 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
format := attrs.MustString("format", helpers.Ptr("Quote #{{ .index }}: {{ .quote }}"))
|
2021-10-18 22:22:05 +00:00
|
|
|
msg, err := formatMessage(format, m, r, fields)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "formatting output message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errors.Wrap(
|
2022-10-25 16:47:30 +00:00
|
|
|
send(&irc.Message{
|
2021-10-18 22:22:05 +00:00
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{
|
|
|
|
plugins.DeriveChannel(m, eventData),
|
|
|
|
msg,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
"sending command",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2021-10-18 22:22:05 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
|
|
|
if err = attrs.ValidateSchema(
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "action", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "quote", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "index", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "format", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
helpers.SchemaValidateTemplateField(tplValidator, "index", "quote", "format"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
action := attrs.MustString("action", helpers.Ptr(""))
|
2021-10-18 22:22:05 +00:00
|
|
|
|
|
|
|
switch action {
|
|
|
|
case "add":
|
|
|
|
if v, err := attrs.String("quote"); err != nil || v == "" {
|
2024-04-03 19:00:28 +00:00
|
|
|
return fmt.Errorf("quote must be non-empty string for action add")
|
2021-10-18 22:22:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "del":
|
|
|
|
if v, err := attrs.String("index"); err != nil || v == "" {
|
2024-04-03 19:00:28 +00:00
|
|
|
return fmt.Errorf("index must be non-empty string for adction del")
|
2021-10-18 22:22:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "get":
|
|
|
|
// No requirements
|
|
|
|
|
|
|
|
default:
|
2024-04-03 19:00:28 +00:00
|
|
|
return fmt.Errorf("action must be one of add, del or get")
|
2022-10-31 16:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 22:22:05 +00:00
|
|
|
return nil
|
|
|
|
}
|