From 6701335302e5f03402d10aa53984c7c2bd9c4bf3 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Thu, 3 Feb 2022 13:51:27 -0500 Subject: [PATCH] Add /unban / /promote commands --- telegram/commands.go | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/telegram/commands.go b/telegram/commands.go index 238b774..fc3064c 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -17,6 +17,17 @@ import ( const notEnoughArguments string = "Not enough arguments" const telegramNotInitialized string = "Telegram connection is not initialized yet" const notOnline string = "Not online" +var permissionsAdmin = client.ChatMemberStatusAdministrator{ + CanBeEdited: true, + CanChangeInfo: true, + CanPostMessages: true, + CanEditMessages: true, + CanDeleteMessages: true, + CanInviteUsers: true, + CanRestrictMembers: true, + CanPinMessages: true, + CanPromoteMembers: false, +} var permissionsMember = client.ChatPermissions{ CanSendMessages: true, CanSendMediaMessages: true, @@ -60,6 +71,8 @@ var chatCommands = map[string]command{ "mute": command{"id or @username [hours]", "mute user in current chat"}, "unmute": command{"id or @username", "unrestrict user from current chat"}, "ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"}, + "unban": command{"id or @username", "unbans @username in current chat (and devotes from admins)"}, + "promote": command{"id or @username [title]", "promote user to admin in current chat"}, "leave": command{"", "leave current chat"}, "close": command{"", "close current secret chat"}, "delete": command{"", "delete current chat from chat list"}, @@ -661,6 +674,51 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) if err != nil { return err.Error(), true } + // unban @username + case "unban": + if len(args) < 1 { + return notEnoughArguments, true + } + + contact, _, err := c.GetContactByUsername(args[0]) + if err != nil { + return err.Error(), true + } + + _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ + ChatId: chatID, + MemberId: &client.MessageSenderUser{UserId: contact.Id}, + Status: &client.ChatMemberStatusMember{}, + }) + if err != nil { + return err.Error(), true + } + // promote @username to admin + case "promote": + if len(args) < 1 { + return notEnoughArguments, true + } + + contact, _, err := c.GetContactByUsername(args[0]) + if err != nil { + return err.Error(), true + } + + // clone the permissions + status := permissionsAdmin + + if len(args) > 1 { + status.CustomTitle = args[1] + } + + _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ + ChatId: chatID, + MemberId: &client.MessageSenderUser{UserId: contact.Id}, + Status: &status, + }) + if err != nil { + return err.Error(), true + } // leave current chat case "leave": _, err := c.client.LeaveChat(&client.LeaveChatRequest{