Discord Diffusion: Bot for image generation via Stable Diffusion

Discord Diffusion is a fully customizable and easy-to-install Discord bot that
brings image generation via Stable Diffusion right to your Discord server. It
responds to @Bot your image prompt
to generate an image. Drive engagement,
have fun with your community members, and see what you can create!

Discord Diffusion is fully open source and leverages Autocode's API integration
and serverless hosting platform to make it really easy to customize your bot.
When installing, you'll be asked to link both your Discord bot credentials and
your Stability AI credentials. Note that you'll need to enable Privileged Intents
on your bot to allow it to respond to mentions. Once properly deployed,
your bot is editable via Autocode's web editor.
Getting Started
To install Discord Diffusion to your Discord server, click on the Install
button above on this page. If you don't have an Autocode account yet you'll be
asked to create one and walked through Autocode onboarding. Next, you'll be asked
to link your Discord and Stability AI API credentials. Once those accounts are linked
it will take only a few seconds to deploy your bot!
My bot isn't responding to mentions, help?
You'll need to enable Privileged Intents,
specifically the Message Content intent to get your bot to work out-of-the-box.
Once enabled, your bot will respond to mentions.
How does this work?
The bot makes use of Autocode's built-in Discord event routing to route
bot_mention
events to the file functions/events/discord/bot_mention.js
.
This file first parses necessary information from the Discord event:
let event = context.params.event;
let mentions = event.mentions;
let botMention = mentions.find(mention => mention.bot);
let content = event.content;
let author = event.author;
let message = content.replace(/<@(\d+)>/gi, ($0, $1) => {
let mention = mentions.find(mention => mention.id === $1);
if (mention) {
return `<@${mention.username}>`;
} else {
return `<@:unknown>`;
}
});
message = message.trim();
let botName = `<@${botMention.username}>`;
let prompt = message;
// Get rid of the bot name if it's at the start of the message
if (message.startsWith(botName)) {
prompt = message.slice(botName.length).trim();
}
Then sends a placeholder message to Discord using the
Discord API on Autocode...
let messageResponse = await lib.discord.channels['@0.3.3'].messages.create({
channel_id: context.params.event.channel_id,
content: `Generating **${prompt}**, give me a moment...`,
tts: false,
message_reference: {
message_id: context.params.event.id,
fail_if_not_exists: false
}
});
Then speaks to Stability AI's API
to ask it to generate an image using some default parameters and the prompt the
user entered...
let imageResult = await lib.stabilityai.api['@0.1.2'].generation.txt2img({
model: 'stable-diffusion-v1-5',
prompts: [
{
'text': prompt,
'weight': 1
}
],
images: 1,
steps: 30,
cfg: 7.5,
width: 512,
height: 512,
sampler: 'AUTO',
guidance: true
});
And finally, updates the Discord message with the attached image once generated.
let editMessageResponse = await lib.discord.channels['@0.3.3'].messages.update({
message_id: messageResponse.id,
channel_id: context.params.event.channel_id,
content: `Here's your image of **${prompt}**!`,
attachments: [{
file: imageResult.artifacts[0].image,
filename: `${filename}.png`,
description: prompt
}]
});
How can I improve the bot?
Improving images
Playing around with different prompts and models via the
Stability AI API is the best place to
get started. We'll make sure we work on preparing more prompt guidance content
in the future!
Improving bot functionality
Autocode is specialized at connecting different APIs together. We encourage you
to play with the OpenAI API to generate
completions that can then be used as prompts. You can also try connecting the
Stability AI API to other apps -- Twitter, Slack, Email. Have fun!
How can I get more help?
Your best bet for help is to visit Autocode's official Discord server at discord.gg/autocode.
You can also e-mail Autocode directly at contact@autocode.com.
To see what other people in the Stable Diffusion community are building, join
Stable Diffusion on Discord at discord.gg/stablediffusion.
Happy hacking!