Add /kick command

This commit is contained in:
bodqhrohro 2019-12-08 15:58:17 +02:00
parent 9f54309b30
commit 4b099fba41

View file

@ -112,6 +112,26 @@ func parseCommand(cmdline string) (string, []string) {
return bodyFields[0][1:], bodyFields[1:]
}
func (c *Client) usernameOrIdToId(username string) (int32, error) {
userID, err := strconv.ParseInt(username, 10, 32)
// couldn't parse the id, try to lookup as a username
if err != nil {
chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
Username: username,
})
if err != nil {
return 0, err
}
userID = chat.Id
if userID <= 0 || userID > math.MaxInt32 {
return 0, errors.New("Not a user")
}
}
return int32(userID), nil
}
// ProcessTransportCommand executes a command sent directly to the component
// and returns a response
func (c *Client) ProcessTransportCommand(cmdline string) string {
@ -451,25 +471,35 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}
if chatID < 0 {
userID, err := strconv.ParseInt(args[0], 10, 32)
// couldn't parse the id, try to lookup as a username
if err != nil {
chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
Username: args[0],
})
userID, err := c.usernameOrIdToId(args[0])
if err != nil {
return err.Error(), true
}
userID = chat.Id
if userID <= 0 || userID > math.MaxInt32 {
return "Not a user", true
}
}
_, err = c.client.AddChatMember(&client.AddChatMemberRequest{
ChatId: chatID,
UserId: int32(userID),
UserId: userID,
})
if err != nil {
return err.Error(), true
}
}
// kick @username from current group chat
case "kick":
if len(args) < 1 {
return notEnoughArguments, true
}
if chatID < 0 {
userID, err := c.usernameOrIdToId(args[0])
if err != nil {
return err.Error(), true
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID,
UserId: userID,
Status: &client.ChatMemberStatusLeft{},
})
if err != nil {
return err.Error(), true