diff --git a/commands/commit-count.js b/commands/commit-count.js index 6e7f536..50abcf6 100644 --- a/commands/commit-count.js +++ b/commands/commit-count.js @@ -1,10 +1,13 @@ /* -* Commit Overflow Counting - Slash Command Definition File -* commit-count.js - Gets an initial commit count for Commit Overflow -*/ + * Commit Overflow Counting - Slash Command Definition File + * commit-count.js - Gets an initial commit count for Commit Overflow + */ const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); -const { convertSnowflakeToDate, convertDateToSnowflake } = require('../utils/convert.js'); +const { + convertSnowflakeToDate, + convertDateToSnowflake, +} = require('../utils/convert.js'); module.exports = { data: new SlashCommandBuilder() @@ -16,21 +19,47 @@ module.exports = { // .addMentionableOption(option => // option.setName('role') // .setDescription('Choose a role for those that can react to verify')) - .addStringOption(option => - option.setName('start') - .setDescription('Optionally specify a start date (MM/DD/YYYY preferred)')) - .addStringOption(option => - option.setName('end') - .setDescription('Optionally specify a end date (MM/DD/YYYY preferred)')), + .addStringOption((option) => + option + .setName('start') + .setDescription( + 'Optionally specify a start date (MM/DD/YYYY preferred)', + ), + ) + .addStringOption((option) => + option + .setName('end') + .setDescription('Optionally specify a end date (MM/DD/YYYY preferred)'), + ) + .addStringOption((option) => + option + .setName('ephemeral') + .setDescription('Post the avatar in the current channel') + .addChoices( + { name: 'Send to me only', value: 'true' }, + { name: 'Send in channel', value: 'false' }, + ), + ), async execute(interaction) { if (interaction.inGuild() && interaction.channel.isThread()) { - const threadStarterMessage = await interaction.channel.fetchStarterMessage(); - const emojiCheck = interaction.options.getString('emoji') ?? 'gh_green_square'; - const roleCheck = interaction.options.getString('role') ?? '1232803664150659133'; - const startDate = new Date(interaction.options.getString('start-date') ?? threadStarterMessage.createdTimestamp); - const endDate = new Date(interaction.options.getString('end-date') ?? interaction.createdTimestamp); + const threadStarterMessage = + await interaction.channel.fetchStarterMessage(); + const emojiCheck = + interaction.options.getString('emoji') ?? 'gh_green_square'; + const roleCheck = + interaction.options.getString('role') ?? '1012751663322382438'; + const startDate = new Date( + interaction.options.getString('start-date') ?? + threadStarterMessage.createdTimestamp, + ); + // let endDate = new Date( + // interaction.options.getString('end-date') ?? + // interaction.createdTimestamp, + // ); + const isEphemeral = + interaction.options.getString('ephemeral') ?? 'true'; - await interaction.deferReply(); + await interaction.deferReply({ ephemeral: (isEphemeral === 'true') }); // Fetch Messages const sum_messages = []; @@ -96,25 +125,31 @@ module.exports = { const messageList = messageMap.get(date); for (const messageId of messageList) { - if (dateVerified != true) { - await interaction.channel.messages.fetch(messageId) - .then(async message => { - if (/http.:\/\/.*\/(commit|compare)\/.*/g.test(message.content)) { + // Complicated... But TLDR gives priority to Commit URL, then Attachment, then Reaction + if (verifyReason == '' || verifyReason != 'Attachment' || verifyReason != 'Reaction') { + await interaction.channel.messages + .fetch(messageId) + .then(async (message) => { + if ( + /http.:\/\/.*\/(commit|compare)\/.*/g.test(message.content) + ) { dateVerified = true; verifyReason = 'Commit URL'; } - else if (message.attachments.size > 0) { + else if ((message.attachments.size > 0) && verifyReason != 'Commit URL') { dateVerified = true; verifyReason = 'Attachment'; } - else if (message.reactions.cache.size > 0) { + else if ((message.reactions.cache.size > 0) && verifyReason != 'Commit URL' && verifyReason != 'Attachment') { for (const reaction of message.reactions.cache.values()) { const emojiName = reaction._emoji.name; const reactionUsers = await reaction.users.fetch(); if (emojiName === emojiCheck) { for (const [userId] of reactionUsers) { - const member = await interaction.guild.members.fetch(userId); + const member = await interaction.guild.members.fetch( + userId, + ); if (member.roles.cache.has(roleCheck)) { dateVerified = true; verifyReason = 'Reaction'; @@ -130,7 +165,7 @@ module.exports = { } if (dateVerified) { - validString += `${date}: ${verifyReason}\n`; + validString = `${date}: ${verifyReason}\n` + validString; validDays.push(date); } } @@ -148,10 +183,19 @@ module.exports = { const allDays = []; const dayMap = new Map(); - let currentDate = startDate; + const currentDate = new Date( + interaction.options.getString('start-date') ?? + threadStarterMessage.createdTimestamp, + ); + const endDate = new Date( + interaction.options.getString('end-date') ?? + interaction.createdTimestamp, + ); + while (currentDate <= endDate) { allDays.push(new Date(currentDate)); - currentDate = currentDate.setDate(currentDate.getDate + 1); + currentDate.setDate(currentDate.getDate() + 1); + console.log(allDays); } for (const day of allDays) { @@ -173,34 +217,55 @@ module.exports = { invalidString += `${formattedDay}\n`; dayMap.set(formattedDay, false); } + console.log(dayMap); } - //for (const [day, hasCommit] of dayMap) { + let streakCount = 0; + const streaksArray = []; - //} + for (const [, hasCommit] of dayMap) { + if (hasCommit) { + streakCount += 1; + } + else { + streaksArray.push(streakCount); + streakCount = 0; + } + } + + streaksArray.push(streakCount); + streakCount = 0; if (invalidString != '') { - resultsEmbed.fields.push( - { - name: 'Missed Days', - value: invalidString, - }, - ); + resultsEmbed.fields.push({ + name: 'Missed Days', + value: invalidString, + }); - resultsEmbed.footer = - { + resultsEmbed.footer = { text: `Organizers: Please check the above dates and react with ${emojiCheck} on any missed commits!`, }; } + // Largest Array Value One Liner from https://stackoverflow.com/a/30850912 resultsEmbed.fields.push( { name: 'Total Commit Count', value: validDays.length, }, + { + name: 'Largest Streak', + value: + streaksArray[ + streaksArray.reduce( + (iMax, x, i, arr) => (x > arr[iMax] ? i : iMax), + 0, + ) + ] ?? 0, + }, ); - await interaction.editReply({ embeds: [resultsEmbed], ephemeral: false }); + await interaction.editReply({ embeds: [resultsEmbed], ephemeral: (isEphemeral === 'true') }); } else { const notInThread = new EmbedBuilder() @@ -210,4 +275,4 @@ module.exports = { await interaction.reply({ embeds: [notInThread], ephemeral: true }); } }, -}; \ No newline at end of file +};