-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvent.gs
68 lines (60 loc) · 2.2 KB
/
Event.gs
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
function handleEvent(request) {
const REACTION_ADDED = "reaction_added";
const REACTION_REMOVED = "reaction_removed";
const EMOJI_CHANGED = "emoji_changed";
let contents = JSON.parse(request.postData.contents);
let event = contents.event;
let eventType = event.type;
switch (eventType) {
case REACTION_ADDED:
reactionAdded(event);
break;
case REACTION_REMOVED:
reactionRemoved(event);
break;
case EMOJI_CHANGED:
emojiChangeEvent(event);
break;
}
}
function reactionRemoved(event) {
textFinder = sheet.createTextFinder(event.reaction);
emojiCell = textFinder.findNext();
nextCell = sheet.getRange(emojiCell.getRow(), emojiCell.getColumn() + 1);
nextCell.setValue(nextCell.getValue() - 1);
}
function reactionAdded(event) {
textFinder = sheet.createTextFinder(event.reaction);
emojiCell = textFinder.findNext();
if (emojiCell == null) {
sheet.appendRow([event.reaction, 1]);
} else {
nextCell = sheet.getRange(emojiCell.getRow(), emojiCell.getColumn() + 1);
nextCell.setValue(nextCell.getValue() + 1);
}
}
function emojiChangeEvent(event) {
const ss = SpreadsheetApp.openByUrl(PropertiesService.getScriptProperties().getProperty('SPREADSHEET_URL'));
const emojisSheet = ss.getSheetByName('Sheet1');
const threadsSheet = ss.getSheetByName('threads');
let subtype = event.subtype;
let textFinder;
switch (subtype) {
case "add":
emojisSheet.appendRow([event.name, 0]);
postMessage(`A new emoji appeared!`, threadsSheet.getRange(threadChannelCell).getValue(), threadsSheet.getRange(currentThreadCell).getValue());
postMessage(`:${event.name}:`, threadsSheet.getRange(threadChannelCell).getValue(), threadsSheet.getRange(currentThreadCell).getValue());
break;
case "remove":
for (let i in event.names) {
textFinder = emojisSheet.createTextFinder(event.names[i]);
emojisSheet.deleteRow(textFinder.findNext().getRow());
}
break;
case "rename":
textFinder = emojisSheet.createTextFinder(event.old_name);
let cell = textFinder.findNext();
cell.setValue(event.new_name);
break;
}
}