-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
123 lines (114 loc) · 3.56 KB
/
example.js
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
/*jslint node: true, esnext: true */
"use strict";
var config = require("./config"); // gitignored
var Slapp = require("./index");
var slapp = new Slapp(config.token);
var SmileyGame = slapp.register({
state: {
x: 0,
y: 0,
height: 5,
width: 5,
icon: ":simple_smile:",
updated_by: null
},
text: function(state) {
// automatically re-rendered after each event
var strs = [];
strs.push("|");
for (let i = 0; i < state.y; i++) {
strs.push("\n|");
}
for (let i = 0; i < state.x; i++) {
strs.push(" ");
}
strs.push(state.icon);
for (let i = 0; i < state.height - state.y - 1; i++) {
strs.push("\n|");
}
if (state.updated_by) {
strs.push("\nLast updated by: " + state.updated_by);
}
return strs.join("");
},
buttons: function() { //state
return ["arrow_up", "arrow_down", "arrow_left", "arrow_right", "x"];
},
myCustomSetX: function(x) {
this.state.x = Math.min(Math.max(x, 0), this.state.width - 1);
},
myCustomSetY: function(y) {
this.state.y = Math.min(Math.max(y, 0), this.state.height - 1);
},
click: function() { //e
// if no handler for the given emoji is in handlers, this is called instead
// basically a "fallback" or default
this.destroy(); // this destroys the message if we want
},
handlers: {
// called instead of click if defined
// text() is automatically called after this to update the message
arrow_up: function(e, state) {
this.myCustomSetY(state.y - 1);
// if you return false it won't update
// can also return a promise and it'll update once the promise resolves
// can also manually call this.update() if you want to do something like...
// state.loading = true;
// this.update();
// return someLongCall.then(function() {
// state.loading = false;
// })
},
arrow_down: function(e, state) {
this.myCustomSetY(state.y + 1);
},
arrow_left: function(e, state) {
this.myCustomSetX(state.x - 1);
},
arrow_right: function(e, state) {
var that = this;
return e.userInfo.then(function(info) {
that.myCustomSetX(state.x + 1); // :(
state.updated_by = info.profile.first_name;
});
}
}
});
// create a smiley game
SmileyGame.create({channel: "#testing-slapp"}).done();
// create a smiley game, overriding state
// SmileyGame.create({channel: "#testing-slapp"}, {icon: ":poop:"})
// .then(function(game) {
// storeInADatabase(game.id);
// });
// re-attach to a previously-created post if your bot restarts
// SmileyGame.attach({id: getIdFromDatabase()});
exports.Checklist = slapp.register({ // jshint ignore:line
state: {
items: ["Make audio game", "Build Asana integration", "PagerDuty!"],
done: [false, false, false],
numberIcons: ["one", "two", "three", "four", "five", "six"],
doneIcon: "ballot_box_with_check",
incompleteIcon: "white_medium_square"
},
text: function(state) {
var ar = [];
for (var i = 0; i < state.items.length; i++) {
ar.push(":" + state.numberIcons[i] + ":");
ar.push(" :");
ar.push(state.done[i] ? state.doneIcon : state.incompleteIcon);
ar.push(":\t");
ar.push(state.items[i]);
ar.push("\n");
}
return ar.join("");
},
buttons: function(state) {
return state.numberIcons.slice(0, state.items.length);
},
click: function(e, state) {
// use the default click handler instead of individually registering
var index = state.numberIcons.indexOf(e.emoji);
state.done[index] = !state.done[index];
}
});