Attempting to create a discord bot and I am following the tutorial linked here. I am receiving the following error from line 14:
intents.message_content = True #NOQA
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'method' object has no attribute 'message_content'
Here is my code:
from typing import Final
import os
from dotenv import load_dotenv
from discord import Intents, Client, Message
from responses import get_response
#Step 0: Load our token from somewhere safe
load_dotenv()
#Final decorator makes it so that this cannot be overwritten or have subclasses
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')
# Step 1: Bot Setup - activate intents
intents: Intents = Intents.default
intents.message_content = True #NOQA
client: Client = Client(intents=intents)
#Step 2: Message Functionality
async def send_message(message: Message, user_message: str):
if not user_message:
print('Message was empty because intents were not enabled... probably')
return
# The := "Walrus Operator" is used to prompt for input.
if is_private := user_message[0] == '?':
user_message = user_message[1:]
try:
response: str = get_response(user_message)
await message.author.send(response) if is_private else message.channel.send(response)
except Exception as e:
print(e)
#Step 3: Handling the startup for our bot.
@client.event
async def on_ready() -> None:
print(f"{client.user} is now running")
#Step 4: Handle incoming messages
@client.event
async def on_message(message: Message) -> None:
if message.author == client.user:
return
username: str = str(message.author)
user_message: str = message.content
channel: str = str(message.channel)
print(f"[{channel}] {username}: '{user_message}")
await send_message(message, user_message)
# Step 5: Main entry point
def main() -> None:
client.run(token=TOKEN)
if __name__ == '__main__':
main()
I've found this issue in multiple forums with varying solutions dependent on the version of discord.py that's installed. For me, I'm running version 2.3.2. Additionally, I'm running python version 3.12.1.
- I've tried changing intents.message_content to intents.messages.
- Uninstalling and reinstalling the discord library.
Any ideas? Sorry if this is submitted incorrectly, I'm still new to Stack Overflow.
To summarize the solution(s) offered in the comments:
Intents.defaultis a method, so it must be called with parentheses. Errors with type annotations (: Intentsin Sam's case) are not detected by python's code checker so it can mislead you to thinking the error is on another line. If writing type annotations, a tool like mypy can help detect errors.Fixed line 13:
intents: Intents = Intents.default()All credit to https://stackoverflow.com/users/2288659/silvio-mayolo and https://stackoverflow.com/users/17030540/nigh-anxiety