简体   繁体   中英

How can I fix, AttributeError: 'NoneType' object has no attribute 'send'

Aim of this is to try get a local image saved on your computer sent to a discord channel via a bot

# Create an Intents object with the `messages` attribute set to True
intents = discord.Intents(messages=True)

# Create the client with the specified intents
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    # When the bot is ready, send the image to the specified channel
    channel = client.get_channel(CHANNEL_ID)
    with open(r"file path", 'rb') as f:
        file = discord.File(f)
        await channel.send(file=file)

client.run(TOKEN)
Traceback (most recent call last):
  File "C:\Python\Python39\lib\site-packages\discord\client.py", line 409, in _run_event
    await coro(*args, **kwargs)
  File "path", line 39, in on_ready
    await channel.send(file=file)
AttributeError: 'NoneType' object has no attribute 'send'

The offical documentation for Discord.py says

Parameters: id ( int ) – The ID to search for.

Returns: The returned channel or None if not found.

This means that the ID supplied does not match any server, so most likely you mistyped it or accidentally modified it in code somewhere

    await channel.send(file=file)
AttributeError: 'NoneType' object has no attribute 'send'

So channel is None , so the error was caused by the following.


channel = client.get_channel(CHANNEL_ID)

I guess 99% that CHANNEL_ID is a str ing and not an int eger...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM