Welcome to the Dify-Discord-Starter project! This is a modern and easy-to-use starter template for creating a Discord bot that integrates with the Dify app. Use this template to build your own bot and enable interactive chatting capabilities within your Discord community.
Demo made with the Example Dify App
/chat
command directly in Discord.Before you begin, ensure you have the following installed:
Clone the Repository
git clone https://github.com/your-username/dify-discord-starter.git
cd dify-discord-starter
Environment Variables
.env-example
to .env
.DIFY_API_KEY="app..." # Your Dify API secret key
DIFY_API_BASE_URL="https://api.dify.ai/v1" # Your Dify instance base URL
DISCORD_BOT_TOKEN="" # Your Discord bot token from Discord Developer Portal
Install Dependencies
npm install
Build the Project
npm run build
Start the Bot
npm start
Upon startup, the bot will log a message in the console with a link to add the bot to your Discord server.
Add Your Bot to a Discord Server Use the provided link in the console to add your bot to a Discord server.
Install Slash Commands
npx ts-node scripts/install.ts <server-id>
Replace <server-id>
with the ID of the server where you want to install the command.
Once the bot is added to your server and the slash command is installed, you can interact with it using the /chat
command. Simply type /chat
followed by your message, and the bot will respond with an ephemeral reply from the Dify app.
npm run build
: Compiles the TypeScript code to JavaScript, preparing it for execution.npm start
: Starts the bot using the compiled JavaScript code.npm run dev
: Runs the bot in development mode with hot reloading, ideal for development purposes.npm run install-cmd
: A shortcut script to run the install command script.By default the discord bot will pass the name of the user to the assistant, within the dify variable username
and the current date as UTC string within the dify variable now
.
You can set the environment variable HISTORY_MODE
to enable chat history. Currently the history is stored in memory, if you restart the bot, it will forget the history.
Be aware that currently, there is no mechanic to clear or summarize the history. This means, that if users send too many messages, you might reach the token limit of the assistant.
If you do not set this variable, the bot will not remember messages - the bot will even forget the last message the bot has sent to the channel.
Set it to user
if you want the bot to enable an own chat history for every user. The assistant will remember every message of the user accross channels and servers.
Set it to channel
if you want the bot to enable a history for channels. The assistant will remember every message in the channe regardless from which user it came. You should use the dify variable "username" to allow the bot recognize the author of messages, otherwise the assistant will think all messages come from the same user.
Hint: If you use this, the userId of messages will no longer be the user but the server id, since dify does store conversations per user - if you share conversations accross users, you cannot pass the userid since dify would still create a unique conversation per user.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Distributed under the MIT License. See LICENSE
for more information.
Happy Coding! 🎉👩💻👨💻
Note: This is a starter project and is not affiliated with the official Dify platform or Discord.