bot: add ability to change nick

This commit is contained in:
Chris Sexton 2022-08-02 13:26:49 -04:00
parent 3482c0c8e4
commit e92c89891f
6 changed files with 31 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.isAdmin(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>\S+)$`)
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,14 @@ 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 {
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 }