Skip to content

Commit

Permalink
Implement Discord command to list pinned messages (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Dec 5, 2023
1 parent c4fae7f commit 15057cd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions services/bots/src/discord/commands/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { CommandCommonInfo } from './info';
import { CommandCommonMessage } from './message';
import { CommandCommonPing } from './ping';
import { CommandCommonPinned } from './pinned';
import { CommandCommonTopic } from './topic';

export const CommandsCommon = [
CommandCommonInfo,
CommandCommonMessage,
CommandCommonPing,
CommandCommonPinned,
CommandCommonTopic,
];
50 changes: 50 additions & 0 deletions services/bots/src/discord/commands/common/pinned.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { TransformPipe } from '@discord-nestjs/common';
import {
DiscordTransformedCommand,
Payload,
TransformedCommandExecutionContext,
UsePipes,
} from '@discord-nestjs/core';
import { CommandHandler, DiscordCommandClass } from '../../discord.decorator';
import { BlankDto } from '../../discord.const';

@DiscordCommandClass({
name: 'pinned',
description: 'Returns pinned messages',
})
@UsePipes(TransformPipe)
export class CommandCommonPinned implements DiscordTransformedCommand<any> {
@CommandHandler()
async handler(
@Payload() handlerDto: BlankDto,
{ interaction }: TransformedCommandExecutionContext,
): Promise<void> {
const pinned = await interaction.channel.messages.fetchPinned();

if (pinned.size === 0) {
await interaction.reply({ content: 'No pinned messages in this channel', ephemeral: true });
return;
}

await interaction.reply({
embeds: [
{
title: 'The pinned messages of this channel are:',
description: pinned
.map(
(message) =>
`- ["${transformConetent(message.content) || 'embeded content'}"](<${
message.url
}>)`,
)
.join('\n'),
},
],
});
}
}

const transformConetent = (content: string): string => {
const base = content.replace(/\n/g, ' ');
return base.length < 64 ? base : `${base.substring(0, 64)}...`;
};

0 comments on commit 15057cd

Please sign in to comment.