#OpenAI bot help! Does not recognise my messages :(

14 messages · Page 1 of 1 (latest)

left galleon
#

My code is:

import os
import openai
import discord
from discord.ext import commands

DISCORD_TOKEN = "xxx"
OPENAI_API_KEY = "xxx"

# Initialize OpenAI API
openai.api_key = OPENAI_API_KEY

intents = discord.Intents.default()
intents.typing = False
intents.presences = False

class ChatBotClient(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    async def on_ready(self):
        print(f"{self.user} has connected to Discord!")

    async def on_message(self, message):
        if message.author == self.user:
            return

        input_text = message.content
        try:
            completion = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "assistant", "content": "You are a helpful assistant."},
                    {"role": "user", "content": input_text}
                ]
            )
            response = completion.choices[0].message["content"]
            await message.channel.send(response)
        except Exception as e:
            await message.channel.send(f"Error: {e}")

client = ChatBotClient(intents=intents)
client.run(DISCORD_TOKEN)```
#

Hi there, I'm having issues with my Discord bot code. It's not recognising the conversation I'm trying to have with it and is outputting the same message repeatedly. Can you help me fix this? Thanks!

storm kiln
#
            completion = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "assistant", "content": "You are a helpful assistant."},
                    {"role": "user", "content": input_text}
                ]
            )
#

its not gonna keep the conversation going unless you append the messages you received to the messages array.

#
                messages=[
                    {"role": "assistant", "content": "You are a helpful assistant."},
                    {"role": "user", "content": input_text},
                    {"role": "assistant", "content": "bots answer here"},
                    {"role": "user", "content": "your 
                ]
left galleon
#

Thanks man, I really appreciate it.

#

@storm kiln Im still getting te issue

#
import os
import openai
import discord
from discord.ext import commands

DISCORD_TOKEN = "xxx"
OPENAI_API_KEY = "xxx"

# Initialize OpenAI API
openai.api_key = OPENAI_API_KEY

intents = discord.Intents.default()
intents.typing = False
intents.presences = False
intents.members = False  # Add this line to disable the privileged intent


bot = commands.Bot(command_prefix="!", intents=intents)

# Initialize conversation history
conversation_history = {}

@bot.event
async def on_ready():
    print(f"{bot.user} has connected to Discord!")

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

    channel_id = message.channel.id
    input_text = message.content

    # Initialize the history for the channel if it doesn't exist
    if channel_id not in conversation_history:
        conversation_history[channel_id] = [
            {"role": "assistant", "content": "You are a helpful assistant."}
        ]

    conversation_history[channel_id].append({"role": "user", "content": input_text})

    try:
        completion = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=conversation_history[channel_id]
        )
        response = completion.choices[0].message["content"]
        conversation_history[channel_id].append({"role": "assistant", "content": response})
        await message.channel.send(response)
    except Exception as e:
        await message.channel.send(f"Error: {e}")

    await bot.process_commands(message)

bot.run(DISCORD_TOKEN)
steep vale
#

import os
import openai
import discord
from discord.ext import commands

DISCORD_TOKEN = "xxx"
OPENAI_API_KEY = "xxx"

openai.api_key = OPENAI_API_KEY

intents = discord.Intents.default()
intents.typing = False
intents.presences = False
intents.members = False

bot = commands.Bot(command_prefix="!", intents=intents)

conversation_history = {}

@bot.event
async def on_ready():
print(f"{bot.user} has connected to Discord!")

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

channel_id = message.channel.id
input_text = message.content

if channel_id not in conversation_history:
    conversation_history[channel_id] = [
        {"role": "assistant", "content": "You are a helpful assistant."}
    ]

previous_user_input = None
for i in range(len(conversation_history[channel_id])-1, -1, -1):
    if conversation_history[channel_id][i]["role"] == "user":
        previous_user_input = conversation_history[channel_id][i]["content"]
        break

if input_text != previous_user_input:
    conversation_history[channel_id].append({"role": "user", "content": input_text})

    try:
        completion = openai.Completion.create(
            model = "gpt-3.5-turbo",
            message = conversation_history[channel_id]
        )
#

response = completion.choices[0].text
response = response.strip()

        previous_response = None
        for i in range(len(conversation_history[channel_id])-1, -1, -1):
            if conversation_history[channel_id][i]["role"] == "assistant":
                previous_response = conversation_history[channel_id][i]["content"]
                break

        if response != previous_response:
            conversation_history[channel_id].append({"role": "assistant", "content": response})
        else:
            return await on_message(message)

        await message.channel.send(response)
    except Exception as e:
        await message.channel.send(f"Error: {e}")

await bot.process_commands(message)

bot.run(DISCORD_TOKEN)

left galleon
#

I added 'read history, write message, and attach files' as permission

left galleon
#

what perms in a server does it need?

hollow jackal
#

@left galleon
make sure to have enabled all Privileged gateway intents.
You can set them in discord developer portal in you application bot section below token