File tree 1 file changed +54
-0
lines changed
1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ const retrievedQuotes = context . getValue ( "quotes" )
2
+
3
+ let quotes
4
+ if ( retrievedQuotes ) {
5
+ quotes = JSON . parse ( retrievedQuotes )
6
+ } else {
7
+ quotes = [ ]
8
+ }
9
+
10
+ function addQuote ( event ) {
11
+ if ( event . command === "addquote" ) {
12
+ quotes . push ( event . body )
13
+ context . setValue ( "quotes" , JSON . stringify ( quotes ) )
14
+ context . respond ( `Quote #${ quotes . length } added.` )
15
+ }
16
+ }
17
+
18
+ function getQuote ( event ) {
19
+ if ( event . command === "quote" ) {
20
+
21
+ if ( quotes . length === 0 ) {
22
+ event . respond ( "I don't know any quotes." )
23
+ return
24
+ }
25
+
26
+ const sayQuote = ( number ) => {
27
+ const quote = quotes [ number - 1 ]
28
+ event . say ( `Quote ${ number } of ${ quotes . length } : ${ quote } ` )
29
+ }
30
+
31
+ if ( event . body ) {
32
+ const number = parseInt ( event . body )
33
+ if ( ! number ) {
34
+ event . say ( "What number is that?" )
35
+ return
36
+ }
37
+ if ( number < 1 || number > quotes . length ) {
38
+ event . say ( "I don't know that quote." )
39
+ return
40
+ }
41
+ sayQuote ( number )
42
+ } else {
43
+ const number = Math . floor ( Math . random ( ) * quotes . length ) + 1
44
+ sayQuote ( number )
45
+ }
46
+ }
47
+ }
48
+
49
+ context . addHelp ( "addquote" , "Store a quote in the quote database" )
50
+ context . addHelp ( "quote" , "Get a random quote from the quote database" )
51
+
52
+ context . addEventHandler ( addQuote )
53
+ context . addEventHandler ( getQuote )
54
+
You can’t perform that action at this time.
0 commit comments