> For the complete documentation index, see [llms.txt](https://savvydev06.gitbook.io/quick-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://savvydev06.gitbook.io/quick-docs/slash-commands/embeds/using-a-function-to-respond.md).

# Using a Function to Respond

The better and more organized method of responding to embeds by utilizing an asynchronous function.

{% hint style="success" %}
All the code below goes inside the "Receiving the Event" block.
{% endhint %}

### Call the Function

{% code title="bot.js" %}

```javascript
if (command === 'help') {
    const description = 'description here';
    const embed = new Discord.MessageEmbed()
        .setTitle("Echo!")
        .setThumbnail(interaction.member.user.displayAvatarURL)
        .setDescription(description)
        .setAuthor(interaction.member.user.username);
        
    client.api.interactions(interaction.id, interaction.token).callback.post({
        data: {
            type: 4,
            data: await createAPIMessage(interaction, embed, Discord, client)
        }
    });
}
```

{% endcode %}

### The Function

{% hint style="info" %}
This code goes outside the if statement, inside the "Receiving the Event" block.
{% endhint %}

{% code title="bot.js" %}

```javascript
async function createAPIMessage(interaction, content, D, client) {
    const apiMessage = await D.APIMessage.create(client.channels.resolve(interaction.channel_id), content)
        .resolveData()
        .resolveFiles();
    
    return {
        ...apiMessage.data,
        files: apiMessage.files
    };
}
```

{% endcode %}

## Complete Example

Here's a complete sample for you to simply copy-paste and utilize:

{% code title="bot.js" %}

```javascript
if (command === 'help') {
    // Here you could do anything. In this sample, I reply with an api interaction
    const description = 'description here';
    const embed = new Discord.MessageEmbed()
        .setTitle("Echo!")
        .setThumbnail(interaction.member.user.displayAvatarURL)
        .setDescription(description)
        .setAuthor(interaction.member.user.username);

    client.api.interactions(interaction.id, interaction.token).callback.post({
        data: {
            type: 4,
            data: await createAPIMessage(interaction, embed, Discord, client)
        }
    });
    
    
async function createAPIMessage(interaction, content, D, client) {
    const apiMessage = await D.APIMessage.create(client.channels.resolve(interaction.channel_id), content)
        .resolveData()
        .resolveFiles();

    return {
        ...apiMessage.data,
        files: apiMessage.files
    };
}
```

{% endcode %}
