From bb7288167d822ac878c95e3fb317e4da4210b505 Mon Sep 17 00:00:00 2001 From: Matte23 Date: Thu, 23 Apr 2020 11:33:11 +0200 Subject: [PATCH] Add support for usernames with whitespaces --- cmd/innkeeperbot/main.go | 42 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/cmd/innkeeperbot/main.go b/cmd/innkeeperbot/main.go index 492adf6..8f078f9 100644 --- a/cmd/innkeeperbot/main.go +++ b/cmd/innkeeperbot/main.go @@ -155,10 +155,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // Parameters should contains the channel name and the username parameters := strings.Fields(m.Content) if len(parameters) < 2 { - s.ChannelMessageSend(m.ChannelID, "Please specify the channel name") + s.ChannelMessageSend(m.ChannelID, "Please specify the channel name and the username") return } + username := strings.TrimPrefix(m.Content, parameters[0]+" "+parameters[1]+" ") + category := searchChannel(s, m.GuildID, "Personalizzato", "") if category == nil { s.ChannelMessageSend(m.ChannelID, "Unable to delete channel, category not found") @@ -170,9 +172,9 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - user := searchUser(s, m.GuildID, parameters[2]) + user := searchUser(s, m.GuildID, username) if user == nil { - s.ChannelMessageSend(m.ChannelID, "User "+parameters[2]+" not found") + s.ChannelMessageSend(m.ChannelID, "User "+username+" not found") return } @@ -191,7 +193,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if authorized { s.ChannelPermissionSet(channel.ID, user.ID, "member", 0x00100000, 0) - s.ChannelMessageSend(m.ChannelID, "User "+parameters[2]+" can now join "+parameters[1]) + s.ChannelMessageSend(m.ChannelID, "User "+username+" can now join "+parameters[1]) } else { s.ChannelMessageSend(m.ChannelID, "You don't have the permission to authorize an user to join "+parameters[1]) } @@ -202,10 +204,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // Parameters should contains the channel name and the username parameters := strings.Fields(m.Content) if len(parameters) < 2 { - s.ChannelMessageSend(m.ChannelID, "Please specify the channel name") + s.ChannelMessageSend(m.ChannelID, "Please specify the channel name and the username") return } + username := strings.TrimPrefix(m.Content, parameters[0]+" "+parameters[1]+" ") + category := searchChannel(s, m.GuildID, "Personalizzato", "") if category == nil { @@ -218,9 +222,9 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - user := searchUser(s, m.GuildID, parameters[2]) + user := searchUser(s, m.GuildID, username) if user == nil { - s.ChannelMessageSend(m.ChannelID, "User "+parameters[2]+" not found") + s.ChannelMessageSend(m.ChannelID, "User "+username+" not found") return } @@ -239,7 +243,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if authorized { s.ChannelPermissionDelete(channel.ID, user.ID) - s.ChannelMessageSend(m.ChannelID, "User "+parameters[2]+" cannot join "+parameters[1]+" anymore") + s.ChannelMessageSend(m.ChannelID, "User "+username+" cannot join "+parameters[1]+" anymore") } else { s.ChannelMessageSend(m.ChannelID, "You don't have the permission to revoke the authorization of an user to join "+parameters[1]) } @@ -251,10 +255,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // Parameters should contains the channel name and the username parameters := strings.Fields(m.Content) if len(parameters) < 2 { - s.ChannelMessageSend(m.ChannelID, "Please specify the channel name") + s.ChannelMessageSend(m.ChannelID, "Please specify the channel name and the username") return } + username := strings.TrimPrefix(m.Content, parameters[0]+" "+parameters[1]+" ") + category := searchChannel(s, m.GuildID, "Personalizzato", "") if category == nil { s.ChannelMessageSend(m.ChannelID, "Unable to delete channel, category not found") @@ -266,9 +272,9 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - user := searchUser(s, m.GuildID, parameters[2]) + user := searchUser(s, m.GuildID, username) if user == nil { - s.ChannelMessageSend(m.ChannelID, "User "+parameters[2]+" not found") + s.ChannelMessageSend(m.ChannelID, "User "+username+" not found") return } @@ -287,21 +293,23 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if authorized { s.ChannelPermissionSet(channel.ID, user.ID, "member", 0x00100000|0x00000010, 0) - s.ChannelMessageSend(m.ChannelID, "User "+parameters[2]+" can now manage "+parameters[1]) + s.ChannelMessageSend(m.ChannelID, "User "+username+" can now manage "+parameters[1]) } else { s.ChannelMessageSend(m.ChannelID, "You don't have the permission to authorize an user to manage "+parameters[1]) } } - // If the message starts with "!rem", remove from an user the permission to manage the channel. Parameters: channel name, user name + // If the message starts with "!deop", remove from an user the permission to manage the channel. Parameters: channel name, user name if strings.HasPrefix(m.Content, "!deop") { // Parameters should contains the channel name and the username parameters := strings.Fields(m.Content) if len(parameters) < 2 { - s.ChannelMessageSend(m.ChannelID, "Please specify the channel name") + s.ChannelMessageSend(m.ChannelID, "Please specify the channel name and the username") return } + username := strings.TrimPrefix(m.Content, parameters[0]+" "+parameters[1]+" ") + category := searchChannel(s, m.GuildID, "Personalizzato", "") if category == nil { @@ -314,9 +322,9 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - user := searchUser(s, m.GuildID, parameters[2]) + user := searchUser(s, m.GuildID, username) if user == nil { - s.ChannelMessageSend(m.ChannelID, "User "+parameters[2]+" not found") + s.ChannelMessageSend(m.ChannelID, "User "+username+" not found") return } @@ -335,7 +343,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if authorized { s.ChannelPermissionSet(channel.ID, user.ID, "member", 0x00100000, 0) - s.ChannelMessageSend(m.ChannelID, "User "+parameters[2]+" cannot manage "+parameters[1]+" anymore") + s.ChannelMessageSend(m.ChannelID, "User "+username+" cannot manage "+parameters[1]+" anymore") } else { s.ChannelMessageSend(m.ChannelID, "You don't have the permission to revoke the authorization of an user to manage "+parameters[1]) }