Add /mute / /unmute commands
This commit is contained in:
parent
de31fa0ad2
commit
9a49d66279
|
@ -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 permissionsMember = client.ChatPermissions{
|
||||
CanSendMessages: true,
|
||||
CanSendMediaMessages: true,
|
||||
CanSendPolls: true,
|
||||
CanSendOtherMessages: true,
|
||||
CanAddWebPagePreviews: true,
|
||||
CanChangeInfo: true,
|
||||
CanInviteUsers: true,
|
||||
CanPinMessages: true,
|
||||
}
|
||||
var permissionsReadonly = client.ChatPermissions{}
|
||||
|
||||
var transportCommands = map[string]command{
|
||||
"login": command{"phone", "sign in"},
|
||||
|
@ -46,6 +57,8 @@ var chatCommands = map[string]command{
|
|||
"invite": command{"id or @username", "add user to current chat"},
|
||||
"link": command{"", "get invite link for current chat"},
|
||||
"kick": command{"id or @username", "remove user to current chat"},
|
||||
"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"},
|
||||
"leave": command{"", "leave current chat"},
|
||||
"close": command{"", "close current secret chat"},
|
||||
|
@ -560,7 +573,61 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
|
||||
ChatId: chatID,
|
||||
MemberId: &client.MessageSenderUser{UserId: contact.Id},
|
||||
Status: c.formatRestrict(false, 0),
|
||||
Status: &client.ChatMemberStatusLeft{},
|
||||
})
|
||||
if err != nil {
|
||||
return err.Error(), true
|
||||
}
|
||||
// mute @username [n hours]
|
||||
case "mute":
|
||||
if len(args) < 1 {
|
||||
return notEnoughArguments, true
|
||||
}
|
||||
|
||||
contact, _, err := c.GetContactByUsername(args[0])
|
||||
if err != nil {
|
||||
return err.Error(), true
|
||||
}
|
||||
|
||||
var hours int64
|
||||
if len(args) > 1 {
|
||||
hours, err = strconv.ParseInt(args[1], 10, 32)
|
||||
if err != nil {
|
||||
return "Invalid number of hours", true
|
||||
}
|
||||
}
|
||||
|
||||
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
|
||||
ChatId: chatID,
|
||||
MemberId: &client.MessageSenderUser{UserId: contact.Id},
|
||||
Status: &client.ChatMemberStatusRestricted{
|
||||
IsMember: true,
|
||||
RestrictedUntilDate: c.formatBantime(hours),
|
||||
Permissions: &permissionsReadonly,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err.Error(), true
|
||||
}
|
||||
// unmute @username
|
||||
case "unmute":
|
||||
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.ChatMemberStatusRestricted{
|
||||
IsMember: true,
|
||||
RestrictedUntilDate: 0,
|
||||
Permissions: &permissionsMember,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err.Error(), true
|
||||
|
@ -587,7 +654,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
|
||||
ChatId: chatID,
|
||||
MemberId: &client.MessageSenderUser{UserId: contact.Id},
|
||||
Status: c.formatRestrict(true, hours),
|
||||
Status: &client.ChatMemberStatusBanned{
|
||||
BannedUntilDate: c.formatBantime(hours),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err.Error(), true
|
||||
|
|
|
@ -339,19 +339,13 @@ func (c *Client) formatFilePath(basedir string, id string, ext string) string {
|
|||
return fmt.Sprintf("%s/%x%s", basedir, sha256.Sum256([]byte(id)), ext)
|
||||
}
|
||||
|
||||
func (c *Client) formatRestrict(ban bool, hours int64) client.ChatMemberStatus {
|
||||
func (c *Client) formatBantime(hours int64) int32 {
|
||||
var until int32
|
||||
if hours != 0 {
|
||||
if hours > 0 {
|
||||
until = int32(time.Now().Unix() + hours*3600)
|
||||
}
|
||||
|
||||
if ban {
|
||||
return &client.ChatMemberStatusBanned{
|
||||
BannedUntilDate: until,
|
||||
}
|
||||
} else {
|
||||
return &client.ChatMemberStatusLeft{}
|
||||
}
|
||||
return until
|
||||
}
|
||||
|
||||
func (c *Client) messageToText(message *client.Message) string {
|
||||
|
|
Loading…
Reference in a new issue