Search code examples
pythondiscorddiscord.pytypeerrorreplit

"TypeError: 'str' object is not callable" Discord Bot with Python on repl.it


I wanted to create a Discord bot to send a text message on a Discord server containing all the commands when someone types "^help" but I get this error:

Traceback (most recent call last):
  File "client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "bot.py", line 38, in on_message
    if help in message.content():
TypeError: 'str' object is not callable

This is my code:

main.py

import bot

if __name__ == '__main__':
    bot.run_discord_bot()

bot.py

import discord
import responses


async def send_message(message, user_message, is_private):
    try:
        response = responses.get_response(user_message)
        await message.author.send(response) if is_private else await message.channel.send(response)

    except Exception as e:
        print(e)


def run_discord_bot():
    TOKEN = '(token)'
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)

    @client.event
    async def on_ready():
        print(f'{client.user} is now running!')

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

        username = str(message.author)
        user_message = str(message.content)
        channel = str(message.channel)

        print(f'{username} said: "{user_message}" ({channel})')
      
    @client.event
    async def on_message(message):
        if "^help" in message.content():
          await message.channel.send("text")
    client.run(TOKEN)

I am new to Dicsord bot development so I don't know what else to do.


Solution

  • The error you're getting, TypeError: 'str' object is not callable, is because you're trying to call a string object as if it is a function. Looking at your error, the issue lies in the following line:

    if "^help" in message.content():
    

    The message.content attribute returns a string. However, you are treating it as a function by using parentheses after message.content. To fix this error, you should remove the parentheses:

    if "^help" in message.content:
    

    Here's the updated code:

    import discord
    import responses
    
    
    async def send_message(message, user_message, is_private):
        try:
            response = responses.get_response(user_message)
            await message.author.send(response) if is_private else await message.channel.send(response)
    
        except Exception as e:
            print(e)
    
    
    def run_discord_bot():
        TOKEN = '(token)'
        intents = discord.Intents.default()
        intents.message_content = True
        client = discord.Client(intents=intents)
    
        @client.event
        async def on_ready():
            print(f'{client.user} is now running!')
    
        @client.event
        async def on_message(message):
            if message.author == client.user:
                return
    
            username = str(message.author)
            user_message = str(message.content)
            channel = str(message.channel)
    
            print(f'{username} said: "{user_message}" ({channel})')
    
            if "^help" in message.content:
                await message.channel.send("text")
    
        client.run(TOKEN)