2021-11-20 20:28:53 +00:00
|
|
|
package roles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-20 17:40:10 +00:00
|
|
|
bh "github.com/timshannon/bolthold"
|
2021-11-20 20:28:53 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RolesPlugin struct {
|
2021-12-20 17:40:10 +00:00
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
store *bh.Store
|
|
|
|
h bot.HandlerTable
|
2021-11-20 20:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *RolesPlugin {
|
|
|
|
p := &RolesPlugin{
|
2021-12-20 17:40:10 +00:00
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
store: b.Store(),
|
2021-11-20 20:28:53 +00:00
|
|
|
}
|
|
|
|
p.RegisterWeb()
|
|
|
|
p.Register()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RolesPlugin) Register() {
|
|
|
|
p.h = bot.HandlerTable{
|
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^role (?P<role>.+)$`),
|
|
|
|
HelpText: "Toggles a role on or off for you. Role must be of the current offerings",
|
|
|
|
Handler: p.toggleRole,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^lsroles\s?(?P<offering>.*)`),
|
|
|
|
HelpText: "Lists roles with an optional offering set specifier",
|
|
|
|
Handler: p.lsRoles,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^offering (?P<offering>.+)`),
|
|
|
|
HelpText: "Changes the current offering set",
|
|
|
|
Handler: p.setOffering,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.b.RegisterTable(p, p.h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RolesPlugin) toggleRole(r bot.Request) bool {
|
|
|
|
role := r.Values["role"]
|
|
|
|
offering := p.c.Get("roles.currentoffering", "")
|
|
|
|
if !strings.HasSuffix(role, offering) {
|
|
|
|
role = fmt.Sprintf("%s-%s", role, offering)
|
|
|
|
}
|
|
|
|
roles, err := r.Conn.GetRoles()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("getRoles")
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "I couldn't get the roles for some reason.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, rr := range roles {
|
|
|
|
if rr.Name == role {
|
|
|
|
if err = r.Conn.SetRole(r.Msg.User.ID, rr.ID); err != nil {
|
|
|
|
log.Error().Err(err).Msg("setRole")
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "I couldn't set that role.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I have toggled your role, %s.", rr.Name))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "I couldn't find that role.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RolesPlugin) lsRoles(r bot.Request) bool {
|
|
|
|
offering := r.Values["offering"]
|
|
|
|
if offering == "" {
|
|
|
|
// This would be all if we hit the fallback
|
|
|
|
offering = p.c.Get("roles.currentOffering", "")
|
|
|
|
}
|
|
|
|
roles, err := r.Conn.GetRoles()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("getRoles")
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "I couldn't get the roles for some reason.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
msg := "Available roles: "
|
|
|
|
i := 0
|
|
|
|
for _, r := range roles {
|
|
|
|
if !strings.HasSuffix(r.Name, offering) || r.Name == "@everyone" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
msg += ", "
|
|
|
|
}
|
|
|
|
msg += fmt.Sprintf("%s", strings.TrimSuffix(r.Name, "-"+offering))
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
msg += "\n\nUse `!role [rolename]` to toggle your membership in one of these roles."
|
|
|
|
if i == 0 {
|
|
|
|
msg = "I found no matching roles."
|
|
|
|
}
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RolesPlugin) setOffering(r bot.Request) bool {
|
|
|
|
if !p.b.CheckAdmin(r.Msg.User.Name) {
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "You must be an admin to set an offering.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
offering := r.Values["offering"]
|
|
|
|
err := p.c.Set("roles.currentOffering", offering)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("setOffering")
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Could not set that offering")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("New offering set to %s", offering))
|
|
|
|
return true
|
|
|
|
}
|