簡體   English   中英

為什么我收到“AttributeError: 'NoneType' object has no attribute 'send'”錯誤

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

它一次又一次地工作,我試圖從一個工作和加載正常的.env文件加載我的日志通道,但是當我 go 向我的日志通道發送日志消息時,我得到AttributeError: 'NoneType' object has no attribute 'send'

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)

.env 文件具有LOG_CHANNEL = 512123500123652096並正確加載它,將CHANNEL標記為 int() 也無濟於事

這是機器人開機時做的第一件事

這是我記錄的地方,它在最后的命令中

# 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)

檢索頻道的正確方法是使用:

bot.get_channel(id)

如果您從文件中讀取通道 id,它可能會將其作為字符串,您需要將其轉換為 int:

channel = bot.get_channel(int(CHANNEL))

如果您的CHANNEL包含一些額外的空格(可能在末尾有一個空格), get_channel也可能返回None 確保仔細檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM