Search code examples
pythondiscorddiscord.py

client.get_channel(id) returning "none" on a channel that exists


I'm trying to make a bot for Discord, and every time I try and send a message to a designated channel using the following code, it doesn't give me any errors, but does print "None", which I would expect when the channel doesn't exist. I have now tried this with multiple guilds/servers and multiple text channels, as well as multiple computers running the same code. In the code following, I have replaced the channelID with the int that is the channelID, and the token with my token (a string).

import discord
from discord.ext import commands

intents = discord.Intents.all()
client = commands.Bot(command_prefix = 'bday ', intents = intents)
channel = client.get_channel(channelID)

print(channel)

client.run("token")

The bot does have admin permissions, as well as both of the intent gateways


Solution

  • It seems like you are trying to call a bot's functionality before actually running your bot.

    Try to add your code inside on_ready() callback to ensure that you are trying to get your channel only after initializing the bot itself.

    import discord
    from discord.ext import commands
    
    intents = discord.Intents.all()
    client = commands.Bot(command_prefix = 'bday ', intents = intents)
    
    @client.event
    async def on_ready():
        channel = client.get_channel(channelID)
        print(channel)
    
    client.run("token")