Skip to content

Commit 5c122d6

Browse files
authored
helps fixed (#5)
* helps fixed * fixed help command
1 parent a306048 commit 5c122d6

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

bot/bot.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ const (
1212
UnknownUpdateType UpdateType = "unknown"
1313
)
1414

15+
type Help struct {
16+
commandList []string
17+
}
18+
19+
func (h *Help) Add(cmd, text string) {
20+
h.commandList = append(h.commandList, fmt.Sprintf("- /%s: %s", cmd, text))
21+
}
22+
23+
func (h *Help) List() []string {
24+
return h.commandList
25+
}
26+
27+
func NewHelp() *Help {
28+
return &Help{commandList: []string{}}
29+
}
30+
1531
type UpdateType string
1632

1733
type MetricsExporter interface {
@@ -61,14 +77,14 @@ type Bot struct {
6177
commands map[string]func(c *BotContext)
6278
messenger Messenger
6379
newMembersID map[int64]int
64-
helps []string
80+
help *Help
6581
}
6682

67-
func New(messenger Messenger, helps []string, newMembersID map[int64]int) *Bot {
83+
func New(messenger Messenger, help *Help, newMembersID map[int64]int) *Bot {
6884
bot := &Bot{
6985
messenger: messenger,
7086
commands: make(map[string]func(c *BotContext)),
71-
helps: helps,
87+
help: help,
7288
newMembersID: newMembersID,
7389
checkers: []func(c *BotContext){},
7490
}
@@ -114,7 +130,7 @@ func (b *Bot) BanUser(chatID int64, userID int64) {
114130
func (b *Bot) Command(cmd string, f func(c *BotContext), help string) {
115131
b.commands[cmd] = f
116132
if help != "" {
117-
b.helps = append(b.helps, fmt.Sprintf("- /%s: %s", cmd, help))
133+
b.help.Add(cmd, help)
118134
}
119135
}
120136

command_handlers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ func changelog(gistUrl string) func(c *bot.BotContext) {
266266
}
267267
}
268268

269-
func help(helps []string) func(c *bot.BotContext) {
269+
func helpCmd(help *bot.Help) func(c *bot.BotContext) {
270270
helpMessage := "⛑ *Справка по командам* \n%s \n📃 исходник: https://github.com/Gasoid/regular-go-bot"
271271
return func(c *bot.BotContext) {
272-
text := strings.Join(helps, "\n")
272+
text := strings.Join(help.List(), "\n")
273273
c.Text(helpMessage, text)
274274
}
275275
}

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ const (
1616
)
1717

1818
func main() {
19-
helps := []string{}
19+
help := bot.NewHelp()
2020
newMembersID := map[int64]int{}
2121
t, err := telegram.New(os.Getenv(token))
2222
if err != nil {
2323
log.Println("token is invalid", err.Error())
2424
return
2525
}
2626
httpEndpoint()
27-
bot := bot.New(t, helps, newMembersID)
28-
bot.Command("help", help(helps), "помощь по командам")
27+
bot := bot.New(t, help, newMembersID)
28+
bot.Command("help", helpCmd(help), "помощь по командам")
2929
bot.Command("estimation", estimation, "")
3030
bot.Command("changelog", changelog(os.Getenv(gistNewsURL)), "")
3131
bot.Command("currency", currency, "курс валют")

0 commit comments

Comments
 (0)