You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pycord斜杠命令报错:AttributeError: 'NoneType'无'created_at'属性

解决Discord斜杠命令中AttributeError: 'NoneType' object has no attribute 'created_at'错误

问题描述

用户信息前缀命令运行正常,但转换为斜杠命令后抛出错误:

AttributeError: 'NoneType' object has no attribute 'created_at'

相关代码:

@bot.slash_command(name="info", description="Get information about a user", guild_ids=[824342611774144543])
async def info(ctx, user:discord.Member=None):
    if user==None:
        user=ctx.author

    embedinfo = discord.Embed(color=0x774dea, timestamp=ctx.message.created_at)
    embedinfo.set_author(name=f"User Info | {user}"),
    embedinfo.set_thumbnail(url=user.avatar.url),
    embedinfo.set_footer(text=f"Requested by {ctx.author}", icon_url=ctx.author.avatar.url)

    embedinfo.add_field(name='Username:', value=user.display_name, inline=True)
    embedinfo.add_field(name='Top Role:', value=user.top_role.mention, inline=True)
    embedinfo.add_field(name='\u200b', value='\u200b', inline=True)

    embedinfo.add_field(name='Created at:', value=user.created_at.strftime("%b %d, %Y, %T"), inline=True)
    embedinfo.add_field(name='Joined at:', value=user.joined_at.strftime("%b %d, %Y, %T"), inline=True)

    embedinfo.add_field(name='ID:', value=user.id, inline=False)

    await ctx.respond(embed=embedinfo)

完整错误栈:

Ignoring exception in command info:
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 110, in wrapped
    ret = await coro(arg)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 774, in _invoke
    await self.callback(ctx, **kwargs)
  File "e:\HypePartyBOT\test5.py", line 792, in info
    embedinfo = discord.Embed(color=0x774dea, timestamp=ctx.message.created_at)
AttributeError: 'NoneType' object has no attribute 'created_at'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 768, in process_application_commands
    await ctx.command.invoke(ctx)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 306, in invoke
    await injected(ctx)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 116, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.commands.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'NoneType' object has no attribute 'created_at'

错误原因

Discord斜杠命令的上下文对象(ctx)不存在message属性,ctx.message的值为None,因此无法访问created_at属性。前缀命令依赖消息上下文,而斜杠命令属于应用命令,使用独立的上下文对象,没有关联的消息实体。

解决方案

将ctx.message.created_at替换为discord.utils.utcnow()获取当前UTC时间作为Embed的时间戳,同时移除原代码中多余的逗号(会导致返回元组而非正确设置属性)。

修正后的代码:

import discord
from discord import utils

@bot.slash_command(name="info", description="Get information about a user", guild_ids=[824342611774144543])
async def info(ctx, user:discord.Member=None):
    if user is None:
        user = ctx.author

    embedinfo = discord.Embed(color=0x774dea, timestamp=utils.utcnow())
    embedinfo.set_author(name=f"User Info | {user}")
    embedinfo.set_thumbnail(url=user.avatar.url)
    embedinfo.set_footer(text=f"Requested by {ctx.author}", icon_url=ctx.author.avatar.url)

    embedinfo.add_field(name='Username:', value=user.display_name, inline=True)
    embedinfo.add_field(name='Top Role:', value=user.top_role.mention, inline=True)
    embedinfo.add_field(name='\u200b', value='\u200b', inline=True)

    embedinfo.add_field(name='Created at:', value=user.created_at.strftime("%b %d, %Y, %T"), inline=True)
    embedinfo.add_field(name='Joined at:', value=user.joined_at.strftime("%b %d, %Y, %T"), inline=True)

    embedinfo.add_field(name='ID:', value=user.id, inline=False)

    await ctx.respond(embed=embedinfo)

内容的提问来源于stack exchange,提问作者Hic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 01:25:37