-
Notifications
You must be signed in to change notification settings - Fork 0
/
botbooter.go
158 lines (135 loc) · 3.4 KB
/
botbooter.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package botbooter
import (
"errors"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
)
type BotType int
const (
SlackBotType BotType = iota
DiscordBotType
)
type Bot struct {
BotType BotType
DiscordSession *discordgo.Session
SlackClient *slack.Client
SlackSocketClient *socketmode.Client
Commands []Command
UnknownCommandHandler UnknownCommandHandler
Middlewares []Middleware
}
type Message struct {
UserID string
ChannelID string
Content string
DiscordData *discordgo.MessageCreate
SlackData *slackevents.MessageEvent
}
type CommandHandler func(bot *Bot, message *Message)
type Command struct {
Pattern string
Handler CommandHandler
}
type UnknownCommandHandler func(bot *Bot, message *Message)
type Middleware func(bot *Bot, message *Message, next CommandHandler)
type Attachment struct {
IsImage bool
URL string
ExtraData interface{}
}
func (b *Bot) Connect() error {
switch b.BotType {
case SlackBotType:
return b.connectSlack()
case DiscordBotType:
return b.connectDiscord()
default:
return fmt.Errorf("Unknown bot type")
}
}
func (b *Bot) Disconnect() error {
switch b.BotType {
case SlackBotType:
return b.disconnectSlack()
case DiscordBotType:
return b.disconnectDiscord()
default:
return fmt.Errorf("Unknown bot type")
}
}
func (b *Bot) GetAttachments(message *Message) ([]Attachment, error) {
switch b.BotType {
case SlackBotType:
return getAttachmentsFromSlackMessage(message.SlackData), nil
case DiscordBotType:
return getAttachmentsFromDiscordMessage(message.DiscordData.Message), nil
default:
return nil, errors.New("Unknown bot type")
}
}
func (b *Bot) SendMessage(channelID string, message string) error {
switch b.BotType {
case SlackBotType:
_, _, err := b.SlackClient.PostMessage(
channelID,
slack.MsgOptionText(message, false),
)
return err
case DiscordBotType:
_, err := b.DiscordSession.ChannelMessageSend(channelID, message)
return err
default:
return fmt.Errorf("Unknown bot type")
}
}
func (b *Bot) AddHandler(handler Command) {
fmt.Println("Adding handler:", handler)
b.Commands = append(b.Commands, handler)
}
func (b *Bot) SetUnknownCommandHandler(handler UnknownCommandHandler) {
b.UnknownCommandHandler = handler
}
func (b *Bot) StartListening() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
<-sigCh
log.Println("Bot is shutting down...")
err := b.Disconnect()
if err != nil {
log.Println("Failed to disconnect:", err)
}
}
func (b *Bot) AddMiddleware(middleware Middleware) {
b.Middlewares = append(b.Middlewares, middleware)
}
func (b *Bot) handleMessageWithCommand(message *Message) {
handler := func(bot *Bot, message *Message) {
for _, command := range bot.Commands {
matched, err := regexp.MatchString(command.Pattern, message.Content)
if err == nil && matched {
command.Handler(bot, message)
return
}
}
if bot.UnknownCommandHandler != nil {
bot.UnknownCommandHandler(bot, message)
}
}
finalHandler := handler
for i := len(b.Middlewares) - 1; i >= 0; i-- {
middleware := b.Middlewares[i]
next := finalHandler
finalHandler = func(bot *Bot, message *Message) {
middleware(bot, message, next)
}
}
finalHandler(b, message)
}