move settings.Allowed() to bot.Allowed()

This commit is contained in:
Aine
2022-08-28 18:36:01 +03:00
parent 1d6b43a83a
commit bd14987561
2 changed files with 20 additions and 21 deletions

View File

@@ -175,7 +175,7 @@ func (b *Bot) runStop(ctx context.Context, checkAllowed bool) {
return
}
if checkAllowed && !cfg.Allowed(b.noowner, evt.Sender, b.allowedUsers) {
if checkAllowed && !b.Allowed(evt.Sender, cfg) {
b.Notice(ctx, evt.RoomID, "you don't have permission to do that")
return
}
@@ -251,7 +251,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
return
}
if !cfg.Allowed(b.noowner, evt.Sender, b.allowedUsers) {
if !b.Allowed(evt.Sender, cfg) {
b.Notice(ctx, evt.RoomID, "you don't have permission to do that, kupo")
return
}

View File

@@ -1,7 +1,6 @@
package bot
import (
"regexp"
"strconv"
"strings"
@@ -20,24 +19,6 @@ type settingsOld struct {
NoSender bool
}
// Allowed checks if change is allowed
func (s settings) Allowed(noowner bool, userID id.UserID, allowedUsers []*regexp.Regexp) bool {
if !utils.Match(userID.String(), allowedUsers) {
return false
}
if noowner {
return true
}
owner := s.Owner()
if owner == "" {
return true
}
return owner == userID.String()
}
// Get option
func (s settings) Get(key string) string {
value := s[strings.ToLower(strings.TrimSpace(key))]
@@ -123,3 +104,21 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg))
}
// Allowed checks if change is allowed
func (b *Bot) Allowed(userID id.UserID, cfg settings) bool {
if !utils.Match(userID.String(), b.allowedUsers) {
return false
}
if b.noowner {
return true
}
owner := cfg.Owner()
if owner == "" {
return true
}
return owner == userID.String()
}