From e92c89891f8faa53b15b678e8d2459cf2406c70a Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Tue, 2 Aug 2022 13:26:49 -0400 Subject: [PATCH] bot: add ability to change nick --- bot/interfaces.go | 3 +++ connectors/discord/discord.go | 5 +++++ connectors/irc/irc.go | 5 +++++ connectors/slackapp/slackApp.go | 4 ++++ plugins/admin/admin.go | 13 +++++++++++++ plugins/cli/cli.go | 1 + 6 files changed, 31 insertions(+) diff --git a/bot/interfaces.go b/bot/interfaces.go index 73ac9d2..744827b 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -226,6 +226,9 @@ type Connector interface { // SetRole toggles a role on/off for a user by ID 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 diff --git a/connectors/discord/discord.go b/connectors/discord/discord.go index 01e1a12..a7f46ac 100644 --- a/connectors/discord/discord.go +++ b/connectors/discord/discord.go @@ -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) +} diff --git a/connectors/irc/irc.go b/connectors/irc/irc.go index ce97fdb..52a9761 100644 --- a/connectors/irc/irc.go +++ b/connectors/irc/irc.go @@ -361,3 +361,8 @@ func (i Irc) SetRole(userID, roleID string) error { } 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") +} diff --git a/connectors/slackapp/slackApp.go b/connectors/slackapp/slackApp.go index 7ab5212..641ba05 100644 --- a/connectors/slackapp/slackApp.go +++ b/connectors/slackapp/slackApp.go @@ -751,3 +751,7 @@ func (s *SlackApp) SetRole(userID, roleID string) error { } func (s *SlackApp) Shutdown() {} + +func (s *SlackApp) Nick(nick string) error { + return s.api.SetUserRealName(nick) +} diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index 95a7dad..f65ddce 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -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, setKeyConfigRegex, p.isAdmin(p.setKeyConfigCmd)) 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) p.registerWeb() @@ -106,6 +107,7 @@ var setConfigRegex = regexp.MustCompile(`(?i)^set (?P\S+) (?P.*)$`) var pushConfigRegex = regexp.MustCompile(`(?i)^push (?P\S+) (?P.*)$`) var setKeyConfigRegex = regexp.MustCompile(`(?i)^setkey (?P\S+) (?P\S+) (?P.*)$`) var getConfigRegex = regexp.MustCompile(`(?i)^get (?P\S+)$`) +var setNickRegex = regexp.MustCompile(`(?i)^nick (?P\S+)$`) func (p *AdminPlugin) isAdmin(rh bot.ResponseHandler) bot.ResponseHandler { 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) 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 +} diff --git a/plugins/cli/cli.go b/plugins/cli/cli.go index b87b08b..9df5277 100644 --- a/plugins/cli/cli.go +++ b/plugins/cli/cli.go @@ -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) GetRoles() ([]bot.Role, error) { return []bot.Role{}, nil } func (p *CliPlugin) SetRole(userID, roleID string) error { return nil } +func (p *CliPlugin) Nick(string) error { return nil }