forked from tcolgate/hugot
/
tableflip.go
95 lines (78 loc) · 2.27 KB
/
tableflip.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
// Copyright (c) 2016 Tristan Colgate-McFarlane
//
// This file is part of hugot.
//
// hugot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// hugot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with hugot. If not, see <http://www.gnu.org/licenses/>.
// Package tableflip provides an exacmple Hears handler that will
// flip tables on behalf of embittered users.
package tableflip
import (
"fmt"
"regexp"
"time"
"golang.org/x/net/context"
"github.com/tcolgate/hugot"
)
// y=ー( ゚д゚)・∵.
// From Will P
func init() {
hugot.AddHearsHandler(New())
}
type tableflip struct {
}
func New() hugot.HearsHandler {
return &tableflip{}
}
func (*tableflip) Describe() (string, string) {
return "tableflip", "stress reliever, just say the word, watch the tables go flying"
}
var tableflipRegexp = regexp.MustCompile(`(^| *)tableflip($| *)`)
func (*tableflip) Hears() *regexp.Regexp {
return tableflipRegexp
}
// We'll be horrid and use some globals
var flipState bool
var lastFlip time.Time
func (*tableflip) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
flip := `(╯°□°)╯︵ ┻━┻`
unFlip := `┬━┬ ノ( ゜-゜ノ)`
doubleFlip := "┻━┻ ︵¯\\(ツ)/¯ ︵ ┻━┻"
tripleFlip := "(╯°□°)╯¸.·´¯`·.¸¸.·´¯`·.¸¸.·´¯ ┻━┻"
flipOff := "ಠ︵ಠ凸"
if !flipState {
flipState = true
go func() {
five, _ := time.ParseDuration("30s")
time.Sleep(five)
if flipState == true {
flipState = false
fmt.Fprint(w, unFlip)
}
}()
switch fs := tableflipRegexp.FindAllString(m.Text, 5); len(fs) {
case 1:
fmt.Fprint(w, flip)
case 2:
fmt.Fprint(w, doubleFlip)
case 3:
fmt.Fprint(w, tripleFlip)
default:
fmt.Fprint(w, flipOff)
flipState = false
}
return
}
flipState = false
fmt.Fprint(w, unFlip)
}