简体   繁体   English

为什么我收到“AttributeError: 'NoneType' object has no attribute 'send'”错误

[英]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'它一次又一次地工作,我试图从一个工作和加载正常的.env文件加载我的日志通道,但是当我 go 向我的日志通道发送日志消息时,我得到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. ID 是正确的,我已明确授予机器人在该频道中发送的权限,但错误仍然存在。

# 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 .env 文件具有LOG_CHANNEL = 512123500123652096并正确加载它,将CHANNEL标记为 int() 也无济于事

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:如果您从文件中读取通道 id,它可能会将其作为字符串,您需要将其转换为 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).如果您的CHANNEL包含一些额外的空格(可能在末尾有一个空格), get_channel也可能返回None Make sure to double check that.确保仔细检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到“AttributeError: 'NoneType' object has no attribute 'get'” - why am i getting "AttributeError: 'NoneType' object has no attribute 'get' " 为什么我会收到 AttributeError: 'NoneType' object has no attribute 'send' 以及如何避免它? - Why am I getting AttributeError: 'NoneType' object has no attribute 'send' and how to avoid it? 为什么我在执行 PCA 时遇到此错误:- AttributeError: 'NoneType' object has no attribute 'split' - Why i am getting this error while doing PCA :- AttributeError: 'NoneType' object has no attribute 'split' 为什么我在 /admin/main/consultation/'NoneType' object 没有属性 'lastname' 处出现 AttributeError 错误 - why am i getting error of AttributeError at /admin/main/consultation/ 'NoneType' object has no attribute 'lastname' Web Scraper:为什么会出现AttributeError:'NoneType'对象没有属性'text'? - Web Scraper: Why am I getting AttributeError: 'NoneType' object has no attribute 'text'? 为什么我收到 AttributeError: “'NoneType' object has no attribute 'get'” 与 Python 和 Tkinkter? - Why I am getting AttributeError: “'NoneType' object has no attribute 'get'” with Python and Tkinkter? 我收到类似 AttributeError: 'NoneType' object has no attribute 'text' 的错误 - I am getting error like AttributeError: 'NoneType' object has no attribute 'text' 我在“AttributeError:'NoneType'对象中没有属性'get_all_permissions'中收到这些错误 - I am getting these error in " AttributeError: 'NoneType' object has no attribute 'get_all_permissions' 我不断收到错误:AttributeError: 'NoneType' object has no attribute 'strip' - I keep getting the error: AttributeError: 'NoneType' object has no attribute 'strip' 为什么错误 - AttributeError: 'NoneType' object 没有属性 - Why error - AttributeError: 'NoneType' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM