Skip to content

armgit / mem0

Public Imported from GitHub
0

The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.

add: support for discord bot (#412)

Sahil Kumar Yadav0b72269e1876
diff --git a/docs/examples/discord_bot.mdx b/docs/examples/discord_bot.mdx
new file mode 100644
index 000000000..5c1e348da
--- /dev/null
+++ b/docs/examples/discord_bot.mdx
@@ -0,0 +1,48 @@
+---
+title: 'πŸ€– Discord Bot'
+---
+
+### πŸ”‘ Keys Setup
+
+- Set your `OPENAI_API_KEY` in your variables.env file.
+- Go to [https://discord.com/developers/applications/](https://discord.com/developers/applications/) and click on `New Application`.
+- Enter the name for your bot, accept the terms and click on `Create`. On the resulting page, enter the details of your bot as you like.
+- On the left sidebar, click on `Bot`. Under the heading `Privileged Gateway Intents`, toggle all 3 options to ON position. Save your changes.
+- Now click on `Reset Token` and copy the token value. Set it as `DISCORD_BOT_TOKEN` in variables.env file.
+- On the left sidebar, click on `OAuth2` and go to `General`.
+- Set `Authorization Method` to `In-app Authorization`. Under `Scopes` select `bot`.
+- Under `Bot Permissions` allow the following and then click on `Save Changes`.
+```text
+Read Messages/View Channel
+Send Messages
+Read Message History
+Mention everyone
+```
+- Now under `OAuth2` and go to `URL Generator`. Under `Scopes` select `bot`.
+- Under `Bot Permissions` set the same permissions as above.
+- Now scroll down and copy the `Generated URL`. Paste it in a browser window and select the Server where you want to add the bot.
+- Click on `Continue` and authorize the bot.
+- πŸŽ‰ The bot has been successfully added to your server.
+
+### 🐳 Docker Setup
+
+- To setup your discord bot using docker, run the following command inside this folder using your terminal.
+```bash
+docker-compose up --build
+```
+πŸ“ Note: The build command might take a while to install all the packages depending on your system resources.
+
+### πŸš€ Usage Instructions
+
+- Go to the server where you have added your bot.
+- You can add data sources to the bot using the command:
+```text
+/ec add <data_type> <url_or_text>
+```
+- You can ask your queries from the bot using the command:
+```text
+/ec query <question>
+```
+πŸ“ Note: To use the bot privately, you can message the bot directly by right clicking the bot and selecting `Message`.
+
+πŸŽ‰ Happy Chatting! πŸŽ‰
diff --git a/docs/mint.json b/docs/mint.json
index 81689a71a..98ccf7452 100644
--- a/docs/mint.json
+++ b/docs/mint.json
@@ -36,7 +36,7 @@
},
{
"group": "Examples",
- "pages": ["examples/full_stack", "examples/api_server"]
+ "pages": ["examples/full_stack", "examples/api_server", "examples/discord_bot"]
},
{
"group": "Contribution Guidelines",
diff --git a/examples/discord_bot/.dockerignore b/examples/discord_bot/.dockerignore
new file mode 100644
index 000000000..1dce42e87
--- /dev/null
+++ b/examples/discord_bot/.dockerignore
@@ -0,0 +1,8 @@
+__pycache__/
+database
+db
+pyenv
+venv
+.env
+.git
+trash_files/
diff --git a/examples/discord_bot/.gitignore b/examples/discord_bot/.gitignore
new file mode 100644
index 000000000..ba288ed39
--- /dev/null
+++ b/examples/discord_bot/.gitignore
@@ -0,0 +1,7 @@
+__pycache__
+db
+database
+pyenv
+venv
+.env
+trash_files/
diff --git a/examples/discord_bot/Dockerfile b/examples/discord_bot/Dockerfile
new file mode 100644
index 000000000..2907c2312
--- /dev/null
+++ b/examples/discord_bot/Dockerfile
@@ -0,0 +1,9 @@
+FROM python:3.11 AS backend
+
+WORKDIR /usr/src/discord_bot
+COPY requirements.txt .
+RUN pip install -r requirements.txt
+
+COPY . .
+
+CMD ["python", "discord_bot.py"]
diff --git a/examples/discord_bot/discord_bot.py b/examples/discord_bot/discord_bot.py
new file mode 100644
index 000000000..c07868aac
--- /dev/null
+++ b/examples/discord_bot/discord_bot.py
@@ -0,0 +1,65 @@
+import os
+
+import discord
+from discord.ext import commands
+from dotenv import load_dotenv
+
+from embedchain import App
+
+load_dotenv()
+intents = discord.Intents.default()
+intents.message_content = True
+
+bot = commands.Bot(command_prefix="/ec ", intents=intents)
+root_folder = os.getcwd()
+
+
+def initialize_chat_bot():
+ global chat_bot
+ chat_bot = App()
+
+
+async def on_ready():
+ print(f"Logged in as {bot.user.name}")
+ initialize_chat_bot()
+
+
+async def on_command_error(ctx, error):
+ if isinstance(error, commands.CommandNotFound):
+ await send_response(ctx, "Invalid command. Please refer to the documentation for correct syntax.")
+ else:
+ print("Error occurred during command execution:", error)
+
+
+async def add(ctx, data_type: str, *, url_or_text: str):
+ print(f"User: {ctx.author.name}, Data Type: {data_type}, URL/Text: {url_or_text}")
+ try:
+ chat_bot.add(data_type, url_or_text)
+ await send_response(ctx, f"Added {data_type} : {url_or_text}")
+ except Exception as e:
+ await send_response(ctx, f"Failed to add {data_type} : {url_or_text}")
+ print("Error occurred during 'add' command:", e)
+
+
+async def query(ctx, *, question: str):
+ print(f"User: {ctx.author.name}, Query: {question}")
+ try:
+ response = chat_bot.chat(question)
+ await send_response(ctx, response)
+ except Exception as e:
+ await send_response(ctx, "An error occurred. Please try again!")
+ print("Error occurred during 'query' command:", e)
+
+
+async def send_response(ctx, message):
+ if ctx.guild is None:
+ await ctx.send(message)
+ else:
+ await ctx.reply(message)
+
+
+bot.run(os.environ["DISCORD_BOT_TOKEN"])
diff --git a/examples/discord_bot/docker-compose.yml b/examples/discord_bot/docker-compose.yml
new file mode 100644
index 000000000..69baff0d8
--- /dev/null
+++ b/examples/discord_bot/docker-compose.yml
@@ -0,0 +1,11 @@
+version: "3.9"
+
+services:
+ backend:
+ container_name: embedchain_discord_bot
+ restart: unless-stopped
+ build:
+ context: .
+ dockerfile: Dockerfile
+ env_file:
+ - variables.env
\ No newline at end of file
diff --git a/examples/discord_bot/requirements.txt b/examples/discord_bot/requirements.txt
new file mode 100644
index 000000000..9cad4b45d
--- /dev/null
+++ b/examples/discord_bot/requirements.txt
@@ -0,0 +1,3 @@
+discord==2.3.1
+embedchain==0.0.30
+python-dotenv==1.0.0
\ No newline at end of file
diff --git a/examples/discord_bot/variables.env b/examples/discord_bot/variables.env
new file mode 100644
index 000000000..7f3bd8975
--- /dev/null
+++ b/examples/discord_bot/variables.env
@@ -0,0 +1,2 @@
+OPENAI_API_KEY=""
+DISCORD_BOT_TOKEN=""
\ No newline at end of file