4

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

random.coder
  • 173
  • 3
  • 7

2 Answers2

7

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

langley
  • 186
  • 4
0

you need to modify

client.get_channel()

with

client.get_guild(your server's id).get_channel(channel id)
Fghjkgcfxx56u
  • 99
  • 1
  • 11
  • Now it gives me the error AttributeError: 'NoneType' object has no attribute 'get_channel', so it's having the same problem finding the guild – random.coder Feb 14 '21 at 16:27