diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2024-02-15 12:40:57 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2024-02-15 12:40:57 +0300 |
commit | 9b5fee88262f22ad14acf621e992167966af278a (patch) | |
tree | c6ef439abf2e3826bea45e7bcdc1b15366c34f0f /telegram/commands.go | |
parent | dc6f99dc3ca0906bfd5f9bda9eab618445cfa878 (diff) |
Filter available commands by chat type
Diffstat (limited to 'telegram/commands.go')
-rw-r--r-- | telegram/commands.go | 127 |
1 files changed, 77 insertions, 50 deletions
diff --git a/telegram/commands.go b/telegram/commands.go index 8d4de91..a01a80e 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -50,56 +50,60 @@ var permissionsMember = client.ChatPermissions{ var permissionsReadonly = client.ChatPermissions{} var transportCommands = map[string]command{ - "help": command{0, []string{}, "help"}, - "login": command{1, []string{"phone"}, "sign in"}, - "logout": command{0, []string{}, "sign out"}, - "cancelauth": command{0, []string{}, "quit the signin wizard"}, - "code": command{1, []string{"xxxxx"}, "check one-time code"}, - "password": command{1, []string{"********"}, "check 2fa password"}, - "setusername": command{0, []string{"@username"}, "update @username"}, - "setname": command{1, []string{"first", "last"}, "update name"}, - "setbio": command{0, []string{"Lorem ipsum"}, "update about"}, - "setpassword": command{0, []string{"old", "new"}, "set or remove password"}, - "config": command{0, []string{"param", "value"}, "view or update configuration options"}, - "report": command{2, []string{"chat", "comment"}, "report a chat by id or @username"}, - "add": command{1, []string{"@username"}, "add @username to your chat list"}, - "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname"}, - "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»"}, - "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»"}, + "help": command{0, []string{}, "help", nil}, + "login": command{1, []string{"phone"}, "sign in", nil}, + "logout": command{0, []string{}, "sign out", nil}, + "cancelauth": command{0, []string{}, "quit the signin wizard", nil}, + "code": command{1, []string{"xxxxx"}, "check one-time code", nil}, + "password": command{1, []string{"********"}, "check 2fa password", nil}, + "setusername": command{0, []string{"@username"}, "update @username", nil}, + "setname": command{1, []string{"first", "last"}, "update name", nil}, + "setbio": command{0, []string{"Lorem ipsum"}, "update about", nil}, + "setpassword": command{0, []string{"old", "new"}, "set or remove password", nil}, + "config": command{0, []string{"param", "value"}, "view or update configuration options", nil}, + "report": command{2, []string{"chat", "comment"}, "report a chat by id or @username", nil}, + "add": command{1, []string{"@username"}, "add @username to your chat list", nil}, + "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", nil}, + "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", nil}, + "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", nil}, } +var notForGroups = []ChatType{ChatTypeBasicGroup, ChatTypeSupergroup, ChatTypeChannel} +var notForPM = []ChatType{ChatTypePrivate, ChatTypeSecret} +var onlyForSecret = []ChatType{ChatTypePrivate, ChatTypeBasicGroup, ChatTypeSupergroup, ChatTypeChannel} + var chatCommands = map[string]command{ - "help": command{0, []string{}, "help"}, - "d": command{0, []string{"n"}, "delete your last message(s)"}, - "s": command{1, []string{"edited message"}, "edit your last message"}, - "silent": command{1, []string{"message"}, "send a message without sound"}, - "schedule": command{2, []string{"{online | 2006-01-02T15:04:05 | 15:04:05}", "message"}, "schedules a message either to timestamp or to whenever the user goes online"}, - "forward": command{2, []string{"message_id", "target_chat"}, "forwards a message"}, - "vcard": command{0, []string{}, "print vCard as text"}, - "add": command{1, []string{"@username"}, "add @username to your chat list"}, - "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname"}, - "group": command{1, []string{"title"}, "create groupchat «title» with current user"}, - "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»"}, - "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»"}, - "secret": command{0, []string{}, "create secretchat with current user"}, - "search": command{0, []string{"string", "[limit]"}, "search <string> in current chat"}, - "history": command{0, []string{"limit"}, "get last [limit] messages from current chat"}, - "block": command{0, []string{}, "blacklist current user"}, - "unblock": command{0, []string{}, "unblacklist current user"}, - "invite": command{1, []string{"id or @username"}, "add user to current chat"}, - "link": command{0, []string{}, "get invite link for current chat"}, - "kick": command{1, []string{"id or @username"}, "remove user to current chat"}, - "mute": command{1, []string{"id or @username", "hours"}, "mute user in current chat"}, - "unmute": command{1, []string{"id or @username"}, "unrestrict user from current chat"}, - "ban": command{1, []string{"id or @username", "hours"}, "restrict @username from current chat for [hours] or forever"}, - "unban": command{1, []string{"id or @username"}, "unbans @username in current chat (and devotes from admins)"}, - "promote": command{1, []string{"id or @username", "title"}, "promote user to admin in current chat"}, - "leave": command{0, []string{}, "leave current chat"}, - "leave!": command{0, []string{}, "leave current chat (for owners)"}, - "ttl": command{0, []string{"seconds"}, "set secret chat messages TTL before self-destroying"}, - "close": command{0, []string{}, "close current secret chat"}, - "delete": command{0, []string{}, "delete current chat from chat list"}, - "members": command{0, []string{"query"}, "search members [by optional query] in current chat (requires admin rights)"}, + "help": command{0, []string{}, "help", nil}, + "d": command{0, []string{"n"}, "delete your last message(s)", nil}, + "s": command{1, []string{"edited message"}, "edit your last message", nil}, + "silent": command{1, []string{"message"}, "send a message without sound", nil}, + "schedule": command{2, []string{"{online | 2006-01-02T15:04:05 | 15:04:05}", "message"}, "schedules a message either to timestamp or to whenever the user goes online", nil}, + "forward": command{2, []string{"message_id", "target_chat"}, "forwards a message", nil}, + "vcard": command{0, []string{}, "print vCard as text", nil}, + "add": command{1, []string{"@username"}, "add @username to your chat list", nil}, + "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", nil}, + "group": command{1, []string{"title"}, "create groupchat «title» with current user", ¬ForGroups}, + "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", nil}, + "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", nil}, + "secret": command{0, []string{}, "create secretchat with current user", ¬ForGroups}, + "search": command{0, []string{"string", "[limit]"}, "search <string> in current chat", nil}, + "history": command{0, []string{"limit"}, "get last [limit] messages from current chat", nil}, + "block": command{0, []string{}, "blacklist current user", ¬ForGroups}, + "unblock": command{0, []string{}, "unblacklist current user", ¬ForGroups}, + "invite": command{1, []string{"id or @username"}, "add user to current chat", ¬ForPM}, + "link": command{0, []string{}, "get invite link for current chat", ¬ForPM}, + "kick": command{1, []string{"id or @username"}, "remove user from current chat", ¬ForPM}, + "mute": command{1, []string{"id or @username", "hours"}, "mute user in current chat", ¬ForPM}, + "unmute": command{1, []string{"id or @username"}, "unrestrict user from current chat", ¬ForPM}, + "ban": command{1, []string{"id or @username", "hours"}, "restrict @username from current chat for [hours] or forever", ¬ForPM}, + "unban": command{1, []string{"id or @username"}, "unbans @username in current chat (and devotes from admins)", ¬ForPM}, + "promote": command{1, []string{"id or @username", "title"}, "promote user to admin in current chat", ¬ForPM}, + "leave": command{0, []string{}, "leave current chat", ¬ForPM}, + "leave!": command{0, []string{}, "leave current chat (for owners)", ¬ForPM}, + "ttl": command{0, []string{"seconds"}, "set secret chat messages TTL before self-destroying", &onlyForSecret}, + "close": command{0, []string{}, "close current secret chat", &onlyForSecret}, + "delete": command{0, []string{}, "delete current chat from chat list", nil}, + "members": command{0, []string{"query"}, "search members [by optional query] in current chat (requires admin rights)", nil}, } var transportConfigurationOptions = map[string]configurationOption{ @@ -112,6 +116,7 @@ type command struct { RequiredArgs int Arguments []string Description string + NotFor *[]ChatType } type configurationOption struct { arguments string @@ -185,14 +190,31 @@ func CommandToHelpString(name string, cmd command) string { return str.String() } -func helpString(typ CommandType) string { +// IsCommandFor checks the suitability of a command for a chat type +func IsCommandForChatType(cmd command, chatType ChatType) bool { + if cmd.NotFor != nil { + for _, typ := range *cmd.NotFor { + if chatType == typ { + return false + } + } + } + + return true +} + +func (c *Client) helpString(typ CommandType, chatId int64) string { var str strings.Builder commandMap := GetCommands(typ) + chatType, chatTypeErr := c.GetChatType(chatId) str.WriteString("Available commands:\n") for _, name := range SortedCommandKeys(commandMap) { command := commandMap[name] + if chatTypeErr == nil && !IsCommandForChatType(command, chatType) { + continue + } str.WriteString(CommandToHelpString(name, command)) str.WriteString("\n") } @@ -502,7 +524,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string case "channel": return c.cmdChannel(args, cmdline) case "help": - return helpString(CommandTypeTransport) + return c.helpString(CommandTypeTransport, 0) } return "" @@ -524,6 +546,11 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return notEnoughArguments, true } + chatType, chatTypeErr := c.GetChatType(chatID) + if chatTypeErr == nil && !IsCommandForChatType(command, chatType) { + return "Not applicable for this chat type", true + } + switch cmd { // delete message case "d": @@ -1103,7 +1130,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return strings.Join(entries, "\n"), true case "help": - return helpString(CommandTypeChat), true + return c.helpString(CommandTypeChat, chatID), true default: return "", false } |