Discord Image Generation AI Bot in Python
Skills:
Image Generation Basics90%
Key Takeaways
The video demonstrates how to create a Discord image generation AI bot in Python using the stable-diffusion-v1-5 model from Hugging Face.
Full Transcript
what is going on guys welcome back in this video today we're going to learn how to easily create an image generation AI Discord bot in Python so let us get right into it [Music] not all right so a quick demonstration and preview up front this is going to be our final result a Discord bot which either reacts to messages that you send to it directly or it also listens to channels to messages and channels uh which it has access to and what we can do now is we can type something like exclamation mark gen and then for example horse and this is going to generate an image for the prompt horse uh takes some time especially because I'm recording now and it's going to result in the image of a horse then I can do something like exclamation mark gen blue horse for example let's see if that works uh and in my case now I'm running stable diffusion 1.5 here so you can also use better models if you have the capacity to do so in this case I get this here okay and then let's do something else like maybe uh man on moon eating lunch let's see if that is successful uh but that is basically the idea you can text it directly or it can react to messages uh yeah okay this is not not very good but yeah you get the idea this is what we're going to build in this video today all right now for this project we're going to need two things first of all a disc Discord bot second of all an image generation model that the bot can use and then we're going to combine everything into a single python script which operates the bot so the first thing we want to do is we want to go to our web browser and visit discord.com developers now once you're logged in you're going to see this page here and here we're going to just click on new application to create our new uh Discord bot I'm going to call this image generation bot I'm going to accept the terms of service create and here now we're going to go to O Off 2 we're going to click down here on bot and we're going to Grant certain permissions for example send messages obviously because we need to send images uh attach files obviously because we need to send images uh and then also read message history view channels and I think embed links can also be useful depending on what we want to do with a bot but these are the permissions that we need so what we're going to do is we're going to copy the link down here this is going to be the invite link for our server uh we're going to open up a new tab I'm going to copy paste this and now I can allow as an admin of the server as an admin of the neural n Community server which you can by the way join as well um I will click on continue and authorize the bot to join the server with the permissions now I need to authenticate myself here with a code so I'm going to do that quickly like this confirm and I'm going to also conf confirm that I'm human there you go invite the bot to the server so the server thebot is now part of the server I can see that this is the case by going to the welcome lounge and you can see that here the image generation bot just slid into the server so I invited the bot and the bot is now part of the server however the bot is offline because uh it's not operated yet I didn't connect to it in order to do that I need to get a token so I'm going to go to the bot page and here um I'm going to scroll up here or I'm going to go to the top and I'm going to go to to reset token so I'm going to do yes do it and I'm going to copy this thing here now you can always reset this but you're only going to be showed this token once so copy it uh go to a file in your python development environment create it call it token or whatever copy paste this here and now you have it and this is what you need to connect to your Bot so um on the bot page we're going to do a second thing we're going to Grant the privilege here uh which is the message content intent the reason we need that is because the bot of course has to be able to read the messages that it sees so I'm going to enable this save changes and uh this now allows the bot to also view message content so this is what we need for the setup here the second thing that we need is we need an image generation model in my case here I'm going to use stable diffusion version 1.5 uh from hugging phas you can use any other version of stable diffusion you can use any other image generation model but you need to have some way to get a prompt feed it to the model and get an image in return this is how you do it with this package you just import from the fusers uh stable diffusion pipeline you also import pytorch you specify the model you create a pipeline which is pre-trained so you don't have to do anything uh of course you're running the mo uh the model locally so you need to have a GPU especially if you want to run run Cuda here um and then you provide a prompt and you get the images result this is what we're going to do here in this case if you have the necessary Hardware to run stable diffusion 3 do it I'm going to run this one here um all right so we're going to open up a new python script and we're going to have to do a couple of installations first first of all we're going to do let me actually see what the Discord package is called I think Discord py oh yeah discord.py so we're going to do pip three install diffusers Discord py and torch which is pi torch in my case all of these are already installed so I'm not going to run this now but these are the packages that we need and then we're going to do some imports first of all we're going to import OS and I'm immediately going to say uh that I want to set the following environment variable TF CPP Min loog level I want to set this to three you don't need to do that I just do it because I get a bunch of warning messages that I don't want to have here uh for this video this is optional you don't need to do that then I want to import uu ID so that I can export uh unique file names I want to import torch I want to import actually from diffusers I want to import the stable diffusion Pipeline and I also want to import the Discord package and what I'm going to do now is I'm going to load the model or I'm going to specify the model from stable diffusion uh or from hugging face I'm going to say the model or actually maybe we can copy paste this from here so I'm going to say uh model and pipe I'm going to copy all of this I'm going to copy paste it here so we have the model ID we get the pipeline from pre-trained which is just uh getting the model from hugging phase I say I want to run this on GPU and then what I do is I create my Discord po so I say the token is equal to opening the file token in reading mode reading the content that is my token which I got from the Discord uh developer page and then I'm going to create intents so the intents is more like uh a scope of permission or what do I want to do with this bot so I'm going to say intents is equal to discord. intense. default and then I'm going to say intense. messages equals true and intense intense. message content is also going to be true and for this line I also need to have the Privileges enabled which I showed you so this is why we need to enable this here um all right so we have these permissions and then I'm going to create a client I'm going to say client is equal to Discord do client intents is going to be equal to intents and then we're going to Define uh the functionality and that's basically going to be all we do we're going to Define two functions one of them is even optional the first one is going to be uh client event it's going to be an asynchronous function on connect this is just optional for me I want to know that when I connect I want to print a message bot connected you don't have to do that if you don't want to and all the work will basically be done in the second function so I'm going to say here as well client event async def on message so when a message is received with a parameter message here I can react to it so I can say Okay depending on the message do something uh the first thing I want to do here is I want to check if the message author is myself so if the message author is equal to client user so to the user of this client then I want to ignore the message so I want to just return because I don't want to go into an endless loop where I react to my own messages um so I want to only react to messages that are not coming from the bot itself um and then I want to say if the message. content starts with and now you can define a command I'm going to use exclamation mark gen and space you can also go with dollar generate or any message you don't even have to check for anything I'm going to go with uh exclamation mark gen if that's the case I want to do the following await and um I'm going to say message do Channel get the channel That the message was sent in so it can be a direct message it can be a channel uh that the bot has access to and in this channel send the following message generating image for prompt now actually let's do double quotations here and then message content but we want to uh to to cut off the first five characters because they are just uh exclamation mark gen and space so we're going to go five colon um and then maybe we want to use three dots there you go um so that is just a message now to actually generate the image we use the code here from hugging face so we just do exactly this uh so what we do is we say file name is going to be equal and I do this in order to be able to generate multiple images uh at the same time or actually I'm not sure no this actually doesn't make sense but we we want to do it like this so we want to do uh uu ID uu ID 4.png just so we don't have overlapping file names in case someone uh start a second second request here um and we want to do image is equal to pipe and the pipe is defined remember up here uh pipe and we feed into the pipe the message content but only after index 5 so to cut off the the command itself um and as a result we get images we want to get the first one here and then we want to do image save to that file name which we generated up here now in order to send a message we want to do with open file name in Reading bytes mode as F we want to say that the picture is equal to Discord file so we create a file based on that file stream and we send that file as a message so we say a wait message Channel send and we send it as a file file equals picture uh all right so what's the problem here no this should actually work uh all right and in the end we want to remove the file from disk so from our server where the whole thing is happening so we want to remove file name and in the end we want to just do client run with a token and the token is the one stored in the file so this already is the whole program let me run this and see if that works so I run this and we should get bot connected here in a second hopefully uh actually it has to download some stuff first and there you go bot connected so I can go to Discord now and I can probably see somewhere here that uh what was it called image generation bot is active so I can actually open up a conversation here and I can say hello I should not get a response for this one but if I say gen let's say Doc I should get generating image for prompt Doc and there you go I get a doc and the same thing should now work also on the server so let me choose a channel that is uh let's actually make a new channel here text Channel experiment I'm going to do a private channel here and I'm only going to allow for the image generation bot to be in it now I'm not sure which one is uh which so I'm going to do both create channel so here now I only have access and the admins have access and here I'm going to say gen cat for example and since the bot has access to this Channel and it would also have access to all the other channels so if by accident someone of my um of my community members here types exclamation mark gen something it's going to be generated in any of these channels because it doesn't matter if I send it or someone else sends it but of course I'm going to turn off the bot here in a second so let's delete the channel but this is basically how you do that this is how you can easily build a Discord image generation bot in Python so that's a for today's video I hope you enjoyed it and I hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Original Description
In this video we learn how to create an image generation AI Discord bot in Python.
Model: https://huggingface.co/runwayml/stable-diffusion-v1-5
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: Image Generation Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
FREE AI Sin City Photo Generator — Turn Any Photo Into High-Contrast Noir Art (2026)
Dev.to AI
Google makes Gemini’s personalized image generation free for all US users
The Next Web AI
Gemini’s personalized AI image generation is now free for U.S. users
TechCrunch AI
WebP's Compression Secret: How a 1MB PNG Becomes a 200KB WebP
Dev.to · swift king
🎓
Tutor Explanation
DeepCamp AI