Search code examples
pythondiscorddiscord.pysendchannel

AttributeError: 'int' object has no attribute 'send'


Not sure what the issue is but help is appreciated!

@client.event
async def on_message(message):
    channel = CHANNEL_ID
    isbot = message.author.bot
    if isbot:
        pass
    else:
        await channel.send(message.content)

CHANNEL_IDis already defined on my tokens.py

This issue occurs whenever I send a message in the channel that the bot is focused on.


Solution

  • Since you are assigning channel to CHANNEL_ID, channel will be the same data type as CHANNEL_ID. Since CHANNEL_ID is probably an int, you can use a variety of available functions and methods to get the TextChannel object to use the send() method.


    Using the discord.utils.get() function:

    @client.event
    async def on_message(message):
        channel = discord.utils.get(message.guild.channels, id=CHANNEL_ID)
        isbot = message.author.bot
        if isbot:
            pass
        else:
            await channel.send(message.content)
    

    Using the Client.fetch_channel() method:

    @client.event
    async def on_message(message):
        channel = await client.fetch_channel(CHANNEL_ID)
        isbot = message.author.bot
        if isbot:
            pass
        else:
            await channel.send(message.content)
    

    Using the Client.get_channel() method:

    @client.event
    async def on_message(message):
        channel = await client.get_channel(CHANNEL_ID)
        isbot = message.author.bot
        if isbot:
            pass
        else:
            await channel.send(message.content)