Build and deploy a Stable Diffusion XL Discord bot with Replicate and Valyent




Replicate is a platform that allows to run and fine-tune open-source models. In this tutorial, we will use Replicate to run Stable Diffusion XL generations.

Valyent allows you to deploy applications from a Dockerfile. In this tutorial, we will use Valyent to deploy a Discord bot that will generate Stable Diffusion XL generations.


Setting up the Discord bot

  1. Log in to the Discord Developer Portal and create a new application. You can name it whatever you want.

Discord bot Discord bot

  1. Go to the "Bot" tab, and turn on the "Message Content Intent" option.

Discord bot

In this section, you can also click on "Reset token" to generate a new token for your bot.

Discord bot

  1. Now, let's invite the bot, so we can test it. Go to the "OAuth2" tab, and the "URL Generator" tab. Select the "bot" scope, and the "Send Messages" permission. Then, copy the generated URL and paste it in your browser. You will be asked to select a server to invite the bot to. Select a server, and click on "Authorize".

Developing the Bot

Initializing the workspace

First, we need to create a new directory for our bot:

mkdir gilbert-bot
cd gilbert-bot

Then, we are going to create a new Python project using Poetry:

# Install Poetry (if you don't have it already)
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
# Initialize a new Python project
poetry init -n
# Install the dependencies
poetry add discord.py python-dotenv replicate

Let's create new file called .env in the root of the project. For your Discord token, grab it from the Discord Developer Portal. You also need to get a Replicate API token from the Replicate website. You can find your API token in the settings page.

touch .env

Open the .env file and add the following line:


Do not forget to add this .env file to your .gitignore file.

Alright, we are ready to start coding!

Let's create a new file called bot.py in the root of the project. This file will contain the code for our bot.

touch bot.py

Open the bot.py file and add the following code:

from discord import Intents
from discord.ext import commands
from dotenv import load_dotenv
import os
import replicate

# We load the environment variables

# We set up the intents to allow the bot to read messages
intents = Intents.default()
intents.message_content = True

# We initialize the bot
bot = commands.Bot(

async def imagine(ctx, *, prompt):
    # We send a message to let the user know we're working on it.
    msg = await ctx.send(f"“{prompt}”\n> Dreaming up an image...")

    # We use the Stable Diffusion XL model to generate images.
    output = replicate.run(
            "prompt": prompt,
            # Customize the following parameters to your liking
            # https://replicate.com/stability-ai/sdxl?input=python
    image = output[0]

    # We edit the message to show the generated image.
    await msg.edit(content=f"“{prompt}”\n{image}")

# Run the bot with the Discord token from the .env file

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. Authenticate with Valyent:
citadel auth login
  1. Initialize Valyent:
citadel init
  1. Dockerize your bot:
FROM python:3.11

RUN pip install poetry

WORKDIR /project

COPY poetry.lock pyproject.toml /project/

RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi

COPY . /project

CMD python bot.py
  1. Deploy your bot:
citadel deploy

Congratulations! You have successfully deployed your Discord bot. To test it, you can send a message to your bot in Discord.

Discord bot