This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
95 lines (77 loc) · 1.97 KB
/
command.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"reflect"
"runtime/debug"
"strings"
"unicode"
"github.com/getsentry/sentry-go"
)
var handlers = map[string]Executor{
"time": timeCommand,
"download": downloadCommand,
"dl": downloadCommand,
"roll": RollCommand,
"omikuji": OmikujiCommand,
"おみくじ": OmikujiCommand,
"おみくじ🎰": OmikujiCommand,
}
type Executor func(sender CommandSender, args []string)
func isBlank(str string) bool {
for _, char := range str {
if !unicode.IsSpace(char) {
return false
}
}
return true
}
func parseCommand(c string) (string, []string) {
if isBlank(c) {
return "", []string{}
}
split := strings.Fields(c)
return strings.ToLower(split[0]), split[1:]
}
// Dispatch executes command that passed by webhook listener,
// Blocking will occurs if tweet need to be looked up.
// and then execute command in blocking.
func Dispatch(s CommandSender, c string) {
c = strings.TrimSpace(c)
label, args := parseCommand(c)
// TODO: DM Handle Practice
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("panic recovered: %v\n%s", r, string(debug.Stack()))
fmt.Printf("Panicking on executing command: %+v\n", err)
sentry.CaptureException(err)
}
}()
var command Executor
if label == "" {
if tl, ok := s.(TimelineSender); ok {
replyID := tl.Tweet.InReplyToStatusID
if replyID != 0 {
tweet := queueProcessor.LookupTweetBlocking(replyID)
if _, err := GetVideoVariant(&tweet); err == nil { // If target tweet has downloadable media
tl.ReplyCache = &tweet
command = downloadCommand
} else {
command = timeCommand
}
} else {
command = timeCommand
}
} else {
fmt.Println("non timeline sender sent empty command: ", reflect.TypeOf(s))
}
} else {
command = handlers[label]
}
if command == nil { // If unknown command has issued
if s, ok := s.(DirectMessageSender); ok { // and If sender is from direct message.
handleQuickTime(s)
}
return
}
command(s, args)
}