random.coder
random.coder

Reputation: 311

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

Upvotes: 3

Views: 10857

Answers (3)

Mateusz Kaflowski
Mateusz Kaflowski

Reputation: 2367

Using channelID as a integer instead of string worked for me.

Upvotes: 1

langley
langley

Reputation: 226

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")

Upvotes: 8

Fghjkgcfxx56u
Fghjkgcfxx56u

Reputation: 109

you need to modify

client.get_channel()

with

client.get_guild(your server's id).get_channel(channel id)

Upvotes: 0

Related Questions