-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Discord command to list pinned messages (#224)
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 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
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, | ||
]; |
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,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)}...`; | ||
}; |