-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
175 lines (152 loc) · 6.06 KB
/
index.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
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
const Discord = require('discord.js');
const express = require('express');
const request = require('request');
const body_parser = require('body-parser');
const fs = require('fs');
const client = new Discord.Client();
const app = express();
// Parses POST requests for JSON
app.use(body_parser.json());
app.use(body_parser.urlencoded({ extended: true }));
// Exposed port for Express
const port = 3000;
// Loads client information from client_info.json (See README.md)
let client_info = JSON.parse(fs.readFileSync('client_info.json'));
let board_id;
let hook_id;
// Obtain board ID from Trello, given the URL of the board
request(`${client_info.board_url}.json?key=${client_info.trello_key}&token=${client_info.trello_token}`, (error, response, body) => {
console.log(response && response.statusCode); // Should be 200
board_id = JSON.parse(String(body)).id;
console.log(board_id);
});
// Once Discord client is ready
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
// Create webhook once Discord client is ready, by sending a POST request to the Trello API
request.post(`https://api.trello.com/1/tokens/${client_info.trello_token}/webhooks/?key=${client_info.trello_key}`, {
json: {
description: "TrelloBot Webhook",
callbackURL: client_info.callback_url,
idModel: board_id
}
}, (error, res, body) => {
if (error) {
console.error(error);
return;
}
hook_id = body.id;
});
});
// Delete webhook if Admin sends command message
client.on('message', (msg) => {
if (msg.content === 't!del' && msg.member.hasPermission("ADMINISTRATOR")) {
console.log('sending delete request')
request.delete(`https://api.trello.com/1/webhooks/${hook_id}?key=${client_info.trello_key}`, (res) => console.log('Webhook deleted'));
}
});
// Respond to HEAD requests with a status (because Trello API requires it for security)
app.get('/', (req, res) => res.send('TrelloBot is alive!'));
// Recieve update from Trello webhook
app.post('/', (req, res) => {
console.log('recieved webhook action');
let body;
let value = '-----';
// Logs the type of action to the console
console.log(req.body.action.display.translationKey);
display_values = req.body.action.display;
// The current list of common action types listed on the Trello API documentation
switch (String(req.body.action.type)) {
case 'addMemberToBoard':
body = 'Added Member to Board';
break;
case 'addMemberToCard':
body = 'Added Member to Card';
break;
case 'addMemberToOrganization':
body = 'Added Member to Organization';
break;
case 'commentCard':
body = 'Added Comment';
value = `*"${display_values.entities.comment.text}"* on **${display_values.entities.card.text}**`;
break;
case 'createCard':
body = 'Created a Card';
value = `**${display_values.entities.card.text}** in **__${display_values.entities.list.text}__**`;
break;
case 'createChecklist':
body = 'Created a Checklist';
break;
case 'createList':
body = 'Created a List';
value = `Created **__${display_values.entities.list.text}__**`;
break;
case 'deleteCard':
body = 'Deleted a Card';
break;
case 'createLabel':
body = 'Added a Label';
break;
case 'deleteLabel':
body = 'Deleted a Label';
break;
case 'memberJoinedTrello':
body = 'Joined Board';
break;
case 'moveCardFromBoard':
body = 'Moved Card From Board';
break;
case 'moveCardToBoard':
body = 'Moved Card To Board';
break;
case 'updateBoard':
body = 'Updated Board';
break;
case 'updateCard':
body = 'Updated a Card';
if (display_values.translationKey === 'action_move_card_from_list_to_list') {
value = `Moved **${display_values.entities.card.text}** from **__${display_values.entities.listBefore.text}__** to **__${display_values.entities.listAfter.text}__**`;
} else if (display_values.translationKey === 'action_renamed_card') {
value = `Renamed **${display_values.entities.name.text}** to **${display_values.entities.card.text}**`;
} else if (display_values.translationKey === 'action_archived_card') {
value = `Archived **${display_values.entities.card.text}**`;
} else {
value = `Updated **${display_values.entities.card.text}**`;
}
break;
case 'updateChecklist':
body = 'Updated a Checklist';
break;
case 'updateLabel':
body = 'Updated a Label';
break;
case 'commentCard':
body = 'Added a Comment';
break;
case 'updateList':
body = 'Updated a List';
break;
case 'updateMember':
body = 'Updated a Member';
break;
case 'updateOrganization':
body = 'Updated Organization';
break;
default:
body = 'Made a Misc. Change to Board';
console.log(req.body.action.type);
break;
}
// Creates embed based on the Discord.RichEmbed class
let update_embed = new Discord.RichEmbed()
.setColor('#0079bf')
.setAuthor(req.body.action.memberCreator.fullName, '', req.body.model.url)
.addField(body, value)
.setTimestamp(req.body.action.date);
client.channels.get(client_info.channel_id).send(update_embed);
// Respond to Trello API when it dispatches a webhook action, so that it doesn't think the connection failed
res.send("Action Recieved!");
});
// Starts Express and Discord client
app.listen(port, () => console.log(`Listening on port ${port}!`));
client.login(client_info.discord_token);