Sign In

SD Webui - Post generations to Discord

0

SD Webui - Post generations to Discord

I regularly use Runpod to generate images. When generating a lot of images it gets somewhat cumbersome to download each file. So I decided to create a SD Webui extension that automatically posts generated images to my Discord.

Disclaimer: I am not a Python programmer and immediately admit that I used AI to generate this code. I therefore do not claim any ownership over this code.

I post this code 'as is' for you to do with what you wish. I do not deliver support or will help you install this extension.

⚙️How it works

  1. Open the SD Webui.

  2. Navigate to Settings Discord Uploader.

  3. Enter your Discord Webhook URL.

  4. Click Apply settings.

💡 How to get a Discord Webhook:

  • Go to Discord → Server Settings → Integrations → Webhooks.

  • Create a new webhook and copy the Webhook URL.

⌨️ Creating the extension

  • Navigate to your stable-diffusion-webui folder. Find and open the extensions folder.

  • Create a folder called sd-webui-discord.

  • Within the sd-webui-discord folder, create a folder called scripts.

  • Now create a file called discord_uploader.py and paste in the following code.

import os
import requests
import gradio as gr
from modules import script_callbacks, shared

# Register the webhook URL setting in WebUI
def on_ui_settings():
    """ Adds Discord Webhook URL input field to Stable Diffusion WebUI settings. """
    section = ("discord_uploader", "Discord Uploader")
    shared.opts.add_option("discord_webhook_url", shared.OptionInfo("", "Discord Webhook URL", section=section))

# Function to send images to Discord
def send_to_discord(image_path):
    webhook_url = shared.opts.data.get("discord_webhook_url", "").strip()

    if not webhook_url:
        print("⚠️ No Discord Webhook URL set! Please configure it in the WebUI settings.")
        return

    if not os.path.exists(image_path):
        print(f"⚠️ File not found: {image_path}")
        return

    with open(image_path, "rb") as file:
        files = {"file": (os.path.basename(image_path), file, "image/png")}
        response = requests.post(webhook_url, files=files)
        
    if response.status_code == 200:
        print("✅ Image successfully sent to Discord!")
    else:
        print(f"❌ Failed to send image. Status code: {response.status_code}, Response: {response.text}")

# Hook into the image generation process
def on_image_saved(params):
    """ Callback function triggered when an image is saved. """
    
    # Print available attributes to debug
    print(f"🔍 Debug: ImageSaveParams attributes: {dir(params)}")
    
    # Identify correct attribute for image path
    if hasattr(params, "filename") and params.filename:
        send_to_discord(params.filename)
    elif hasattr(params, "path") and params.path:
        send_to_discord(params.path)
    else:
        print("⚠️ Could not determine saved image path.")

# Register the callback functions
script_callbacks.on_image_saved(on_image_saved)
script_callbacks.on_ui_settings(on_ui_settings)

0