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

如何在discord.py嵌入消息中将时间戳改为D/M/Y+24小时制格式?

Discord.py Kick命令嵌入消息时间戳格式修改方案

问题说明

你当前的kick命令中,嵌入消息的时间戳被Discord客户端自动显示为「刚刚」+20:00的相对时间格式,需要改为日/月/年(D/M/Y)+时分的固定格式。

解决方案

Discord客户端会自动将Embed的timestamp参数转换为相对时间显示,无法直接通过该参数自定义格式。要实现目标格式,需手动生成格式化后的时间字符串,并添加到Embed的描述或字段中。

修改后的代码示例

方法1:将格式化时间直接加入描述

import datetime
import discord
from discord.ext import commands

@bot.hybrid_command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.reply(f'{member.name} foi expulso do servidor.', ephemeral=True)
    channel = bot.get_channel(WINTON_STATUS)
    
    # 生成D/M/Y + 时分格式的时间字符串
    formatted_time = datetime.datetime.now().strftime("%d/%m/%Y %H:%M")
    
    embed = discord.Embed(
        title=f'{member.global_name} kikou pros crias',
        description=f'{member.mention}\n\nData e Hora: {formatted_time}',
        color=0x2b4982,
    )
    
    await channel.send(embed=embed)

方法2:保留相对时间的同时添加格式化时间字段

如果想同时保留Discord自带的相对时间显示,可保留timestamp参数,再新增一个字段展示固定格式时间:

import datetime
import discord
from discord.ext import commands

@bot.hybrid_command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.reply(f'{member.name} foi expulso do servidor.', ephemeral=True)
    channel = bot.get_channel(WINTON_STATUS)
    
    current_datetime = datetime.datetime.now()
    formatted_time = current_datetime.strftime("%d/%m/%Y %H:%M")
    
    embed = discord.Embed(
        title=f'{member.global_name} kikou pros crias',
        description=f'{member.mention}',
        timestamp=current_datetime,  # 保留相对时间显示
        color=0x2b4982,
    )
    # 添加固定格式时间字段
    embed.add_field(name="Data e Hora", value=formatted_time, inline=False)
    
    await channel.send(embed=embed)

关键说明

  • 使用datetime.strftime("%d/%m/%Y %H:%M")生成指定格式的时间字符串:
    • %d:两位数字的日期(01-31)
    • %m:两位数字的月份(01-12)
    • %Y:四位数字的年份
    • %H:%M:24小时制的时分

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 18:52:34