-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_context.go
More file actions
82 lines (71 loc) · 2.62 KB
/
Copy pathcommand_context.go
File metadata and controls
82 lines (71 loc) · 2.62 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package vkc
import (
"fmt"
"github.com/SevereCloud/vksdk/v3/api"
"github.com/SevereCloud/vksdk/v3/api/params"
"github.com/SevereCloud/vksdk/v3/events"
"github.com/SevereCloud/vksdk/v3/object"
)
// Контекст команды. Передается в каждый обработчик.
type CommandContext[DEPS any] struct {
VK *api.VK
Message object.MessagesMessage
Arguments []string
RawEvent events.MessageNewObject
Dependency DEPS
}
// Параметры отправки сообщения. Используется в методах Send и SendMessageRaw.
type SendTextParams struct {
Reply bool
Fmt []any
}
// Сокращение для SendTextParams со включенным ответом на сообщение.
var WithReplyParams = &SendTextParams{
Reply: true,
}
// Сокращение для SendTextParams с подстановкой.
func WithFmtParams(subst ...any) *SendTextParams {
return &SendTextParams{
Fmt: subst,
}
}
// Сокращение для SendTextParams с подстановкой и включенным ответом на сообщение.
func WithFmtAndReplyParams(subst ...any) (ret *SendTextParams) {
ret = WithFmtParams(subst...)
ret.Reply = true
return
}
// Базовый метод отправки сообщения.
// Возвращает ошибки в случаях: ...
func SendMessageRaw(vk *api.VK, msg *object.MessagesMessage, peerID int, text string, sendParams *SendTextParams) error {
if sendParams == nil {
sendParams = &SendTextParams{}
}
b := params.NewMessagesSendBuilder()
if sendParams.Reply && msg != nil {
b.ReplyTo(msg.ID)
}
finalText := text
if len(sendParams.Fmt) > 0 {
finalText = fmt.Sprintf(finalText, sendParams.Fmt...)
}
b.Message(finalText)
b.RandomID(0)
b.PeerID(peerID)
if _, err := vk.MessagesSend(b.Params); err != nil {
return fmt.Errorf("send error: %v", err)
}
return nil
}
// Отправка сообщения с параметрами.
func (ctx CommandContext[DEPS]) Send(text string, sendParams *SendTextParams) error {
return SendMessageRaw(ctx.VK, &ctx.Message, ctx.Message.PeerID, text, sendParams)
}
// Отправка сообщения с форматированием.
func (ctx CommandContext[DEPS]) SendText(text string, fmts ...any) error {
return SendMessageRaw(ctx.VK, &ctx.Message, ctx.Message.PeerID, text, WithFmtParams(fmts...))
}
// Отправка ответа на команду с форматированием.
func (ctx CommandContext[DEPS]) Reply(text string, fmts ...any) error {
return SendMessageRaw(ctx.VK, &ctx.Message, ctx.Message.PeerID, text, WithFmtAndReplyParams(fmts...))
}