Building and deploying a ChatGPT Discord bot with Bun.



Discord bots have become an integral part of the gaming and community experience on the popular messaging platform. With the power of Bun and Discord.js, creating and deploying your own bot is a straightforward process. In this guide, we'll walk through the steps to set up a basic Discord bot using Bun and then enhance it by integrating OpenAI's ChatGPT.

Setting Up the Project with Bun

To get started, let's create a new directory for our bot and initialize it with Bun:

mkdir my-bot
cd my-bot
bun init

Now, install Discord.js and OpenAI:

bun add discord.js openai

Before proceeding, visit the Discord Developer Portal, log in or sign up, create a new application, and add a bot to it. Make sure to follow the official guide for step-by-step instructions.

Once you've created your bot in the Discord Developer Portal, you'll obtain a private key. Add this key to a file named .env.local in the following format:

DISCORD_TOKEN="<your bot's private key>"

Remember to add .env.local to your .gitignore to avoid exposing your bot's private key:


Now, let's write the basic structure of our bot in a file named bot.ts:

// import discord.js
import { Client, Events, GatewayIntentBits } from "discord.js";

// create a new Client instance
const client = new Client({
  intents: [

// listen for the client to be ready
client.once(Events.ClientReady, (c) => {
  console.log(`Ready! Logged in as ${c.user.tag}`);

// login with the token from .env.local

Run your bot using:

bun run bot.ts

Enhancing the Bot with OpenAI's ChatGPT

First, retrieve your OpenAI API key from the OpenAI Dashboard. Add this key to your .env.local file in the following format:

OPENAI_API_KEY="<your OpenAI API key>"

Now, let's take your Discord bot to the next level by integrating OpenAI's ChatGPT. Replace the content of bot.ts with the following code:

// import discord.js and OpenAI
import { Client, Events, GatewayIntentBits } from "discord.js";
import OpenAI from "openai";

// create instances of Discord.js and OpenAI
const discordClient = new Client({
  intents: [

const openai = new OpenAI();

discordClient.once(Events.ClientReady, (c) => {
  console.log(`Ready! Logged in as ${c.user.tag}`);

discordClient.on(Events.MessageCreate, async (message) => {
  if (!message.content.toLowerCase().startsWith("hi gilbert")) {

  await message.channel.sendTyping();

  const chatCompletion = await openai.chat.completions.create({
    messages: [{ role: "user", content: message.content }],
    model: "gpt-3.5-turbo",

  await message.channel.send(chatCompletion.choices[0].message.content!);


This code enhances your bot to respond with a ChatGPT-generated message when a user sends a message starting with "hi gilbert."

Deploying the Bot with Valyent

To deploy your bot, you can use Valyent. Follow these steps:

  1. Install Valyent CLI:
curl -L https://cli.valyent.dev/install.sh | sh
  1. Initialize Valyent:
citadel init
  1. Dockerize your bot, by adding a Dockerfile to the root of your project:
# Dockerfile
FROM oven/bun:1
COPY . .
RUN bun install

CMD ["bun", "bot.ts"]
  1. Deploy your bot:
citadel deploy

These commands will set up and deploy your Discord bot, making it accessible to users on the Discord platform.

Congratulations! You've successfully built and deployed a Discord bot with Bun, integrated OpenAI's ChatGPT, and deployed it using Valyent. Feel free to customize and expand the functionality of your bot further to create a unique and engaging experience for your Discord community.