Why does bot.get_channel() produce NoneType?

Question:

I’m making a Discord bot to handle an announcement command. When the command is used, I want the bot to send a message in a specific channel and send a message back to the user to show that the command was sent.

However, I cannot get the message to be sent to the channel. I tried this code:


import discord
import os
import random
import asyncio

testing_servers = [912361242985918464]
intents = discord.Intents().all()
bot = discord.Bot(intents=intents)

@bot.slash_command(guild_ids=testing_servers, name="announce", description="Make server announcements!")
async def announce(ctx, title, text, channel_id,anonymous=None):
    #response embed
    print(channel_id)
    #announcement embed
    embed_announce = discord.Embed(
        colour = discord.Colour.blue(),
        title=str(title),
        description = text
    )
    await bot.get_channel(channel_id).send(embed = embed_announce)  

But before even attempting to send the other message back to the user, I get an error that says AttributeError: 'NoneType' object has no attribute 'send'.

I conclude that bot.get_channel(channel_id) evaluated to None. But why? How can I get the correct Channel to send the message?

Asked By: Blue Robin

||

Answers:

Make sure you are sending an integer to get_channel():

await bot.get_channel(int(channel_id)).send(embed=embed_announce)
Answered By: 3nws
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.