Search code examples
pythondiscordartificial-intelligenceopenai-api

Why does this error keep showing, what am i missing? await message.channel.send(f"Answer: {bot_response}") IndentationError: unexpected indent


So my code is basically copied from a walktrough for making a discord bot use chatgpt.

Im using Pycharm for the project.

This would be the main - code:

from typing import Final
import os
import discord
from dotenv import load_dotenv
from chatgpt_ai.openai import chatgpt_response

load_dotenv()
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')

class MyClient(discord.Client):
    async def on_ready(self):
        print("Successfully logged in as: ", self.user)

    async  def on_message(self, message):
        print(message.content)
        if message.author == self.user:
            return
        command, user_message=None, None

    **for text in ['/ai', '/bot', 'chatgpt']:**
        if message.content.startswith(text):
            command = message.content.split('')[0]
            user_message = message.content.replace(text, '')
            print(command, user_message)

        if command == '/ai' or command == '/bot' or command == '/chatgpt':
            bot_response = chatgpt_response(prompt=user_message)
            await message.channel.send(f"Answer: {bot_response}")

    intents = discord.Intents.default()
    intents.message_content = True

    client = MyClient(intents=intents)




Terminal error code: 

File "C:\Users\Flavi\PycharmProjects\discord_flavio_bot\discord_bot\main.py", line 28
    await message.channel.send(f"Answer: {bot_response}")
IndentationError: unexpected indent

whats also weird - is that from line**for text in ['/ai', '/bot', 'chatgpt']**:

everly line with "message" is marked as error with suggestions to import from "email"....

I tried various things, checked my code for errors, spellchecking and see if i missed something within the walktrough. But also its my very first attempt on a python project so i might just be to inexcpirienced to even know where to look for the error.enter image description here


Solution

  • it seems like you made a mistake in your indentation starting at the for loop. Python indentation determines the scope where code runs, message cannot be found because it is out of scope (because it's missing a level of indentation)

    i believe indenting 20 to 28 and unindenting the rest after should fix your issue

    async def on_message(self, message):
        print(message.content)
        if message.author == self.user:
            return
        command, user_message=None, None
    
        for text in ['/ai', '/bot', 'chatgpt']:**
            if message.content.startswith(text):
                command = message.content.split('')[0]
                user_message = message.content.replace(text, '')
                print(command, user_message)
    
            if command == '/ai' or command == '/bot' or command == '/chatgpt':
                bot_response = chatgpt_response(prompt=user_message)
                await message.channel.send(f"Answer: {bot_response}")