Skip to content

Commit

Permalink
Merge pull request #1 from Hexastack/initial-commit
Browse files Browse the repository at this point in the history
Feat: Initial Commit
  • Loading branch information
marrouchi authored Dec 19, 2024
2 parents 396c47d + b7a8254 commit 8782345
Show file tree
Hide file tree
Showing 15 changed files with 1,301 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
164 changes: 162 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,162 @@
# hexabot-channel-discord
The Discord Channel Extension for Hexabot Chatbot / Agent Builder
# Hexabot Discord Channel Extension

Welcome to the [Hexabot](https://hexabot.ai/) Discord Channel Extension! This extension enables seamless integration of your Hexabot chatbot with Discord, allowing you to engage with your audience on this popular communication platform.

Not yet familiar with [Hexabot](https://hexabot.ai/)? It's an open-source chatbot / agent solution that allows users to create and manage AI-powered, multi-channel, and multilingual chatbots with ease. If you would like to learn more, please visit the [official GitHub repo](https://github.com/Hexastack/Hexabot/).

## Features

- **Dual Interaction Channels**:
- **Direct Messages**: Personal 1-on-1 bot conversations
- **Server Interactions**: Context-aware `/chat` slash command responses
- **User Data Retrieval**: Fetch essential user information such as username, avatar, roles, and server-specific details.
- **Flexible Communication**:
- **Private Conversations**: Seamless direct messaging
- **Server Chat**: Slash command-driven interactions
- **Rich Messaging Features**:
- **Slash Commands**: Interactive `/chat` command for server environments
- **Buttons**: Add interactive elements to guide user actions
- **Embeds**: Create visually appealing message presentations
- **Context Management**: Intelligent response handling in different communication contexts

## Prerequisites

Before you begin, ensure you have:

- A **Discord account**
- Basic knowledge of **APIs** and **web development** (optional but recommended)
- A **server** to host your chatbot
- **HTTPS** enabled on your server
- Cloned Hexabot locally (refer to https://github.com/hexastack/hexabot)

## Installation

Navigate to your Hexabot project directory and install the extension:

```sh
cd ~/projects/my-chatbot/

npm install --save hexabot-channel-discord

hexabot dev
```

## Step 1: Create a Discord Developer Application

1. **Access Discord Developer Portal**:

- Navigate to [Discord Developer Portal](https://discord.com/developers/applications)
- Click **"New Application"**

2. **Configure Application**:

- Name your application
- Accept Developer Terms of Service

3. **Create Bot**:
- Go to **"Bot"** section
- Click **"Add Bot"**
- Customize bot settings and permissions

## Step 2: Generate Bot Token

1. In the **"Bot"** section, find **"Token"**
2. Click **"Copy"** to retrieve your bot token
3. **IMPORTANT**: Keep this token secret

## Step 3: Configure OAuth2 for Server Invitation

1. **Access OAuth2 Section**:

- In Discord Developer Portal, navigate to **"OAuth2"**
- Click on **"URL Generator"**

2. **Select Scopes**:

- Check **"bot"**
- Check **"applications.commands"** for slash command support

3. **Select Bot Permissions**:

- Choose appropriate permissions based on bot functionality:
- Read Messages/View Channels
- Send Messages
- Embed Links
- Attach Files
- Read Message History
- Use Slash Commands

4. **Generate Invitation URL**:

- Scroll down, the **"Generated URL"** will appear
- Copy this URL

5. **Add Bot to Server**:
- Open the generated URL in a web browser
- Select the target server
- Confirm permissions
- Authorize bot installation

💡 **Pro Tip**: Always use the principle of least privilege. Only select permissions your bot absolutely needs.

## Configuration

### Settings

1. **Bot Token**

- **Description**: Your Discord bot's authentication token
- **Mandatory**: Yes
- **How to Obtain**: Discord Developer Portal > Bot section

2. **Client ID**

- **Description**: Your application's unique identifier
- **How to Obtain**: Discord Developer Portal > General Information

3. **Intents Configuration**
- **Description**: Configure which Discord events your bot can access
- **Recommended Intents**:
- Server Members
- Message Content
- Guild Messages

## Usage

Once configured, your Hexabot will be available on Discord with:

- Slash command interactions
- Automated responses
- User data retrieval

### Testing Integration

1. Invite bot to server
2. Verify slash commands work only in servers
3. Verify direct messages work only in private discussions
4. Check user data retrieval

## Contributing

We welcome community contributions!

- Report bugs
- Suggest features
- Submit pull requests

Please review [Contribution Guidelines](./CONTRIBUTING.md)

[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](./CODE_OF_CONDUCT.md)

Join our [Discord](https://discord.gg/rNb9t2MFkG)

## License

Licensed under GNU Affero General Public License v3.0 (AGPLv3) with additional terms:

1. "Hexabot" is a trademark of Hexastack
2. Derivative works must attribute Hexastack and Hexabot

---

_Happy Bot Building!_
104 changes: 104 additions & 0 deletions discord-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

import {
Client,
Events,
GatewayIntentBits,
Partials,
PermissionFlagsBits,
REST,
Routes,
SlashCommandBuilder,
} from 'discord.js';

import { MenuService } from '@/cms/services/menu.service';
import { LoggerService } from '@/logger/logger.service';

export class DiscordBotService {
private client: Client;

private rest: REST;

constructor(
private botToken: string,
private appId: string,
private readonly menuService: MenuService,
private readonly logger: LoggerService,
) {
// Initialize the Discord client
this.client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
partials: [Partials.Channel],
});
}

getClient(): Client {
return this.client;
}

async init() {
// Register slash commands
await this.registerSlashCommands();

// Destroy the client if it's already running
await this.client.destroy();

// Log in to the Discord bot account using the token
await this.client.login(this.botToken);

// Listen for the ready event
this.client.on(Events.ClientReady, () => {
this.logger.log('Discord bot is ready!');
});
}

setBotToken(bot_token: string) {
this.logger.verbose('Bot token updated', 'DiscordBotService');
this.botToken = bot_token;
this.init();
}

setAppId(app_id: string) {
this.logger.verbose('App ID updated', 'DiscordBotService');
this.appId = app_id;
this.init();
}

private async registerSlashCommands(): Promise<void> {
this.rest = new REST({ version: '10' }).setToken(this.botToken);

const chatCommand = new SlashCommandBuilder()
.setName('chat')
.setDescription('Start a conversation with the bot')
.addStringOption((option) =>
option
.setName('message')
.setDescription('Your message to the bot')
.setRequired(true),
)
.setDefaultMemberPermissions(PermissionFlagsBits.SendMessages);

try {
this.logger.log('Started refreshing application (/) commands.');

await this.rest.put(Routes.applicationCommands(this.appId), {
body: [chatCommand],
});

this.logger.log('Successfully registered application (/) commands.');
} catch (error) {
this.logger.error('Error registering slash commands:', error);
}
}
}
1 change: 1 addition & 0 deletions i18n/en/help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions i18n/en/label.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bot_token": "Token",
"app_id": "App ID"
}
3 changes: 3 additions & 0 deletions i18n/en/title.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"discord_channel": "Discord"
}
1 change: 1 addition & 0 deletions i18n/fr/help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions i18n/fr/label.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bot_token": "Token",
"app_id": "ID de l'application"
}
3 changes: 3 additions & 0 deletions i18n/fr/title.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"discord_channel": "Discord"
}
Loading

0 comments on commit 8782345

Please sign in to comment.