[英]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.