-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MessageCommand command to Discord bot #20
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call it
ensureMessageDataLoaded