Admin x360Labs Mod Xeon Posted May 19 Group: Admin Content Count: 9 Reputation: 14 Solved Content: 0 Days Won: 4 Admin Post Share Posted May 19 Hello, similar to my other post, this is a node.js discord bot. This one uses OpenAI API to provide AI image generation with a simple text command and prompt. Requirements - Node.js & Discord.js - A PC/Server to run the bot on - The ability to create discord applications in the developer portal, set intents, and configure bot and server permissions. Windows Installation: Step 1: Install discord.js & Axios to your project directory by running the following commands in your terminal. npm init -y npm install discord.js axios dotenv Step 2: Create a new text file and paste the following code into it while making sure to replace the placeholder tokens with your real ones, then save it and rename the file to ".env" (IMPORTANT!) DISCORD_BOT_TOKEN=Your_bot_token OPENAI_API_KEY=your_OpenAI_API_key Step 3: Copy & paste the code below into a file called "index.js" with your text editor of choice, then save it. const { Client, GatewayIntentBits } = require('discord.js'); const axios = require('axios'); require('dotenv').config(); // Ensure this is at the top // Logging to verify environment variables console.log('Discord Bot Token:', process.env.DISCORD_BOT_TOKEN); console.log('OpenAI API Key:', process.env.OPENAI_API_KEY); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); client.once('ready', () => { console.log(`Logged in as ${client.user.tag}!`); }); client.on('messageCreate', async message => { if (message.author.bot) return; if (message.content.startsWith('!generate ')) { const prompt = message.content.replace('!generate ', ''); try { const response = await axios.post( 'https://api.openai.com/v1/images/generations', { prompt: prompt, n: 1, size: "1024x1024" }, { headers: { 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`, 'Content-Type': 'application/json' } } ); const imageUrl = response.data.data[0].url; message.channel.send(`Here is your generated image: ${imageUrl}`); } catch (error) { console.error('Error generating image:', error.response ? error.response.data : error.message); message.channel.send('There was an error generating the image. Please try again.'); } } }); client.login(process.env.DISCORD_BOT_TOKEN); Step 4: Run your bot by typing "node index.js" into your terminal while in the project content directory Note OpenAI API keys are free but their usage is NOT. They require you to spend $5 USD minimum with them before the keys you generate will work. Trials do not count either, the bot will return an error if you do not have enough funds in your OpenAI account to generate a usage request. You can read more about this in OpenAI's API documentation. Screenshots 1 Quote Link to comment Share on other sites More sharing options...
Support x360Labs Mod Zac Posted May 19 Group: Support Content Count: 35 Reputation: 32 Solved Content: 0 Days Won: 7 Share Posted May 19 thanks for more discord bot loot to add to my folder! i enjoy looking through code. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.