Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e51e151
feat: add showcase-digest
michal-skraburski Oct 13, 2026
1407a2b
fix: split the showcase-digest in multiple files
michal-skraburski Oct 13, 2026
1ac90c0
fix: split out the functions for showcase digest
michal-skraburski Oct 13, 2026
9ea1842
fix: hoist Date.now() out of filter per post
michal-skraburski Oct 13, 2026
d0697b4
fix: rewrite conversion to be safe, apply KISS&DRY
michal-skraburski Oct 13, 2026
55f3d06
fix: move functions out of handlers into util + unit tests
michal-skraburski Oct 14, 2026
4dd7a2d
feat: add showcase-digest cronjob trigger
michal-skraburski Oct 14, 2026
abd0f69
fix: add missing functions for showcase-digest
michal-skraburski Oct 14, 2026
5549661
feat: add pingable role
michal-skraburski Oct 14, 2026
33c2c9f
fix: update the default emoji to thumbsup
michal-skraburski Oct 15, 2026
d988d95
fix: prevent non-moderators from running manual digest
michal-skraburski Oct 15, 2026
ec44c5f
feat: allow the digest scheduler to only execute on within correct ti…
michal-skraburski Oct 15, 2026
c99c7cc
fix: append example vars into .env.example + typo fix
michal-skraburski Oct 15, 2026
9200e44
fix: correct import on commands/index.ts
michal-skraburski Oct 15, 2026
1c3e8c8
fix: add missing .env.test vars
michal-skraburski Oct 15, 2026
a181b32
fix: rm comments & lint
michal-skraburski Oct 18, 2026
fc63a36
feat: drop variables showcase-digest config from .env
michal-skraburski Oct 18, 2026
c4c0b89
fix: refactor code to prefer throwing errors and handling them
michal-skraburski Oct 18, 2026
2f91566
fix: update all to use codeblocks, allSettled use, updated user msgs
michal-skraburski Oct 18, 2026
714ed49
fix: spelling, error handling fixes, digest cron simplified
michal-skraburski Oct 18, 2026
6320ad2
fix: update sortByDescPost to byLikesDescending
michal-skraburski Oct 18, 2026
5a1a243
fix: formatting on byLikesDescending
michal-skraburski Oct 18, 2026
5b64411
fix: stale test from rejection handling
michal-skraburski Oct 18, 2026
2fec8f3
fix: readd diff line from showcase accident
michal-skraburski Oct 18, 2026
5190b38
fix: remove promise.then, simplify error class
michal-skraburski Oct 18, 2026
8ccd500
fix: add digest .env vars to production
michal-skraburski Oct 18, 2026
d4c41b7
fix: remove unused import
michal-skraburski Oct 18, 2026
951d020
fix: rename showcase error vars to useful names
michal-skraburski Oct 18, 2026
e74dd04
fix: update showcase error to be more verbose
michal-skraburski Oct 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ REPEL_LOG_CHANNEL_ID=your_repel_log_channel_id_here
ONBOARDING_CHANNEL_ID=onboarding_channel_id_here
SPAM_DETECTION_CHANNEL_ID=spam_detection_channel_id_here
ARCHIVE_CATEGORY_ID=archive_category_id_here
SHOWCASE_DIGEST_CHANNEL_ID=showcase_digest_channel_id_here

# Role IDs (REQUIRED)
MODERATORS_ROLE_IDS=role_id_1,role_id_2,role_id_3
REPEL_ROLE_ID=your_repel_role_id_here
ONBOARDING_ROLE_ID=onboarding_role_id_here
TAG_ACCESS_ROLE_ID=tag_access_role_id_here
DIGEST_ROLE_ID=digest_role_id_here

# Optional Role IDs
# ROLE_A_ID=optional_role_a_id
Expand All @@ -32,6 +34,3 @@ ADVENT_OF_CODE_TRACKER_PATH=advent-of-code-tracker.json


DATABASE_URL=file:./data/webdev.db



3 changes: 2 additions & 1 deletion .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ SHOWCASE_LOG_CHANNEL_ID=1517565847982444634
SHOWCASE_RULES_CHANNEL_ID=1517948527098073158
SPAM_DETECTION_CHANNEL_ID=973895712314110032
ARCHIVE_CATEGORY_ID=837507969859977258

SHOWCASE_DIGEST_CHANNEL_ID=1561474075745059017

# Role IDs (from your dev server)
REPEL_ROLE_ID=1002411741776461844
MODERATORS_ROLE_IDS=849481536654803004
REGULAR_ROLE_ID=1383170370634514544
TAG_ACCESS_ROLE_ID=1502128862224711805
DIGEST_ROLE_ID=1561474157479207032

