-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwait_for_event.tmpl
62 lines (58 loc) · 1.65 KB
/
wait_for_event.tmpl
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
// Code generated by gommand. DO NOT EDIT.
package gommand
import (
"context"
"github.com/andersfylling/disgord"
"time"
)
// WaitManager is used to manage waiting within the context.
type WaitManager struct {
ctx *Context
}
// This allows you to wait for a event with specific conditions. You should NOT block during the check function.
func (w *WaitManager) waitForEvent(ctx context.Context, EventName string, CheckFunc func(s disgord.Session, evt interface{}) bool) interface{} {
x := make(chan interface{})
middleware := func(evt interface{}) interface{} {
if !CheckFunc(w.ctx.Session, evt) {
return nil
}
return evt
}
var timer *time.Timer
until, ok := ctx.Deadline()
if ok {
timer = time.AfterFunc(time.Until(until), func() {
x <- nil
})
}
handleEmit := func(e interface{}) {
x <- e
if timer != nil {
timer.Stop()
}
}
gateway := w.ctx.Session.Gateway().WithMiddleware(middleware).WithCtrl(&disgord.Ctrl{Runs: 1, Until: until})
switch EventName {
{{ range . }}case "{{ . }}":
gateway.{{ . }}(func(_ disgord.Session, e *disgord.{{.}}) { handleEmit(e) })
{{ end }}default:
panic("unknown event")
}
return <-x
}
{{ range . }}
// WaitFor{{ . }} allows you to wait for the {{ . }} event. You should NOT block during the check function.
func (w *WaitManager) WaitFor{{ . }}(ctx context.Context, CheckFunc func(s disgord.Session, evt *disgord.{{ . }}) bool) *disgord.{{ . }} {
x := w.waitForEvent(ctx, "{{ . }}", func(s disgord.Session, evt interface{}) bool {
if e, ok := evt.(*disgord.{{ . }}); ok {
return CheckFunc(s, e)
}
return false
})
r, ok := x.(*disgord.{{ . }})
if !ok {
return nil
}
return r
}
{{ end }}