Reputation: 311
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
Reputation: 2367
Using channelID as a integer instead of string worked for me.
Upvotes: 1
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
Reputation: 109
you need to modify
client.get_channel()
with
client.get_guild(your server's id).get_channel(channel id)
Upvotes: 0