-
Notifications
You must be signed in to change notification settings - Fork 15
/
jokes.go
233 lines (199 loc) · 6.02 KB
/
jokes.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package jarvisbot
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"github.com/boltdb/bolt"
"github.com/kardianos/osext"
"github.com/tucnak/telebot"
)
// jokes.go contain joke functions. Not part of jarvisbot's default funcmap
// SendLaugh returns Jon's laugh. Thx, @jhowtan
func (j *JarvisBot) SendLaugh(msg *message) {
j.bot.SendChatAction(msg.Chat, telebot.UploadingAudio)
fn, err := j.sendFileWrapper("data/laugh.ogg", "ogg")
if err != nil {
j.log.Printf("error sending file: %s", err)
return
}
fn(msg)
}
// NeverForget returns the Barisan Socialis flag. Thx, @shawntan
func (j *JarvisBot) NeverForget(msg *message) {
j.bot.SendChatAction(msg.Chat, telebot.UploadingPhoto)
fn, err := j.sendFileWrapper("data/barisan.jpg", "photo")
if err != nil {
j.log.Printf("error sending file: %s", err)
return
}
fn(msg)
}
// SendLogic returns Tuan's logical meme, based on when Ian called him
// illogical.
func (j *JarvisBot) SendLogic(msg *message) {
j.bot.SendChatAction(msg.Chat, telebot.UploadingPhoto)
fn, err := j.sendFileWrapper("data/logic.jpg", "photo")
if err != nil {
j.log.Printf("error sending file: %s", err)
return
}
fn(msg)
}
// SendKid sends Tin's kid meme.
func (j *JarvisBot) SendKid(msg *message) {
j.bot.SendChatAction(msg.Chat, telebot.UploadingPhoto)
fn, err := j.sendFileWrapper("data/thiskid.jpg", "photo")
if err != nil {
j.log.Printf("error sending file: %s", err)
return
}
fn(msg)
}
// Yank returns a gif of @jellykaya being yanked off-stage by @vishnup
func (j *JarvisBot) Yank(msg *message) {
j.bot.SendChatAction(msg.Chat, telebot.UploadingPhoto)
fn, err := j.sendFileWrapper("data/yank.gif", "gif")
if err != nil {
j.log.Printf("error sending file: %s", err)
return
}
fn(msg)
}
// Hanar returns a picture of a Hanar. Thx, @shawntan
// Context: 'hanar, hanar, hanar' means 'yeah I get it, stop nagging'
// in Singaporean Hokkien. The picture sent is a picture of a creature in
// Mass Effect called a Hanar.
func (j *JarvisBot) Hanar(msg *message) {
j.bot.SendChatAction(msg.Chat, telebot.UploadingPhoto)
fn, err := j.sendFileWrapper("data/hanar.jpg", "photo")
if err != nil {
j.log.Printf("error sending file: %s", err)
return
}
fn(msg)
}
// TellThatTo returns a picture of Kanjiklub
func (j *JarvisBot) TellThatTo(msg *message) {
j.bot.SendChatAction(msg.Chat, telebot.UploadingPhoto)
fn, err := j.sendFileWrapper("data/kanjiklub.jpg", "photo")
if err != nil {
j.log.Printf("error sending file: %s", err)
return
}
fn(msg)
}
// Touch allows Jarvis to be touched. Thx, @rahulg
func (j *JarvisBot) Touch(msg *message) {
messages := []string{
"Why, thank you!",
"Stop touching me!",
"Ooh, that feels *so* good.",
"\U0001f60a",
"AAAAAHHHHHHHH!!!!!\n\nOh, frightened me for a moment, there.",
"Ouch! Watch it!",
"Noice!\nhttps://www.youtube.com/watch?v=rQnYi3z56RE",
}
n := rand.Intn(len(messages))
j.SendMessage(msg.Chat, messages[n], nil)
}
// sendFileWrapper checks if the file exists and writes it before returning the response function.
func (j *JarvisBot) sendFileWrapper(assetName string, filetype string) (ResponseFunc, error) {
fileId, err := j.getCachedFileID(assetName)
if err != nil {
j.log.Printf("error retreiving cached file_id for %s", assetName)
}
if fileId != "" {
file := telebot.File{FileID: fileId}
return func(msg *message) {
if filetype == "ogg" {
audio := telebot.Audio{File: file, Mime: "audio/ogg"}
j.bot.SendAudio(msg.Chat, &audio, nil)
} else if filetype == "photo" {
photo := telebot.Photo{File: file}
j.bot.SendPhoto(msg.Chat, &photo, nil)
} else if filetype == "gif" {
doc := telebot.Document{File: file, Mime: "image/gif"}
j.bot.SendDocument(msg.Chat, &doc, nil)
}
}, nil
} else {
pwd, err := osext.ExecutableFolder()
if err != nil {
j.log.Printf("error retrieving pwd: %s", err)
}
_, filename := path.Split(assetName)
filePath := path.Join(pwd, tempDir, filename)
// Check if file exists, if it doesn't exist, create it
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fileData, err := Asset(assetName)
if err != nil {
err = fmt.Errorf("error retrieving asset %s: %s", assetName, err)
return nil, err
}
err = ioutil.WriteFile(filePath, fileData, 0775)
if err != nil {
err = fmt.Errorf("error creating %s: %s", assetName, err)
return nil, err
}
}
return func(msg *message) {
file, err := telebot.NewFile(filePath)
if err != nil {
j.log.Printf("error reading %s: %s", filePath, err)
return
}
var newFileId string
if filetype == "ogg" {
audio := telebot.Audio{File: file, Mime: "audio/ogg"}
j.bot.SendAudio(msg.Chat, &audio, nil)
newFileId = audio.FileID
} else if filetype == "photo" {
photo := telebot.Photo{File: file, Thumbnail: telebot.Thumbnail{File: file}}
j.bot.SendPhoto(msg.Chat, &photo, nil)
newFileId = photo.FileID
} else if filetype == "gif" {
doc := telebot.Document{File: file, Preview: telebot.Thumbnail{File: file}, Mime: "image/gif"}
j.bot.SendDocument(msg.Chat, &doc, nil)
newFileId = doc.FileID
}
err = j.cacheFileID(assetName, newFileId)
if err != nil {
j.log.Println("error caching file_id '%s' for '%s': %s", fileId, assetName, err)
}
}, nil
}
}
func (j *JarvisBot) cacheFileID(assetName string, fileId string) error {
err := j.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(file_cache_bucket_name)
err := b.Put([]byte(assetName), []byte(fileId))
if err != nil {
return err
}
return nil
})
return err
}
func (j *JarvisBot) getCachedFileID(assetName string) (string, error) {
var fileId string
err := j.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(file_cache_bucket_name)
v := b.Get([]byte(assetName))
fileId = string(v[:])
return nil
})
if err != nil {
return "", err
}
return fileId, nil
}
// Returns pictures of keyword.
// Used for joke functions, e.g. /ducks, because I like to say that
func (j *JarvisBot) SendImage(keyword string) ResponseFunc {
return func(msg *message) {
msg.Args = []string{keyword}
j.ImageSearch(msg)
}
}