Add Streak Calculation

This commit is contained in:
Michael 2024-12-23 20:06:53 -06:00
parent e813f39ab2
commit 419e25ce46
Signed by: TheShadowEevee
GPG key ID: 7A8AA92B3BAFAB75

View file

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