简体   繁体   中英

Why am I getting a `AttributeError: 'NoneType' object has no attribute 'send'` Error

It worked once then never again, I'm trying to load my log channel from a.env file which is working and loading fine, but when I go to send a log message to my log channel I get AttributeError: 'NoneType' object has no attribute 'send'

The ID is correct and I have given the bot explicit permission to send in that channel, but the error persists.

# Load Bot token
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
CHANNEL = os.getenv('LOG_CHANNEL')

# Set Bot command prefix
prefix = 'Q!'
bot = commands.Bot(command_prefix=prefix)
channel = bot.get_channel(CHANNEL)

The.env file has LOG_CHANNEL = 512123500123652096 and it loads it correctly, marking CHANNEL as int() doesn't help either

This is the first thing the bot does when it turns on

This is where I log, it's inside a command at the end

# Log command
command = discord.Embed(description=f"File unzipped in {ctx.channel.mention}", color=0x4040EC).set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
command.add_field(name="File", value=f'{filename}')
command.timestamp = ctx.message.created_at
await channel.send(embed=command)

The correct way of retrieving a channel is by using:

bot.get_channel(id)

If you are reading the channel id from a file it will be probably taking it as an string, you will need to convert it to int:

channel = bot.get_channel(int(CHANNEL))

It might also happen that get_channel returns None if your CHANNEL includes some extra spaces (maybe a space at the end). Make sure to double check that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM