finished sending messages guide
This commit is contained in:
@@ -71,8 +71,96 @@ const reacord = new ReacordDiscordJs(client, {
|
|||||||
|
|
||||||
## Discord Slash Commands
|
## Discord Slash Commands
|
||||||
|
|
||||||
<aside class="opacity-75 italic border-l-4 pl-3 border-white/50">
|
<aside>
|
||||||
This section also applies to other kinds of application commands, such as context menu commands.
|
This section also applies to other kinds of application commands, such as context menu commands.
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
todo
|
To reply to a command interaction, use the `.reply()` function. This function returns an instance that works the same way as the one from `.send()`. Here's an example:
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
import { Client } from "discord.js"
|
||||||
|
import * as React from "react"
|
||||||
|
import { Button, ReacordDiscordJs } from "reacord"
|
||||||
|
|
||||||
|
const client = new Client({ intents: [] })
|
||||||
|
const reacord = new ReacordDiscordJs(client)
|
||||||
|
|
||||||
|
client.on("ready", () => {
|
||||||
|
client.application?.commands.create({
|
||||||
|
name: "pong",
|
||||||
|
description: "pong!",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
client.on("interactionCreate", (interaction) => {
|
||||||
|
if (interaction.isCommand() && interaction.commandName === "pong") {
|
||||||
|
// Use the reply() function instead of send
|
||||||
|
reacord.reply(interaction, <>pong</>)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
client.login(process.env.DISCORD_TOKEN)
|
||||||
|
```
|
||||||
|
|
||||||
|
<aside>
|
||||||
|
This example uses <a href="https://discord.com/developers/docs/interactions/application-commands#registering-a-command">global commands</a>, so the command might take a while to show up 😅
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
However, the process of creating commands can get really repetitive and error-prone. A command framework could help with this, or you could make a small helper:
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
function handleCommands(client, commands) {
|
||||||
|
client.on("ready", () => {
|
||||||
|
for (const { name, description } of commands) {
|
||||||
|
client.application?.commands.create({ name, description })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
client.on("interactionCreate", (interaction) => {
|
||||||
|
if (interaction.isCommand()) {
|
||||||
|
for (const command of commands) {
|
||||||
|
if (interaction.commandName === command.name) {
|
||||||
|
command.run(interaction)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
handleCommands(client, [
|
||||||
|
{
|
||||||
|
name: "pong",
|
||||||
|
description: "pong!",
|
||||||
|
run: (interaction) => {
|
||||||
|
reacord.reply(interaction, <>pong</>)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "hi",
|
||||||
|
description: "say hi",
|
||||||
|
run: (interaction) => {
|
||||||
|
reacord.reply(interaction, <>hi</>)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
## Ephemeral Command Replies
|
||||||
|
|
||||||
|
Ephemeral replies are replies that only appear for one user. To create them, use the `.ephemeralReply()` function.
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
handleCommands(client, [
|
||||||
|
{
|
||||||
|
name: "pong",
|
||||||
|
description: "pong, but in secret",
|
||||||
|
run: (interaction) => {
|
||||||
|
reacord.ephemeralReply(interaction, <>(pong)</>)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
The `ephemeralReply` function also returns an instance, but ephemeral replies cannot be updated via `instance.render()`. You can `.deactivate()` them, but `.destroy()` will not delete the message; only the user can hide it from view.
|
||||||
|
|||||||
Reference in New Issue
Block a user