#ChatGPT Discord Bot

37 messages · Page 1 of 1 (latest)

pseudo token
#

so basically i dont like bots from others and i like to do things on my own (probably a bad idea ngl), anyways im trying to build a bot that would use openai api as a response to messages. i tried consulting with the openai itself (chatgpt) but it started looping and i keep running into the same errors. (code is in python)

import discord 

openai.api_key = "myapi"

def generate_response(prompt):
    response = openai.Completion.create(engine="text-davinci-002", prompt=prompt, max_tokens=1024)
    return response.text

intents = discord.Intents().all()
client = discord.Client(intents=intents)

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    prompt = message.content
    response_text = generate_response(prompt)
    await message.channel.send(response_text)

client.run("mycode")```
so that's the code and the newest error is:

```[2022-12-31 02:53:39] [ERROR   ] discord.client: Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\piotr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openai\openai_object.py", line 59, in __getattr__
    return self[k]
KeyError: 'text'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\client.py", line 409, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\xxx\source\repos\PythonApplication1\PythonApplication1.py", line 19, in on_message
    response_text = generate_response(prompt)
  File "C:\Users\xxx\source\repos\PythonApplication1\PythonApplication1.py", line 8, in generate_response
    return response.text
  File "C:\Users\xxx\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openai\openai_object.py", line 61, in __getattr__
    raise AttributeError(*err.args)
AttributeError: text

also im using the openai library version 0.25.0 cause i can't seem to find the newer ones. any help would be appreciated. also if it would be better then you can rewrite the code to a newer library or smth. ps: yes i do have the api and discord token replaced, i just deleted it for privacy reasons

indigo bobcat
#

Isn't this what all the other bots do? They all do this interfacing with GPT3

#

also, response.txt is not a valid field

#

You're looking for response["choices"][0]["text"]

#

replace return response.txt wtih
return response["choices"][0]["text"]

#

Something useful in the future too is, when you see an issue like this, print the response object and try to find the data field that you're looking for

#

and don't use ChatGPT to develop on OpenAI APIs, it has terrible knowledge of how they work, ironically

pastel pecan
#

hi

lyric compass
#

@pseudo token you fixed the issue?

pseudo token
lyric compass
#

so the bot works?

#

im trying to create a chatbot from 1 week , but everytime there are much errors, and im looking for a script with no errors

lyric compass
#

@pseudo token let me know if you can give me your code

pseudo token
# lyric compass <@608219199453528085> let me know if you can give me your code

yea sure. the bot works with the openai 0.25.0 library and python. the code is

import discord 

openai.api_key = "YOUT-API"

def generate_response(prompt):
    response = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=1024)
    return response["choices"][0]["text"]

intents = discord.Intents().all()
client = discord.Client(intents=intents)

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    prompt = message.content
    response_text = generate_response(prompt)
    await message.channel.send(response_text)



client.run("YOUR_BOT_TOKEN")```
#

also the discord library

lyric compass
pseudo token
#

np

#

the library is pretty old and has bugs, but i can't really do nothing about it, since newer versions are not public

elfin shadow
#

/help

dapper tree
#

/help

azure oar
#

Can yall explain to me how the bot works but like, in dummy? Idk how discord bots work at all really

unreal yoke
#

CREATE AN APPLE WITH LION

azure oar
pseudo token
#

you will need the discord library and openai with npm

azure oar
#

I'm on mobile

#

I just wanna know what it does

pseudo token
#

it starts up a discord bot that uses the chat gpt 3.0

#

basically it

radiant notch
#

/help

#

help

pale tulip
#

Hi

round walrus
#

HY

pseudo token
#

HY

pale tulip
#

Hello

fickle hatch
#

hi