forked from Spirast/Trello-Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (57 loc) · 2.74 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
require('dotenv').config();
const { Client, IntentsBitField, EmbedBuilder } = require('discord.js');
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
]
});
client.on('ready', function (c) {
console.log(`${c.user.tag} is ready for operation!!`);
});
client.on('interactionCreate', async (interaction) => {
if (interaction.isChatInputCommand()) {
if (interaction.commandName === "progress") {
let lists = await fetch('https://api.trello.com/1/boards/' + process.env.BOARD_ID + '/lists?key=' + process.env.TRELLO_API_KEY)
let listsData = await lists.json();
let fieldz = [];
let totalCheckItemsGlobal = 0;
let totalCheckedItemsGlobal = 0;
for (const list of listsData) {
let cardsInList = await fetch('https://api.trello.com/1/lists/' + list.id + '/cards?key=' + process.env.TRELLO_API_KEY);
let cardsData = await cardsInList.json();
let totalCheckItems = 0;
let totalCheckedItems = 0;
for (const card of cardsData) {
if (card.badges.checkItems && card.badges.checkItems > 0) {
totalCheckItems += card.badges.checkItems;
totalCheckedItems += card.badges.checkItemsChecked;
totalCheckItemsGlobal += card.badges.checkItems;
totalCheckedItemsGlobal += card.badges.checkItemsChecked;
}
}
if (totalCheckItems > 0) { // Only consider lists with one or more cards
let progressPercent = totalCheckItems > 0 ? Math.floor((totalCheckedItems / totalCheckItems) * 100) : 0;
fieldz.push({
name: list.name + ' (' + progressPercent + '%)',
value: 'Total: ' + totalCheckItems + '\nDone: ' + totalCheckedItems,
inline: true
});
}
}
let overallProgressPercent = totalCheckItemsGlobal > 0 ? Math.floor((totalCheckedItemsGlobal / totalCheckItemsGlobal) * 100) : 0;
let embed = new EmbedBuilder()
.setTitle(process.env.BOARD_NAME + ': Progress (' + overallProgressPercent + '%)')
.setFields(fieldz)
.setColor('Purple')
.setFooter({
text: process.env.FOOTER_TEXT,
})
.setThumbnail(process.env.THUMBNAIL_URL);
interaction.reply({ embeds: [embed] });
}
}
});
client.login(process.env.TOKEN);