diff --git a/services/bots/src/discord/commands/common/index.ts b/services/bots/src/discord/commands/common/index.ts index ae2263d..ac7a3a4 100644 --- a/services/bots/src/discord/commands/common/index.ts +++ b/services/bots/src/discord/commands/common/index.ts @@ -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, ]; diff --git a/services/bots/src/discord/commands/common/pinned.ts b/services/bots/src/discord/commands/common/pinned.ts new file mode 100644 index 0000000..b988cad --- /dev/null +++ b/services/bots/src/discord/commands/common/pinned.ts @@ -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 { + @CommandHandler() + async handler( + @Payload() handlerDto: BlankDto, + { interaction }: TransformedCommandExecutionContext, + ): Promise { + 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)}...`; +};