-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
119 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const effortScore = (xp, score = 1) => { | ||
const needs = score * 5; | ||
return xp > needs ? effortScore(xp - needs, score + 1) : score + xp / needs; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { startOfWeek } from "date-fns"; | ||
import { EmbedBuilder } from "discord.js"; | ||
import schedule from "node-schedule"; | ||
import { loggedWorkoutCollection } from "./LoggedWorkout"; | ||
import { effortScore } from "./EffortScore"; | ||
|
||
const unique = (arr) => Array.from(new Set(arr)); | ||
const notNull = (x) => !!x; | ||
|
||
/** | ||
* @param {import("discord.js").Client} discord | ||
* @param {import("mongodb").Db} db | ||
*/ | ||
export const createRecap = (discord, db) => async () => { | ||
const workouts = loggedWorkoutCollection(db); | ||
const monday = startOfWeek(new Date(), { weekStartsOn: 1 }); | ||
|
||
const workoutsThisWeek = await workouts | ||
.find({ insertedDate: { $gt: monday.toISOString() } }) | ||
.toArray(); | ||
|
||
const discordIds = unique(workoutsThisWeek.map((x) => x.discordId)); | ||
|
||
const guild = await discord.guilds.fetch(process.env.SERVER_ID); | ||
const members = await guild.members.fetch(discordIds); | ||
await guild.members.fetch(discordIds); | ||
|
||
return new EmbedBuilder({ | ||
color: 0x4287f5, | ||
author: { | ||
icon_url: "https://imgur.com/iRWWVZY.png", | ||
name: "Monday Moist Recap", | ||
}, | ||
description: | ||
"Here's everyone's total 💦 score from the last week!\n\n" + | ||
members | ||
.filter((m) => workoutsThisWeek.some((x) => x.discordId === m.id)) | ||
.map((member) => { | ||
const exp = workoutsThisWeek | ||
.filter((x) => x.discordId === member.id) | ||
.reduce((sum, a) => sum + a.exp, 0); | ||
|
||
return { | ||
username: member.nickname ?? member.user.username, | ||
score: effortScore(exp), | ||
}; | ||
}) | ||
.filter(notNull) | ||
.sort((a, b) => (a.score > b.score ? -1 : 1)) | ||
.map((x) => `🔹 **${x.username}** ${x.score.toFixed(1)}`) | ||
.join("\n"), | ||
}); | ||
}; | ||
|
||
export const schedulePost = async (discord, db) => { | ||
const create = createRecap(discord, db); | ||
|
||
const post = async () => { | ||
const embed = await create(); | ||
const channel = await discord.channels.fetch(process.env.CHANNEL_STRAVA); | ||
await channel.send({ embeds: [embed] }); | ||
}; | ||
|
||
schedule.scheduleJob("0 9 * * MON", () => post()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,30 @@ | ||
export { stravaWebhookHandler } from "./LoggedWorkout"; | ||
export { schedulePost } from "./MondayRecap"; | ||
|
||
import { PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; | ||
import { createRecap } from "./MondayRecap"; | ||
|
||
export const fitSlashConfig = new SlashCommandBuilder() | ||
.setName("fit_admin") | ||
.setDescription("foo bar") | ||
.setDefaultMemberPermissions(PermissionFlagsBits.KickMembers) | ||
.addSubcommand((cmd) => | ||
cmd | ||
.setName("monday") | ||
.setDescription("Preview the fitness bot's monday post."), | ||
); | ||
|
||
export const fitInteractionHandler = (db) => async (interaction) => { | ||
if ("fit_admin" === interaction.commandName) { | ||
const sub = interaction.options.getSubcommand(); | ||
|
||
if ("monday" === sub) { | ||
const embed = await createRecap(interaction.client, db)(); | ||
interaction.reply({ | ||
content: "Here is a preview of the upcoming monday recap", | ||
embeds: [embed], | ||
ephemeral: true, | ||
}); | ||
} | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.