Refactor some chat commands
This commit is contained in:
parent
e260668f91
commit
0b79d6feb1
|
@ -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,7 +492,6 @@ 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],
|
||||
|
@ -505,10 +499,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
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,
|
||||
|
@ -516,10 +508,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
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,
|
||||
|
@ -527,89 +517,74 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
if err != nil {
|
||||
return err.Error(), true
|
||||
}
|
||||
}
|
||||
// invite @username to current groupchat
|
||||
case "invite":
|
||||
if len(args) < 1 {
|
||||
return notEnoughArguments, true
|
||||
}
|
||||
|
||||
if chatID < 0 {
|
||||
userID, err := c.usernameOrIDToID(args[0])
|
||||
contact, _, err := c.GetContactByUsername(args[0])
|
||||
if err != nil {
|
||||
return err.Error(), true
|
||||
}
|
||||
|
||||
_, err = c.client.AddChatMember(&client.AddChatMemberRequest{
|
||||
ChatID: chatID,
|
||||
UserID: userID,
|
||||
UserID: contact.ID,
|
||||
ForwardLimit: 100,
|
||||
})
|
||||
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])
|
||||
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{},
|
||||
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":
|
||||
if len(args) < 1 {
|
||||
return notEnoughArguments, true
|
||||
}
|
||||
|
||||
if chatID < 0 {
|
||||
userID, err := c.usernameOrIDToID(args[0])
|
||||
contact, _, err := c.GetContactByUsername(args[0])
|
||||
if err != nil {
|
||||
return err.Error(), true
|
||||
}
|
||||
|
||||
var until int32
|
||||
var hours int64
|
||||
if len(args) > 1 {
|
||||
hours, err := strconv.ParseInt(args[1], 10, 32)
|
||||
hours, err = strconv.ParseInt(args[1], 10, 32)
|
||||
if err != nil {
|
||||
until = int32(time.Now().Unix() + hours*3600)
|
||||
return "Invalid number of hours", true
|
||||
}
|
||||
}
|
||||
|
||||
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
|
||||
ChatID: chatID,
|
||||
UserID: userID,
|
||||
Status: &client.ChatMemberStatusBanned{
|
||||
BannedUntilDate: until,
|
||||
},
|
||||
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)
|
||||
if err != nil {
|
||||
return err.Error(), true
|
||||
}
|
||||
|
||||
chatType := chat.Type.ChatTypeType()
|
||||
if chatType == client.TypeChatTypeBasicGroup || chatType == client.TypeChatTypeSupergroup {
|
||||
_, err = c.client.LeaveChat(&client.LeaveChatRequest{
|
||||
_, err := c.client.LeaveChat(&client.LeaveChatRequest{
|
||||
ChatID: chatID,
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -617,7 +592,6 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -312,6 +312,21 @@ func (c *Client) formatContent(file *client.File, filename string) string {
|
|||
)
|
||||
}
|
||||
|
||||
func (c *Client) formatRestrict(ban bool, hours int64) client.ChatMemberStatus {
|
||||
var until int32
|
||||
if hours != 0 {
|
||||
until = int32(time.Now().Unix() + hours*3600)
|
||||
}
|
||||
|
||||
if ban {
|
||||
return &client.ChatMemberStatusBanned{
|
||||
BannedUntilDate: until,
|
||||
}
|
||||
} else {
|
||||
return &client.ChatMemberStatusLeft{}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) messageToText(message *client.Message) string {
|
||||
if message.Content == nil {
|
||||
log.Warnf("Unknown message (big emoji?): %#v", message)
|
||||
|
|
Loading…
Reference in a new issue