Compare commits

...

3 Commits

Author SHA1 Message Date
Chris Sexton 45103cec62 admin: fix nick regex 2022-08-02 13:42:35 -04:00
Chris Sexton 7af94f3473 admin: conditionally require admin for nick change 2022-08-02 13:35:50 -04:00
Chris Sexton e92c89891f bot: add ability to change nick 2022-08-02 13:35:50 -04:00
6 changed files with 34 additions and 0 deletions

View File

@ -226,6 +226,9 @@ type Connector interface {
// SetRole toggles a role on/off for a user by ID // SetRole toggles a role on/off for a user by ID
SetRole(userID, roleID string) error SetRole(userID, roleID string) error
// Nick sets the username of the bot on the server
Nick(string) error
} }
// Plugin interface used for compatibility with the Plugin interface // Plugin interface used for compatibility with the Plugin interface

View File

@ -423,3 +423,8 @@ func (d *Discord) Shutdown() {
} }
} }
} }
func (d *Discord) Nick(nick string) error {
guildID := d.config.Get("discord.guildid", "")
return d.client.GuildMemberNickname(guildID, "@me", nick)
}

View File

@ -361,3 +361,8 @@ func (i Irc) SetRole(userID, roleID string) error {
} }
func (i Irc) Shutdown() {} func (i Irc) Shutdown() {}
func (i Irc) Nick(nick string) error {
// Yeah, I could figure this out, but I don't want to test/debug it
return fmt.Errorf("nick changes not supported on irc")
}

View File

@ -751,3 +751,7 @@ func (s *SlackApp) SetRole(userID, roleID string) error {
} }
func (s *SlackApp) Shutdown() {} func (s *SlackApp) Shutdown() {}
func (s *SlackApp) Nick(nick string) error {
return s.api.SetUserRealName(nick)
}

View File

@ -57,6 +57,7 @@ func New(b bot.Bot) *AdminPlugin {
b.RegisterRegexCmd(p, bot.Message, pushConfigRegex, p.isAdmin(p.pushConfigCmd)) b.RegisterRegexCmd(p, bot.Message, pushConfigRegex, p.isAdmin(p.pushConfigCmd))
b.RegisterRegexCmd(p, bot.Message, setKeyConfigRegex, p.isAdmin(p.setKeyConfigCmd)) b.RegisterRegexCmd(p, bot.Message, setKeyConfigRegex, p.isAdmin(p.setKeyConfigCmd))
b.RegisterRegexCmd(p, bot.Message, getConfigRegex, p.isAdmin(p.getConfigCmd)) b.RegisterRegexCmd(p, bot.Message, getConfigRegex, p.isAdmin(p.getConfigCmd))
b.RegisterRegexCmd(p, bot.Message, setNickRegex, p.setNick)
b.Register(p, bot.Help, p.help) b.Register(p, bot.Help, p.help)
p.registerWeb() p.registerWeb()
@ -106,6 +107,7 @@ var setConfigRegex = regexp.MustCompile(`(?i)^set (?P<key>\S+) (?P<value>.*)$`)
var pushConfigRegex = regexp.MustCompile(`(?i)^push (?P<key>\S+) (?P<value>.*)$`) var pushConfigRegex = regexp.MustCompile(`(?i)^push (?P<key>\S+) (?P<value>.*)$`)
var setKeyConfigRegex = regexp.MustCompile(`(?i)^setkey (?P<key>\S+) (?P<name>\S+) (?P<value>.*)$`) var setKeyConfigRegex = regexp.MustCompile(`(?i)^setkey (?P<key>\S+) (?P<name>\S+) (?P<value>.*)$`)
var getConfigRegex = regexp.MustCompile(`(?i)^get (?P<key>\S+)$`) var getConfigRegex = regexp.MustCompile(`(?i)^get (?P<key>\S+)$`)
var setNickRegex = regexp.MustCompile(`(?i)^nick (?P<nick>.+)$`)
func (p *AdminPlugin) isAdmin(rh bot.ResponseHandler) bot.ResponseHandler { func (p *AdminPlugin) isAdmin(rh bot.ResponseHandler) bot.ResponseHandler {
return func(r bot.Request) bool { return func(r bot.Request) bool {
@ -407,3 +409,17 @@ func (p *AdminPlugin) modList(query, channel, plugin string) error {
err := fmt.Errorf("unknown plugin named '%s'", plugin) err := fmt.Errorf("unknown plugin named '%s'", plugin)
return err return err
} }
func (p *AdminPlugin) setNick(r bot.Request) bool {
if needAdmin := p.cfg.GetInt("nick.needsadmin", 1); needAdmin == 1 && !p.bot.CheckAdmin(r.Msg.User.ID) {
return false
}
nick := r.Values["nick"]
if err := r.Conn.Nick(nick); err != nil {
log.Error().Err(err).Msg("set nick")
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I can't seem to set a new nick.")
return true
}
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I shall now be known as %s.", nick))
return true
}

View File

@ -140,3 +140,4 @@ func (p *CliPlugin) GetChannelName(id string) string { return id }
func (p *CliPlugin) GetChannelID(name string) string { return name } func (p *CliPlugin) GetChannelID(name string) string { return name }
func (p *CliPlugin) GetRoles() ([]bot.Role, error) { return []bot.Role{}, nil } func (p *CliPlugin) GetRoles() ([]bot.Role, error) { return []bot.Role{}, nil }
func (p *CliPlugin) SetRole(userID, roleID string) error { return nil } func (p *CliPlugin) SetRole(userID, roleID string) error { return nil }
func (p *CliPlugin) Nick(string) error { return nil }