# Discord Bot Template

## `Build An Interactive Discord Bot`

This guide will walk you through setting up and using the Skillful AI Discord bot integration. Our Discord integration allows you to create AI-powered Discord bots that can interact with users, generate images, and leverage all of Skillful AI's capabilities.

### `Step 1: Prerequisites`

#### <kbd>Discord Bot Setup</kbd>

1. Go to [Discord Developer Portal](https://discord.com/developers/applications)
2. Click "New Application"
3. Navigate to the "Bot" section
4. Click "Add Bot"
5. Enable Privileged Intents:
   * **MESSAGE CONTENT INTENT**
   * **SERVER MEMBERS INTENT**
   * **PRESENCE INTENT**
6. Copy your bot token

#### <kbd>Skillful AI Setup</kbd>

1. [Reach out](https://discord.gg/WhAYqkXYkg) for an API key

### `Step 2: Installation`

Using a code editor like **Visual Studio Code**, we can create a new project with:

```bash
# Create new project directory
mkdir my-skillful-bot
cd my-skillful-bot

# Initialize our project
npm init -y

# Install the Discord bot template
npm install @skillfulai/discord-template
```

### `Step 3: Basic Setup`

Create an `index.js` script and copy the below into it. The basic setup will disable [ProActive Mode](/web-sdk/templates/discord-bot-template/proactive-mode.md) by default, but enable everything else you need to interact with your AI Agent.

```javascript
const SkillfulDiscordBot = require('@skillfulai/discord-template');

// Initialize bot
const bot = new SkillfulDiscordBot({
    discordToken: 'your_discord_token',
    skillfulApiKey: 'your_skillful_api_key'

    // Optional
    agentName: 'Wojak',          // Your Agent Name
    debug: false,                // Default: false
    proactive: false,            // Default: false
    proactiveThreshold: 50       // Default: 50
});

// Start bot
bot.start()
    .then(() => console.log('Bot started'))
    .catch(console.error);

// Handle shutdown
process.on('SIGINT', async () => {
    await bot.stop();
    process.exit(0);
});
```

### `Step 4: Run the Bot`

Run your bot to start the application:

```bash
node index.js
```

### `Basic Usage`

You can interact with your agent in a few different ways:

#### <kbd>Direct Mentions</kbd>

The bot responds to direct mentions:

```
@YourBot Hello there!
```

#### <kbd>Replies</kbd>

The bot will respond to direct replies.

#### <kbd>ProActive Mode</kbd>

If [ProActive Mode](/web-sdk/templates/discord-bot-template/proactive-mode.md) has been enabled in the constructor, your bot will dynamically jump into conversations and reply to users.

### `Debug Mode`

Enable debug logging:

```javascript
const bot = new SkillfulDiscordBot({
    discordToken: 'token',
    skillfulApiKey: 'key',
    debug: true
});
```

**Debug mode shows:**

* Message processing
* Engagement decisions
* Error details
* Connection status

### `Error Handling`

```javascript
bot.start()
    .then(() => console.log('Bot started'))
    .catch(error => {
        console.error('Startup error:', error);
        process.exit(1);
    });
```

### `Best Practices`

1. **Environment Variables**

   ```javascript
   require('dotenv').config();

   const bot = new SkillfulDiscordBot({
       discordToken: process.env.DISCORD_TOKEN,
       skillfulApiKey: process.env.SKILLFUL_API_KEY
   });
   ```
2. **Proper Shutdown**

   ```javascript
   process.on('SIGINT', async () => {
       console.log('Shutting down...');
       await bot.stop();
       process.exit(0);
   });
   ```
3. **Error Monitoring**

   ```javascript
   process.on('unhandledRejection', error => {
       console.error('Unhandled promise rejection:', error);
   });
   ```

### `Troubleshooting`

#### Common Issues

1. **Invalid Token**
   * Verify token in Discord Developer Portal
   * Check for token format issues
   * Ensure intents are enabled
2. **Connection Issues**
   * Check internet connection
   * Verify Discord API status
   * Check for rate limiting
3. **Permission Issues**
   * Verify bot has required permissions
   * Check channel permissions
   * Verify intent settings

For more help, join our [Discord community](https://discord.gg/WhAYqkXYkg).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gaming.skillfulai.io/web-sdk/templates/discord-bot-template.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
