-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MessageCommand command to Discord bot
- Loading branch information
Showing
6 changed files
with
148 additions
and
1 deletion.
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
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,3 @@ | ||
hello_world: | ||
description: "Print hello world message" | ||
content: "Hello world :wave:" |
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
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,124 @@ | ||
import fetch from 'node-fetch'; | ||
import yaml from 'js-yaml'; | ||
|
||
import { TransformPipe } from '@discord-nestjs/common'; | ||
import { | ||
DiscordTransformedCommand, | ||
Payload, | ||
TransformedCommandExecutionContext, | ||
Param, | ||
UsePipes, | ||
On, | ||
} from '@discord-nestjs/core'; | ||
import { CommandHandler, DiscordCommandClass } from '../discord.decorator'; | ||
import { AutocompleteInteraction, EmbedBuilder } from 'discord.js'; | ||
import { reportException } from '@lib/sentry/reporting'; | ||
|
||
const DATA_FILE_URL = | ||
'https://raw.githubusercontent.com/home-assistant/service-hub/main/data/discord_messages.yaml'; | ||
|
||
interface MessageData { | ||
[key: string]: { description: string; content: string }; | ||
} | ||
|
||
class MessageDto { | ||
@Param({ | ||
name: 'message', | ||
description: 'What message do you want to post?', | ||
required: true, | ||
autocomplete: true, | ||
}) | ||
messageKey: string; | ||
} | ||
|
||
@DiscordCommandClass({ | ||
name: 'message', | ||
description: 'Returns a predefined message', | ||
}) | ||
@UsePipes(TransformPipe) | ||
export class MessageCommand implements DiscordTransformedCommand<MessageDto> { | ||
private messageData: MessageData; | ||
|
||
async reloadMessageData(force?: boolean): Promise<void> { | ||
if (force || !this.messageData) { | ||
this.messageData = yaml.load(await (await fetch(DATA_FILE_URL)).text(), { | ||
json: true, | ||
}) as MessageData; | ||
} | ||
} | ||
@CommandHandler() | ||
async handler( | ||
@Payload() handlerDto: MessageDto, | ||
context: TransformedCommandExecutionContext, | ||
): Promise<void> { | ||
const { messageKey } = handlerDto; | ||
const { interaction } = context; | ||
if (messageKey === 'reload') { | ||
await this.reloadMessageData(true); | ||
|
||
await interaction.reply({ | ||
content: 'Message list reloaded', | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
if (!this.messageData[messageKey]) { | ||
await interaction.reply({ | ||
content: 'Could not find information', | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
await this.reloadMessageData(); | ||
|
||
await interaction.reply({ | ||
embeds: [ | ||
new EmbedBuilder({ | ||
description: this.messageData[messageKey].content, | ||
}), | ||
], | ||
}); | ||
} | ||
|
||
// This is the autocomplete handler for the /message command | ||
@On('interactionCreate') | ||
async onInteractionCreate(interaction: AutocompleteInteraction): Promise<void> { | ||
if (!interaction.isAutocomplete() || interaction.commandName !== 'message') { | ||
return; | ||
} | ||
try { | ||
await this.reloadMessageData(); | ||
const focusedValue = interaction.options.getFocused()?.toLowerCase(); | ||
|
||
await interaction.respond( | ||
focusedValue.length !== 0 | ||
? Object.entries(this.messageData) | ||
.map(([key, data]) => ({ | ||
name: data.description, | ||
value: key, | ||
})) | ||
.filter( | ||
(choice) => | ||
choice.value.toLowerCase().includes(focusedValue) || | ||
choice.name.toLowerCase().includes(focusedValue), | ||
) | ||
// The API only allow max 25 sugestions | ||
.slice(0, 25) | ||
: [], | ||
); | ||
} catch (err) { | ||
reportException(err, { | ||
cause: err, | ||
data: { | ||
interaction: interaction.toJSON(), | ||
user: interaction.user.toJSON(), | ||
channel: interaction.channel.toJSON(), | ||
command: interaction.command.toJSON(), | ||
}, | ||
}); | ||
await interaction.respond([]); | ||
} | ||
} | ||
} |
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
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