-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrespondutil.go
61 lines (55 loc) · 1.55 KB
/
respondutil.go
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
package main
import (
"time"
"github.com/andersfylling/disgord"
"github.com/meooow25/cfspy/bot"
)
func respondWithError(ctx *bot.Context, err error) {
ctx.SendTimed(30*time.Second, ctx.MakeErrorEmbed(err.Error()))
}
func prepareCallbacks(ctx *bot.Context) (
msgCallback func(*disgord.Message),
delCallback func(*disgord.MessageReactionAdd),
allowOp func(*disgord.MessageReactionAdd) bool,
) {
return func(*disgord.Message) {
// This will fail without manage messages permission, that's fine.
go bot.SuppressEmbeds(ctx.Session, ctx.Message)
},
func(*disgord.MessageReactionAdd) {
// This will fail without manage messages permission, that's fine.
go bot.UnsuppressEmbeds(ctx.Session, ctx.Message)
},
func(evt *disgord.MessageReactionAdd) bool {
// Allow only the author to control the widget.
return evt.UserID == ctx.Message.Author.ID
}
}
func respondWithOnePagePreview(
ctx *bot.Context,
page *bot.Page,
files ...disgord.CreateMessageFileParams,
) error {
getPage := func(int) *bot.Page { return page }
return respondWithMultiPagePreview(ctx, getPage, 1, files...)
}
func respondWithMultiPagePreview(
ctx *bot.Context,
getPage func(int) *bot.Page,
numPages int,
files ...disgord.CreateMessageFileParams,
) error {
msgCallback, delCallback, allowOp := prepareCallbacks(ctx)
return ctx.SendWidget(&bot.WidgetParams{
Pages: &bot.Pages{
Get: getPage,
Total: numPages,
First: numPages,
Files: files,
},
MsgCallback: msgCallback,
Lifetime: time.Minute,
DelCallback: delCallback,
AllowOp: allowOp,
})
}