Attempting a ChatGPT Discord Bot: Part 2 - Wrangling Old Tutorials

·

2 min read

Alright! We're making a Discord bot and we're giving it a brain with ChatGPT. The vision has been cast. What's next? Basic project management skills tell me that I need to figure out a list of tasks. Here are my thoughts:

  1. Figure out the Discord and ChatGPT APIs

  2. Understand how Discord bots are made

  3. Determine how to input text from a Discord channel into ChatGPT

  4. Sanitize the inputs once the pipeline is tested

Me: "Should be pretty straightforward!" Narrator: "It was not".

So, the first thing I did was, of course, see if someone else had already done this and I could steal their code - obviously. Lo and behold, I found a whole Medium article called "Create a ChatGPT Discord Bot" by Ivan Campos, written March of 2023. This is an important detail.

In this article, Campos delineates the process to setup steps 1-3 that I'd outlined above. Great! Copy and paste.

Wrong. After setting up the bot, creating my Node.js project directory in Visual Code Studio, setting up my ChatGPT API details, and making my index.js file, I brought up my terminal and ran the last step: "node index.js". Error.

Just as an aside, to point out just how much ChatGPT has changed my debugging paradigm, I threw the entire error sequence into ChatGPT to see what was up. It was kind of mindless to be honest with you - it spat out updated code and I plugged it back in and got another error before I realized, "Huh, I never actually looked at the code."

Anyways, I waded threw the tutorial again and realized that the OpenAI API versions were different. Campos' tutorial ran OpenAI 3.2.1 and I had installed the latest version, 4.28.0. That's the important part - remember how the article was made in March of '23? Discord.js had been updated. This will be unfortunately the bane of my entire attempt to follow this tutorial, but luckily with some sleuthing and Stack Overflow searching I was able to figure out what needed to be updated.

As you can see from the image above the changes are fairly straightforward - in this case the first error was simply a change in the initialization structure. The newest version of the OpenAI API utilizes "import OpenAI from 'openai';" - a lot cleaner without the need for "Configuration".

With that out of the way, all I had to do now was rerun "node index.js" and I was good to go!

Wrong. Turns out I had to figure out Javascript to. Tune in next time, on "Attempting a ChatGPT Discord Bot", Seej wrestles with Javascript!