Par. GPT AI Team

How to use ChatGPT-4 in Discord?

In recent months, the popularity of artificial intelligence and chatbots soared to unprecedented levels, with users exploring innovative ways to harness the power of various AI models for gaming, business, and even casual conversations. Among these bots, ChatGPT-4 is a favorite for its robust capabilities. If you have ever wanted to chat with a powerful AI model while socializing on Discord, then you’re in the right place! In this guide, we will break down the process of using ChatGPT-4 on Discord step by step, making it as easy as pie. So, buckle up as we dive into the world of AI and Discord!

The Breakdown: Getting ChatGPT-4 Up and Running in Your Discord Server

Step 1: Create Your Server on Discord

Before we dive into the nitty-gritty of building a bot, let’s create a Discord server. It’s effortless!

  • Log in to your Discord account. If you don’t have one, it takes mere minutes to set up.
  • On the left sidebar, you’ll see a plus (+) symbol. Click that to add a server.
  • Select “Create my own.”
  • Now, you can customize it a bit: choose “For me and my friends” if you want a more private vibe.
  • Give your server an awesome name and upload an eye-catching image that represents the theme of your new community.

Step 2: Build a Bot

Once you have a server up and running, the next step is creating your bot that will host the ChatGPT-4 functionality.

  • Navigate to the Discord Developer Portal. This is where all the magic happens!
  • Click on “Applications” and then “Add New Application.”
  • Head to General Information, give your App a snazzy name, and add an app icon. You want something that yells, “I’m a cool bot!”
  • Make sure to hit “Save Changes” afterwards.
  • Select “Bot” from the left pane. Hit the button to add a bot and then click “Yes, do it!” Enjoy your shiny new bot!

Step 3: Generate a URL for Bot Permissions

Now that the bot is created, we need to grant it permissions to interact with your Discord server.

  • Go to the “OAuth2” section on the left side menu and click on “URL Generator.”
  • Tick the box for “bot” under scopes. This will allow your bot to perform actions.
  • Scroll down to “Bot Permissions” and select preferences like “Read Messages/View Channels.” Other useful permissions include “Send Messages” and “Attach Files.”
  • At the bottom, you’ll find the generated URL. Copy it for later.
  • Return to your Bot Settings and click “Reset Token.” Make sure to save that token securely!

Step 4: Authorize the Bot

Using the generated URL, let’s add the bot to your server.

  • Open a web browser and paste the URL you copied earlier.
  • Select your Discord server from the dropdown and click “Continue.”
  • Finish by verifying that you are, in fact, a human (unless you’re a robot pretending to be one!).

Step 5: Setting Up Files and Folders in Visual Studio Code

Alright, the bot is now part of your server! Next, we need a good development environment.

  • Use Visual Studio Code. Navigate to the left pane and click on the “Folder” icon to create a new folder.
  • Create another folder named “Discord_ChatGPT_Bot” and navigate into it. This is where the fun begins!

Step 6: Create a Virtual Environment (venv)

We want our bot to run smoothly without conflicting packages. It’s time to create a virtual environment.

  • Open up the terminal in Visual Studio Code and type:
  • py -m venv env
  • Now, activate the environment. For Windows, use:
  • env\Scripts\activate
  • If you’re on macOS or Linux, type:
  • source env/bin/activate
  • You should see (venv) before your command prompt to indicate that the environment is activated.

Step 7: Use Command Prompt for Installation

Now it’s time to install the necessary Python packages to make your bot run. This is super straightforward.

  • While in your terminal, run these commands:
  • pip install -U discord.py
  • pip install openai
  • pip install python-dotenv
  • Your environment is now supercharged with the libraries needed for Discord and OpenAI API!

Step 8: Write the .env File

The next step ensures that sensitive information like tokens remains secure.

  • Create a file named “.env” in your project folder.
  • Open it and type:
  • Discord_Token =
  • OpenAI_key =
  • This will help you keep your tokens safe while connecting to the Discord and OpenAI API.

Step 9: Get your OpenAI Key

To use ChatGPT-4, you’ll also need an OpenAI API key.

  • Go to the OpenAI website, log in, and navigate to your personal account settings.
  • Click on “View API keys.”
  • Generate a new secret key and keep it safe!

Step 10: Check the Model

To ensure optimal performance, check out the models available in the OpenAI documentation.

  • Under “Get Started,” click “Models.”
  • Select “GPT-4” for maximum performance, but keep in mind that it costs over 30 times more than the GPT-3.5 Turbo model for the same token count.
  • Copy the name of the GPT-4 model you’ll be using later in your code.

Step 11: Write Code in the discord.py File

With all the setups in place, it’s finally time to write some code to bring your bot to life.

  • Create a file named “discord.py” in your project directory.
  • Copy the following code:

import os import discord from dotenv import load_dotenv import openai load_dotenv() token = os.getenv(‘Discord_Token’) openai_key = os.getenv(‘OpenAI_key’) # Connect to OpenAI openai.api_key = openai_key intents = discord.Intents.all() client = discord.Client(command_prefix=’!’, intents=intents) @client.event async def on_ready(): print(f'{client.user} has connected to Discord!’) @client.event async def on_message(message): if message.author == client.user: return # Check if the bot is mentioned in the message if client.user.mentioned_in(message): response = openai.Completion.create( engine=’gpt-4′, prompt=f'{message.content}’, temperature=0.5, max_tokens=2048, ) await message.channel.send(response.choices[0].text) client.run(token)

  • Save your changes! This code will get your bot to respond to messages where it’s mentioned using the ChatGPT-4 model.
  • Step 12: Run the Script!

    Time to see if it all works. In your terminal, type:

    python discord.py

    Your bot should now be online. You’ll see a confirmation message if everything went smoothly!

    Step 13: Chat with Your ChatBot

    That’s it! The moment of joy has arrived. You can now chat with your ChatGPT-4 fueled bot.

    • Navigate to your Discord channel. You should see your ChatBot online.
    • Type a question, such as “How can I use AI for improving my coding skills?”
    • Watch the magic unfold as ChatGPT responds in real-time!

    Step 14: Why Do We Need to Use Replit?

    If you want your bot to run even when your device is turned off or you’re away, you’ll need to use Replit to keep it alive.

    Using Replit allows the bot to remain operational in the cloud, ensuring you’ll continue to receive responses even when your computer is off. You can think of it as setting up a mini-server that runs independently!

    Step 15: Creating a Repl and Installing Your Discord Bot

    To start with Replit:

    • Create a new Replit.
    • Select “Python” as your template.
    • Give it a name like “ChatGPT_Bot” and hit “Create Repl.”

    Step 16: Setting Up Authorization for Your Bot

    • Scroll down and look for the “Secrets” button.
    • Type “Discord_Token” in the Key field, and copy your API Key from Visual Studio and paste it into the Value field.
    • Hit “Add New Secret” to save this securely.

    Step 17: Create a Keep Alive File

    Now, let’s create a keep-alive script to ensure your bot stays operational.

    • Create a new file and name it “keep_alive.py.”
    • Copy the following code:

    from flask import Flask from threading import Thread app = Flask( ») @app.route(‘/’) def home(): return « I’m alive » def run(): app.run(host=’0.0.0.0′, port=8080) def keep_alive(): t = Thread(target=run) t.start()

  • This code snippet utilizes Flask to keep your bot alive and kicking!
  • Save your changes!
  • Step 18: Write Code in the main.py File

    Now we need to update the main.py file.

    • Navigate to the “main.py” file.
    • Paste the following code:

    import os import discord import openai from keep_alive import keep_alive token = os.getenv(‘Discord_Token’) openai_key = os.getenv(‘OpenAI_key’) # Connect to OpenAI openai.api_key = openai_key intents = discord.Intents.all() client = discord.Client(command_prefix=’!’, intents=intents) @client.event async def on_ready(): print(f'{client.user} has connected to Discord!’) @client.event async def on_message(message): if message.author == client.user: return # Check if the bot is mentioned in the message if client.user.mentioned_in(message): response = openai.Completion.create( engine=’gpt-4′, prompt=f'{message.content}’, temperature=0.5, max_tokens=2048, ) await message.channel.send(response.choices[0].text) keep_alive() client.run(token)

  • Hit the save button after pasting the code!
  • Step 19: Click on Run Button

    Finally, click the run button in Replit, and there you have it!

    Step 20: Chat with the ChatBot

    To ensure everything is running smoothly, navigate back to your Discord channel, where your bot resides. Type a message, and enjoy having a one-on-one conversation with your very own ChatGPT-4 bot!

    Conclusion: Your Gateway to AI-Powered Conversations

    There you have it! You’ve successfully set up ChatGPT-4 on your very own Discord server. With all the steps laid out, you’re now equipped to harness the power of AI right at your fingertips. Whether for fun banter, coding advice, or philosophical questions, your bot is ready to engage. Don’t hesitate to explore new features and capabilities as your familiarity with Discord and ChatGPT grows. The sky’s the limit.

    If you found this guide helpful, consider subscribing or following reputable AI channels. Embrace the future and connect with others who share your enthusiasm for AI. Happy chatting!

    Laisser un commentaire