Skip to content

Commit

Permalink
feat: stickies for forum posts which get posted on forum post create (#…
Browse files Browse the repository at this point in the history
…384)

Co-authored-by: BenW <[email protected]>
  • Loading branch information
pdellaert and benw202 authored Dec 19, 2022
1 parent d4dff6c commit 001c0eb
Show file tree
Hide file tree
Showing 8 changed files with 715 additions and 128 deletions.
1 change: 1 addition & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Update <small>_ December 2022</small>

- feat: stickies for forum posts which get posted on forum post create (19/12/2022)
- refactor: outdated information + links in commands (08/12/2022)
- fix: fix latlong image typo and prime meridian alignment (04/12/2022)
- refactor: update wb command for change in Stable (03/12/2022)
Expand Down
1 change: 1 addition & 0 deletions .github/command-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
| .faq | Sends the FAQ | --- |
| .roleassignment | Sends the role assignment messages | --- |
| .rules | Sends the rules | --- |
| .sticky | Manage sticky messages which are posted in Forum Posts when a new post is created | --- |
| .temporarycommandedit | Manage temporary commands, which are simple output commands to highlight temporary situations to users. | .tempcommandedit <br> .tcedit <br> .tcmod |
| .timeout | --- | --- |
| .unban | --- | --- |
Expand Down
313 changes: 185 additions & 128 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ import { flyPadAbout } from './support/flyPadAbout';
import { navdata } from './support/navdata';
import { winss } from './support/winss';
import { simridgeLog } from './support/simbridgeLog';
import { sticky } from './moderation/sticky';

const commands: BaseCommandDefinition[] = [
typeCommand,
Expand Down Expand Up @@ -310,6 +311,7 @@ const commands: BaseCommandDefinition[] = [
navdata,
winss,
simridgeLog,
sticky,
];

const commandsObject: { [k: string]: BaseCommandDefinition } = {};
Expand Down
460 changes: 460 additions & 0 deletions src/commands/moderation/sticky.ts

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions src/handlers/forumPostCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getConn } from '../lib/db';
import Logger from '../lib/logger';
import StickyMessage from '../lib/schemas/stickyMessageSchema';
import { stickyMessageEmbed } from '../lib/stickyMessageEmbed';

module.exports = {
event: 'threadCreate',
executor: async (thread) => {
const { parentId, parent, name } = thread;
Logger.debug(`Thread Create - Handling thread create for ${name}`);
const { name: parentName, type } = parent;
Logger.debug(`Thread Create - Parent ID: ${parentId} - Parent Type: ${type} - Parent Name: ${parentName}`);
if (type !== 15) {
Logger.debug('Thread Create - Thread not created in a Forum Channel, skipping.');
return;
}

const conn = getConn();
if (!conn) {
Logger.debug('Thread Create - Unable to connect to database, skipping.');
return;
}

const stickyMessageSearchResult = await StickyMessage.find({ channelId: parentId });
const [stickyMessage] = stickyMessageSearchResult.length === 1 ? stickyMessageSearchResult : [];
if (!stickyMessage) {
Logger.debug('Forum Post Create - No sticky for the forum, skipping.');
return;
}

const { message, imageUrl } = stickyMessage;
Logger.debug('Forum Post Create - Posting new sticky.');
await thread.send({ embeds: [stickyMessageEmbed(message, imageUrl)] });
},
};
19 changes: 19 additions & 0 deletions src/lib/schemas/stickyMessageSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose, { Schema } from 'mongoose';

const stickyMessageSchema = new Schema({
channelId: {
type: String,
unique: true,
},
message: String,
imageUrl: String,
timeInterval: Number,
messageCount: Number,
moderator: String,
updatedTimestamp: Date,
lastPostedId: String,
});

const stickyMessage = mongoose.model('StickyMessage', stickyMessageSchema);

export default stickyMessage;
12 changes: 12 additions & 0 deletions src/lib/stickyMessageEmbed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Colors } from '../constants';
import { makeEmbed } from './embed';

export const STICKY_MESSAGE_TITLE = 'Stickied Message';
export const STICKY_MOD_TITLE = 'Sticky Message - Log';

export const stickyMessageEmbed = (description: string, imageUrl: string) => makeEmbed({
title: STICKY_MESSAGE_TITLE,
description,
color: Colors.FBW_CYAN,
image: imageUrl ? { url: imageUrl } : null,
});

0 comments on commit 001c0eb

Please sign in to comment.