-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtrellobot.js
359 lines (324 loc) · 22.3 KB
/
trellobot.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
const Discord = require('discord.js')
const bot = new Discord.Client()
const fs = require('fs')
const auth = JSON.parse(fs.readFileSync('.auth'))
const conf = JSON.parse(fs.readFileSync('conf.json'))
let latestActivityID = fs.existsSync('.latestActivityID') ? fs.readFileSync('.latestActivityID') : 0
const Trello = require('trello-events')
const events = new Trello({
pollFrequency: conf.pollInterval, // milliseconds
minId: latestActivityID, // auto-created and auto-updated
start: false,
trello: {
boards: conf.boardIDs, // array of Trello board IDs
key: auth.trelloKey, // your public Trello API key
token: auth.trelloToken // your private Trello token for Trellobot
}
})
/*
** =====================================
** Discord event handlers and functions.
** =====================================
*/
bot.login(auth.discordToken)
bot.on('ready', () => {
let guild = bot.guilds.get(conf.serverID)
let channel = bot.channels.get(conf.channelID)
if (!guild) {
console.log(`Server with ID "${conf.serverID}" not found! I can't function without a valid server and channel.\nPlease add the correct server ID to your conf file, or if the conf data is correct, ensure I have proper access.\nYou may need to add me to your server using this link:\n https://discordapp.com/api/oauth2/authorize?client_id=${bot.user.id}&permissions=0&scope=bot`)
process.exit()
} else if (!channel) {
console.log(`Channel with ID "${conf.channelID}" not found! I can't function without a valid channel.\nPlease add the correct channel ID to your conf file, or if the conf data is correct, ensure I have proper access.`)
process.exit()
} else if (!conf.boardIDs || conf.boardIDs.length < 1) {
console.log(`No board IDs provided! Please add at least one to your conf file. Check the readme if you need help finding a board ID.`)
}
conf.guild = guild
conf.channel = channel
/*
** Make contentString a map of event names to their paired strings
** like this: {"createCard": "someone created a card", ...}, so you
** can, for example, ping specific roles for specific events.
**
** Also add a new conf section for pairing lists within a board to
** contentStrings? That way you can ping one role for new Moderation
** cards, and another role for new Event cards, for example.
*/
if (!conf.contentString) conf.contentString = ""
if (!conf.enabledEvents) conf.enabledEvents = []
if (!conf.userIDs) conf.userIDs = {}
if (!conf.realNames) conf.realNames = true
// set default prefix is none provided in conf
if (!conf.prefix) {
conf.prefix = "."
fs.writeFileSync('conf.json', JSON.stringify(conf, null, 4), (err, data) => console.log(`Updated conf file with default prefix ('.')`))
}
// logInitializationData()
console.log(`== Bot logged in as @${bot.user.tag}. Ready for action! ==`)
events.start()
})
bot.on('message', (msg) => {
if (msg.channel.type !== "text") return
if (msg.content.startsWith(`${conf.prefix}ping`)) {
let now = Date.now()
msg.channel.send(`Ping!`).then(m => {
m.edit(`Pong! (took ${Date.now() - now}ms)`)
})
}
})
/*
** ====================================
** Trello event handlers and functions.
** ====================================
*/
// Fired when a card is created
events.on('createCard', (event, board) => {
if (!eventEnabled(`cardCreated`)) return
let embed = getEmbedBase(event)
.setTitle(`New card created under __${event.data.list.name}__!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card created under __${event.data.list.name}__ by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
})
// Fired when a card is updated (description, due date, position, associated list, name, and archive status)
events.on('updateCard', (event, board) => {
let embed = getEmbedBase(event)
if (event.data.old.hasOwnProperty("desc")) {
if (!eventEnabled(`cardDescriptionChanged`)) return
embed
.setTitle(`Card description changed!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card description changed (see below) by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
.addField(`New Description`, typeof event.data.card.desc === "string" && event.data.card.desc.trim().length > 0 ? (event.data.card.desc.length > 1024 ? `${event.data.card.desc.trim().slice(0, 1020)}...` : event.data.card.desc) : `*[No description]*`)
.addField(`Old Description`, typeof event.data.old.desc === "string" && event.data.old.desc.trim().length > 0 ? (event.data.old.desc.length > 1024 ? `${event.data.old.desc.trim().slice(0, 1020)}...` : event.data.old.desc) : `*[No description]*`)
send(addDiscordUserData(embed, event.memberCreator))
} else if (event.data.old.hasOwnProperty("due")) {
if (!eventEnabled(`cardDueDateChanged`)) return
embed
.setTitle(`Card due date changed!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card due date changed to __${event.data.card.due ? new Date(event.data.card.due).toUTCString() : `[No due date]`}__ from __${event.data.old.due ? new Date(event.data.old.due).toUTCString() : `[No due date]`}__ by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
} else if (event.data.old.hasOwnProperty("pos")) {
if (!eventEnabled(`cardPositionChanged`)) return
embed
.setTitle(`Card position changed!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card position in list __${event.data.list.name}__ changed by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
} else if (event.data.old.hasOwnProperty("idList")) {
if (!eventEnabled(`cardListChanged`)) return
embed
.setTitle(`Card list changed!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card moved to list __${event.data.listAfter.name}__ from list __${event.data.listBefore.name}__ by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
} else if (event.data.old.hasOwnProperty("name")) {
if (!eventEnabled(`cardNameChanged`)) return
embed
.setTitle(`Card name changed!`)
.setDescription(`**CARD:** *[See below for card name]* — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card name changed (see below) by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
.addField(`New Name`, event.data.card.name)
.addField(`Old Name`, event.data.old.name)
send(addDiscordUserData(embed, event.memberCreator))
} else if (event.data.old.hasOwnProperty("closed")) {
if (event.data.old.closed) {
if (!eventEnabled(`cardUnarchived`)) return
embed
.setTitle(`Card unarchived!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card unarchived and returned to list __${event.data.list.name}__ by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
} else {
if (!eventEnabled(`cardArchived`)) return
embed
.setTitle(`Card archived!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card under list __${event.data.list.name}__ archived by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
}
}
})
// Fired when a card is deleted
events.on('deleteCard', (event, board) => {
if (!eventEnabled(`cardDeleted`)) return
let embed = getEmbedBase(event)
.setTitle(`Card deleted!`)
.setDescription(`**EVENT:** Card deleted from list __${event.data.list.name}__ by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
})
// Fired when a comment is posted, or edited
events.on('commentCard', (event, board) => {
let embed = getEmbedBase(event)
if (event.data.hasOwnProperty("textData")) {
if (!eventEnabled(`commentEdited`)) return
embed
.setTitle(`Comment edited on card!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card comment edited (see below for comment text) by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
.addField(`Comment Text`, event.data.text.length > 1024 ? `${event.data.text.trim().slice(0, 1020)}...` : event.data.text)
.setTimestamp(event.data.dateLastEdited)
send(addDiscordUserData(embed, event.memberCreator))
} else {
if (!eventEnabled(`commentAdded`)) return
embed
.setTitle(`Comment added to card!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Card comment added (see below for comment text) by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
.addField(`Comment Text`, event.data.text.length > 1024 ? `${event.data.text.trim().slice(0, 1020)}...` : event.data.text)
send(addDiscordUserData(embed, event.memberCreator))
}
})
// Fired when a member is added to a card
events.on('addMemberToCard', (event, board) => {
let embed = getEmbedBase(event)
.setTitle(`Member added to card!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Member **[${conf.realNames ? event.member.fullName : event.member.username}](https://trello.com/${event.member.username})**`)
let editedEmbed = addDiscordUserData(embed, event.member)
if (event.member.id === event.memberCreator.id) {
if (!eventEnabled(`memberAddedToCardBySelf`)) return
editedEmbed.setDescription(editedEmbed.description + ` added themselves to card.`)
send(editedEmbed)
} else {
if (!eventEnabled(`memberAddedToCard`)) return
editedEmbed.setDescription(editedEmbed.description + ` added to card by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(editedEmbed, event.memberCreator))
}
})
// Fired when a member is removed from a card
events.on('removeMemberFromCard', (event, board) => {
let embed = getEmbedBase(event)
.setTitle(`Member removed from card!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Member **[${conf.realNames ? event.member.fullName : event.member.username}](https://trello.com/${event.member.username})**`)
let editedEmbed = addDiscordUserData(embed, event.member)
if (event.member.id === event.memberCreator.id) {
if (!eventEnabled(`memberRemovedFromCardBySelf`)) return
editedEmbed.setDescription(editedEmbed.description + ` removed themselves from card.`)
send(editedEmbed)
} else {
if (!eventEnabled(`memberRemovedFromCard`)) return
editedEmbed.setDescription(editedEmbed.description + ` removed from card by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(editedEmbed, event.memberCreator))
}
})
// Fired when a list is created
events.on('createList', (event, board) => {
if (!eventEnabled(`listCreated`)) return
let embed = getEmbedBase(event)
.setTitle(`New list created!`)
.setDescription(`**EVENT:** List __${event.data.list.name}__ created by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
})
// Fired when a list is renamed, moved, archived, or unarchived
events.on('updateList', (event, board) => {
let embed = getEmbedBase(event)
if (event.data.old.hasOwnProperty("name")) {
if (!eventEnabled(`listNameChanged`)) return
embed
.setTitle(`List name changed!`)
.setDescription(`**EVENT:** List renamed to __${event.data.list.name}__ from __${event.data.old.name}__ by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
} else if (event.data.old.hasOwnProperty("pos")) {
if (!eventEnabled(`listPositionChanged`)) return
embed
.setTitle(`List position changed!`)
.setDescription(`**EVENT:** List __${event.data.list.name}__ position changed by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
} else if (event.data.old.hasOwnProperty("closed")) {
if (event.data.old.closed) {
if (!eventEnabled(`listUnarchived`)) return
embed
.setTitle(`List unarchived!`)
.setDescription(`**EVENT:** List __${event.data.list.name}__ unarchived by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
} else {
if (!eventEnabled(`listArchived`)) return
embed
.setTitle(`List archived!`)
.setDescription(`**EVENT:** List __${event.data.list.name}__ archived by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
}
}
})
// Fired when an attachment is added to a card
events.on('addAttachmentToCard', (event, board) => {
if (!eventEnabled(`attachmentAddedToCard`)) return
let embed = getEmbedBase(event)
.setTitle(`Attachment added to card!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Attachment named \`${event.data.attachment.name}\` added to card by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
})
// Fired when an attachment is removed from a card
events.on('deleteAttachmentFromCard', (event, board) => {
if (!eventEnabled(`attachmentRemovedFromCard`)) return
let embed = getEmbedBase(event)
.setTitle(`Attachment removed from card!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Attachment named \`${event.data.attachment.name}\` removed from card by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
})
// Fired when a checklist is added to a card (same thing as created)
events.on('addChecklistToCard', (event, board) => {
if (!eventEnabled(`checklistAddedToCard`)) return
let embed = getEmbedBase(event)
.setTitle(`Checklist added to card!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Checklist named \`${event.data.checklist.name}\` added to card by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
})
// Fired when a checklist is removed from a card (same thing as deleted)
events.on('removeChecklistFromCard', (event, board) => {
if (!eventEnabled(`checklistRemovedFromCard`)) return
let embed = getEmbedBase(event)
.setTitle(`Checklist removed from card!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Checklist named \`${event.data.checklist.name}\` removed from card by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
send(addDiscordUserData(embed, event.memberCreator))
})
// Fired when a checklist item's completion status is toggled
events.on('updateCheckItemStateOnCard', (event, board) => {
if (event.data.checkItem.state === "complete") {
if (!eventEnabled(`checklistItemMarkedComplete`)) return
let embed = getEmbedBase(event)
.setTitle(`Checklist item marked complete!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Checklist item under checklist \`${event.data.checklist.name}\` marked complete by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
.addField(`Checklist Item Name`, event.data.checkItem.name.length > 1024 ? `${event.data.checkItem.name.trim().slice(0, 1020)}...` : event.data.checkItem.name)
send(addDiscordUserData(embed, event.memberCreator))
} else if (event.data.checkItem.state === "incomplete") {
if (!eventEnabled(`checklistItemMarkedIncomplete`)) return
let embed = getEmbedBase(event)
.setTitle(`Checklist item marked incomplete!`)
.setDescription(`**CARD:** ${event.data.card.name} — **[CARD LINK](https://trello.com/c/${event.data.card.shortLink})**\n\n**EVENT:** Checklist item under checklist \`${event.data.checklist.name}\` marked incomplete by **[${conf.realNames ? event.memberCreator.fullName : event.memberCreator.username}](https://trello.com/${event.memberCreator.username})**`)
.addField(`Checklist Item Name`, event.data.checkItem.name.length > 1024 ? `${event.data.checkItem.name.trim().slice(0, 1020)}...` : event.data.checkItem.name)
send(addDiscordUserData(embed, event.memberCreator))
}
})
/*
** =======================
** Miscellaneous functions
** =======================
*/
events.on('maxId', (id) => {
if (latestActivityID == id) return
latestActivityID = id
fs.writeFileSync('.latestActivityID', id)
})
const send = (embed, content = ``) => conf.channel.send(`${content} ${conf.contentString}`, {embed:embed}).catch(err => console.error(err))
const eventEnabled = (type) => conf.enabledEvents.length > 0 ? conf.enabledEvents.includes(type) : true
const logEventFire = (event) => console.log(`${new Date(event.date).toUTCString()} - ${event.type} fired`)
const getEmbedBase = (event) => new Discord.RichEmbed()
.setFooter(`${conf.guild.members.get(bot.user.id).displayName} • ${event.data.board.name} [${event.data.board.shortLink}]`, bot.user.displayAvatarURL)
.setTimestamp(event.hasOwnProperty(`date`) ? event.date : Date.now())
.setColor("#127ABD")
// Converts Trello @username mentions in titles to Discord mentions, finds channel and role mentions, and mirros Discord user mentions outside the embed
const convertMentions = (embed, event) => {
}
// adds thumbanil and appends user mention to the end of the description, if possible
const addDiscordUserData = (embed, member) => {
if (conf.userIDs[member.username]) {
let discordUser = conf.guild.members.get(conf.userIDs[member.username])
if (discordUser) embed
.setThumbnail(discordUser.user.displayAvatarURL)
.setDescription(`${embed.description} / ${discordUser.toString()}`)
}
return embed
}
// logs initialization data (stuff loaded from conf.json) - mostly for debugging purposes
const logInitializationData = () => console.log(`== INITIALIZING WITH:
latestActivityID - ${latestActivityID}
boardIDs --------- ${conf.boardIDs.length + " [" + conf.boardIDs.join(", ") + "]"}
serverID --------- ${conf.serverID} (${conf.guild.name})
channelID -------- ${conf.channelID} (#${conf.channel.name})
pollInterval ----- ${conf.pollInterval} ms (${conf.pollInterval / 1000} seconds)
prefix ----------- "${conf.prefix}"${conf.prefix === "." ? " (default)" : ""}
contentString ---- ${conf.contentString !== "" ? "\"" + conf.contentString + "\"" : "none"}
enabledEvents ---- ${conf.enabledEvents.length > 0 ? conf.enabledEvents.length + " [" + conf.enabledEvents.join(", ") + "]" : "all"}
userIDs ---------- ${Object.getOwnPropertyNames(conf.userIDs).length}`)