# Other
GUIDES_TRACKER_PATH=/app/data/guides-tracker.json
Expand Down
2 changes: 2 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ SHOWCASE_LOG_CHANNEL_ID=your-showcase-log-channel-id
SHOWCASE_RULES_CHANNEL_ID=your-showcase-rules-channel-id
SPAM_DETECTION_CHANNEL_ID=spam-detection-channel-id-here
ARCHIVE_CATEGORY_ID=your-archived-category-id
SHOWCASE_DIGEST_CHANNEL_ID=your-showcase-digest-channel-id

# Role IDs (from your dev server)
REPEL_ROLE_ID=your-repel-role-id
MODERATORS_ROLE_IDS=your-moderator-role-id
REGULAR_ROLE_ID=your-regular-role-id
TAG_ACCESS_ROLE_ID=your-tag-access-role-id
DIGEST_ROLE_ID=your-digest-role-id

# Other
GUIDES_TRACKER_PATH=guides-tracker.json
Expand Down
2 changes: 2 additions & 0 deletions src/common/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Command } from './types.js';
import { reportMessage } from '@/features/report-message/index.js';
import { tagCommand } from '@/features/tags/index.js';
import { botOptionsCommand } from '@/features/bot-options/index.js';
import { createDigestCommand } from '@/features/showcase-digest/triggers.js';

