aboutsummaryrefslogtreecommitdiff
path: root/telegram/commands.go
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2022-01-17 22:58:16 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2022-01-17 22:58:16 +0300
commit0b79d6feb1e3d2f87753a1808acbb148a346eeec (patch)
treeb563a67967d696ccb3baa72c3d851c78ee80947d /telegram/commands.go
parente260668f91ee7a1a1fb8ecaa159c6795b9db2eb2 (diff)
Refactor some chat commands
Diffstat (limited to 'telegram/commands.go')
-rw-r--r--telegram/commands.go155
1 files changed, 65 insertions, 90 deletions
diff --git a/telegram/commands.go b/telegram/commands.go
index 8baf068..9a7cf19 100644
--- a/telegram/commands.go
+++ b/telegram/commands.go
@@ -480,12 +480,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}
// create new secret chat with current user
case "secret":
- _, user, err := c.GetContactByID(chatID, nil)
- if err != nil || user == nil {
- return "User not found", true
- }
-
- _, err = c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
+ _, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
UserID: chatID,
})
if err != nil {
@@ -497,36 +492,30 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true
}
- if chatID > 0 {
- _, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
- UserIDs: []int64{chatID},
- Title: args[0],
- })
- if err != nil {
- return err.Error(), true
- }
+ _, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
+ UserIDs: []int64{chatID},
+ Title: args[0],
+ })
+ if err != nil {
+ return err.Error(), true
}
// blacklists current user
case "block":
- if chatID > 0 {
- _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
- Sender: &client.MessageSenderUser{UserID: chatID},
- IsBlocked: true,
- })
- if err != nil {
- return err.Error(), true
- }
+ _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
+ Sender: &client.MessageSenderUser{UserID: chatID},
+ IsBlocked: true,
+ })
+ if err != nil {
+ return err.Error(), true
}
// unblacklists current user
case "unblock":
- if chatID > 0 {
- _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
- Sender: &client.MessageSenderUser{UserID: chatID},
- IsBlocked: false,
- })
- if err != nil {
- return err.Error(), true
- }
+ _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
+ Sender: &client.MessageSenderUser{UserID: chatID},
+ IsBlocked: false,
+ })
+ if err != nil {
+ return err.Error(), true
}
// invite @username to current groupchat
case "invite":
@@ -534,19 +523,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true
}
- if chatID < 0 {
- userID, err := c.usernameOrIDToID(args[0])
- if err != nil {
- return err.Error(), true
- }
+ contact, _, err := c.GetContactByUsername(args[0])
+ if err != nil {
+ return err.Error(), true
+ }
- _, err = c.client.AddChatMember(&client.AddChatMemberRequest{
- ChatID: chatID,
- UserID: userID,
- })
- if err != nil {
- return err.Error(), true
- }
+ _, err = c.client.AddChatMember(&client.AddChatMemberRequest{
+ ChatID: chatID,
+ UserID: contact.ID,
+ ForwardLimit: 100,
+ })
+ if err != nil {
+ return err.Error(), true
}
// kick @username from current group chat
case "kick":
@@ -554,20 +542,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true
}
- if chatID < 0 {
- userID, err := c.usernameOrIDToID(args[0])
- if err != nil {
- return err.Error(), true
- }
+ contact, _, err := c.GetContactByUsername(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
- }
+ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
+ ChatID: chatID,
+ UserID: contact.ID,
+ Status: c.formatRestrict(false, 0),
+ })
+ if err != nil {
+ return err.Error(), true
}
// ban @username from current chat [for N hours]
case "ban":
@@ -575,49 +561,37 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true
}
- if chatID < 0 {
- userID, err := c.usernameOrIDToID(args[0])
- if err != nil {
- return err.Error(), true
- }
-
- var until int32
- if len(args) > 1 {
- hours, err := strconv.ParseInt(args[1], 10, 32)
- if err != nil {
- until = int32(time.Now().Unix() + hours*3600)
- }
- }
+ contact, _, err := c.GetContactByUsername(args[0])
+ if err != nil {
+ return err.Error(), true
+ }
- _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
- ChatID: chatID,
- UserID: userID,
- Status: &client.ChatMemberStatusBanned{
- BannedUntilDate: until,
- },
- })
+ var hours int64
+ if len(args) > 1 {
+ hours, err = strconv.ParseInt(args[1], 10, 32)
if err != nil {
- return err.Error(), true
+ return "Invalid number of hours", true
}
}
+
+ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
+ ChatID: chatID,
+ UserID: contact.ID,
+ Status: c.formatRestrict(true, hours),
+ })
+ if err != nil {
+ return err.Error(), true
+ }
// leave current chat
case "leave":
- chat, _, err := c.GetContactByID(chatID, nil)
+ _, err := c.client.LeaveChat(&client.LeaveChatRequest{
+ ChatID: chatID,
+ })
if err != nil {
return err.Error(), true
}
- chatType := chat.Type.ChatTypeType()
- if chatType == client.TypeChatTypeBasicGroup || chatType == client.TypeChatTypeSupergroup {
- _, err = c.client.LeaveChat(&client.LeaveChatRequest{
- ChatID: chatID,
- })
- if err != nil {
- return err.Error(), true
- }
-
- c.unsubscribe(chatID)
- }
+ c.unsubscribe(chatID)
// close secret chat
case "close":
chat, _, err := c.GetContactByID(chatID, nil)
@@ -642,6 +616,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
_, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{
ChatID: chatID,
RemoveFromChatList: true,
+ Revoke: true,
})
if err != nil {
return err.Error(), true
@@ -650,7 +625,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
c.unsubscribe(chatID)
// message search
case "search":
- var limit int32 = 10
+ var limit int32 = 100
if len(args) > 1 {
newLimit, err := strconv.ParseInt(args[1], 10, 32)
if err == nil {
@@ -697,7 +672,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}
c.sendMessagesReverse(chatID, messages.Messages)
- // members list (for admins)
+ // chat members
case "members":
var query string
if len(args) > 0 {