-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathadvertisement.go
More file actions
63 lines (53 loc) · 1.57 KB
/
advertisement.go
File metadata and controls
63 lines (53 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package slackbot
import (
"fmt"
"slices"
"strings"
"github.com/keybase/go-keybase-chat-bot/kbchat/types/chat1"
)
type commandAdvertiser interface {
AdvertiseCommands(commands []chat1.UserBotCommandInput) error
}
func (b *Bot) AddAdvertisements(commands ...chat1.UserBotCommandInput) {
b.advertisements = append(b.advertisements, commands...)
}
func (b *Bot) AdvertisedCommands() []chat1.UserBotCommandInput {
commands := []chat1.UserBotCommandInput{{
Name: "help",
Description: "Show available commands",
Usage: fmt.Sprintf("!%s help", b.name),
ExtendedDescription: b.helpExtendedDescription(),
}}
for _, trigger := range b.triggers() {
command := b.commands[trigger]
commands = append(commands, chat1.UserBotCommandInput{
Name: trigger,
Description: command.Description(),
Usage: fmt.Sprintf("!%s %s", b.name, trigger),
})
}
extras := slices.Clone(b.advertisements)
slices.SortFunc(extras, func(a, b chat1.UserBotCommandInput) int {
return strings.Compare(a.Name, b.Name)
})
commands = append(commands, extras...)
return commands
}
func (b *Bot) advertiseCommands() error {
advertiser, ok := b.backend.(commandAdvertiser)
if !ok {
return nil
}
return advertiser.AdvertiseCommands(b.AdvertisedCommands())
}
func (b *Bot) helpExtendedDescription() *chat1.UserBotExtendedDescription {
help := strings.TrimSpace(b.resolvedHelp())
if help == "" {
return nil
}
return &chat1.UserBotExtendedDescription{
Title: fmt.Sprintf("%s help", b.name),
DesktopBody: help,
MobileBody: help,
}
}