export const commands = new Map<string, Command>(
[
Expand All @@ -22,6 +23,7 @@ export const commands = new Map<string, Command>(
cacheMessages,
publicGuidesCommand,
createShowcaseCommand,
createDigestCommand,
sendShowcasePinnedMessage,
reportMessage,
tagCommand,
Expand Down
2 changes: 2 additions & 0 deletions src/constants/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ChannelTypeMap = {
showcase: ForumChannel;
showcaseLogs: TextChannel;
showcaseRules: TextChannel;
showcaseDigest: TextChannel;
spamDetection: TextChannel;
archiveCategory: CategoryChannel;
};
Expand All @@ -27,6 +28,7 @@ const EXPECTED_DISCORD_TYPE: Record<ChannelKey, ChannelType> = {
showcase: ChannelType.GuildForum,
showcaseLogs: ChannelType.GuildText,
showcaseRules: ChannelType.GuildText,
showcaseDigest: ChannelType.GuildText,
spamDetection: ChannelType.GuildText,
archiveCategory: ChannelType.GuildCategory,
};
Expand Down
2 changes: 2 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const config = {
b: optionalEnv('ROLE_B_ID'),
c: optionalEnv('ROLE_C_ID'),
tagAccess: requireEnv('TAG_ACCESS_ROLE_ID'),
digestRole: requireEnv('DIGEST_ROLE_ID'),
},
channelIds: {
repelLogs: requireEnv('REPEL_LOG_CHANNEL_ID'),
Expand All @@ -43,6 +44,7 @@ export const config = {
showcase: requireEnv('SHOWCASE_CHANNEL_ID'),
showcaseLogs: requireEnv('SHOWCASE_LOG_CHANNEL_ID'),
showcaseRules: requireEnv('SHOWCASE_RULES_CHANNEL_ID'),
showcaseDigest: requireEnv('SHOWCASE_DIGEST_CHANNEL_ID'),
spamDetection: requireEnv('SPAM_DETECTION_CHANNEL_ID'),
archiveCategory: requireEnv('ARCHIVE_CATEGORY_ID'),
},
Expand Down
8 changes: 7 additions & 1 deletion src/features/ready/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createEvent } from '@/common/events/create-event.js';
import { resolveChannels } from '@/constants/channels.js';
import { config } from '@/env.js';
import { initializeAdventScheduler } from '@/util/advent-scheduler.js';
import { initializeShowcaseDigestScheduler } from '@/features/showcase-digest/triggers.js';
import { fetchAndCachePublicChannelsMessages } from '@/util/channel-prefetch.js';
import { syncGuidesToChannel } from '@/util/post-guides.js';
import { leaveIfNotAllowedServer } from '@/util/server-guard.js';
Expand Down Expand Up @@ -58,13 +59,18 @@ export const readyEvent = createEvent(
}
}

// Initialize Advent of Code scheduler
try {
initializeAdventScheduler(client, config.channelIds.adventOfCode);
} catch (error) {
console.error('❌ Failed to initialize Advent of Code scheduler:', error);
}

try {
initializeShowcaseDigestScheduler();
} catch (error) {
console.error('Failed to initialize showcase digest scheduler:', error);
}

// Make sure all channels in the archived category are properly archived on startup
try {
await syncArchiveCategoryChannels();
Expand Down
2 changes: 2 additions & 0 deletions src/features/showcase-digest/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const LOOKBACK_N: number = 14;
export const REACT_EMOJI = '%F0%9F%91%8D';
129 changes: 129 additions & 0 deletions src/features/showcase-digest/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { SERVER_CHANNELS } from '@/constants/channels.js';
import { config } from '@/env.js';
import { getCommandUser } from '@/util/member.js';
import { isModerator } from '@/util/permissions.js';
import { LOOKBACK_N, REACT_EMOJI } from './config.js';
import {
ButtonInteraction,
ChatInputCommandInteraction,
Collection,
MessageFlags,
ContainerBuilder,
TextDisplayBuilder,
ForumThreadChannel,
channelMention,
roleMention,
} from 'discord.js';
import {
convertPosts,
filterPostsByCreationTime,
type Post,
ShowcaseDigestValidationError,
} from './util.js';

const postProcessor = async (posts: Collection<string, ForumThreadChannel>) => {
const postsUnordered: Post[] = await convertPosts(posts, REACT_EMOJI);
const byLikesDescending = (postA: Post, postB: Post) => {
return postB.likes - postA.likes;
};
return postsUnordered.sort(byLikesDescending);
};

function createContainer(posts: Post[]): ContainerBuilder {
if (posts.length == 0) {
throw new ShowcaseDigestValidationError(
'Something went wrong. Please try again later.',
'createContainer passed posts[] with length 0. Impossible scenario.'
);
}

const container = new ContainerBuilder();
container.addTextDisplayComponents(
new TextDisplayBuilder().setContent('## Showcase Digest')
);

posts.forEach((post, index) => {
container.addTextDisplayComponents(
new TextDisplayBuilder().setContent(
`${index + 1}. ${channelMention(post.id)} (Comments: ${post.messageCount})`
)
);
});

container.addTextDisplayComponents(
new TextDisplayBuilder().setContent(
`-# ${roleMention(config.roleIds.digestRole)}`
)
);

return container;
}

export const createShowcaseDigest = async (): Promise<void> => {
const digestChannel = SERVER_CHANNELS.showcaseDigest;
const forumChannel = SERVER_CHANNELS.showcase;
const posts = filterPostsByCreationTime(
forumChannel.threads.cache,
LOOKBACK_N
);

if (posts.size === 0) {
throw new ShowcaseDigestValidationError(
'There was no posts to be displayed in Showcase Digest.',
'There was no posts to display in this digest.'
);
}

const postsOrdered: Post[] = await postProcessor(posts);
const container = createContainer(postsOrdered);

await digestChannel.send({
flags: MessageFlags.IsComponentsV2,
components: [container],
allowedMentions: { roles: [config.roleIds.digestRole] },
});
};

export const digestHandler = async (
interaction: ButtonInteraction | ChatInputCommandInteraction
) => {
if (!interaction.inGuild() || !interaction.guild) {
await interaction.reply({
content: 'This command can only be run in the web development discord.',
flags: MessageFlags.Ephemeral,
});
return;
}

if (!isModerator(getCommandUser(interaction))) {
await interaction.reply({
content: 'Only moderators can run the showcase digest.',
flags: MessageFlags.Ephemeral,
});
return;
}

try {
await createShowcaseDigest();
} catch (error: unknown) {
if (error instanceof ShowcaseDigestValidationError) {
console.error(error.message);
await interaction.reply({
flags: MessageFlags.Ephemeral,
content: error.getExternalMessage(),
});
} else {
console.error('An unexpected error caught:', error);
await interaction.reply({
flags: MessageFlags.Ephemeral,
content: 'Something went wrong, please try again.',
});
}
return;
}

await interaction.reply({
flags: MessageFlags.Ephemeral,
content: 'Successfully created a showcase digest!',
});
};
39 changes: 39 additions & 0 deletions src/features/showcase-digest/triggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as cron from 'node-cron';
import { createSlashCommand } from '@/common/commands/create-commands.js';
import { LOOKBACK_N } from './config.js';
import { digestHandler, createShowcaseDigest } from './handlers.js';

export const createDigestCommand = createSlashCommand({
data: {
name: 'digest-force',
description: `Forcefully consumes posts of the last ${LOOKBACK_N} days`,
},
execute: digestHandler,
});

// Fire on the 1st and 3rd Friday of every month at 16:00 UTC using node-cron's
// nth-weekday syntax (5#1 = first Friday, 5#3 = third Friday).
// https://www.nodecron.com/cron-syntax#nth-weekday
const SHOWCASE_DIGEST_CRONS = ['0 16 * * 5#1', '0 16 * * 5#3'];

const runScheduledShowcaseDigest = async (): Promise<void> => {
console.log('Running scheduled showcase digest...');
try {
await createShowcaseDigest();
console.log(`Successfully displayed showcase digest!`);
} catch (error) {
console.error(error);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful

};

export function initializeShowcaseDigestScheduler(): void {
console.log('Initializing showcase digest scheduler...');

SHOWCASE_DIGEST_CRONS.forEach((expression) => {
cron.schedule(expression, runScheduledShowcaseDigest, { timezone: 'UTC' });
});

console.log(
'Showcase digest scheduler initialized (runs 1st and 3rd Fridays at 16:00 UTC)'
);
}
Loading
Loading