如何制作在页脚中显示时间戳的狙击命令

How do I make a snipe command that shows the timestamp in the footer

我可以使用 'pytz' 库和 'datetime' 库获取 UTC 时间,但我需要用户本地时间。假设你 运行 来自美国的 snipe 命令,你应该得到你的本地时间,如果我 运行 来自意大利,我应该得到意大利的时间。希望我说清楚了。

x = message = {}
y = author = {}
z = author_avatar = {}
time = {}

@client.event
async def on_message_delete(msg):
    UTC = pytz.utc
    datetime_utc = datetime.now(UTC)   
    


    if msg.author.bot == False:
        x[msg.channel.id] = msg.content
        y[msg.channel.id] = msg.author
        time[msg.channel.id] = datetime_utc.strftime('%H:%M UTC')

    if msg.author == client.user:
        x[msg.channel.id] = msg.content
        y[msg.channel.id] = msg.author
        time[msg.channel.id] = datetime_utc.strftime('%H:%M UTC')

    


@client.command(name = 'snipe')
async def snipe(ctx):
    try:
        em = discord.Embed(description = f"    {x[ctx.channel.id]}" ,color = random.choice(colors_for_embeds1), timestamp = datetime.now())
        em.set_author(name = y[ctx.channel.id] ,icon_url = (y[ctx.channel.id]).author.url)
        em.set_footer(text = f"at {time[ctx.channel.id]}")
        
        
        await ctx.send(embed = em)
    except:
        await ctx.send("There is nothing to snipe!")

这就是命令的工作原理。删除的消息被添加到一个以频道 ID 为键的字典中,作者 ID 被保存在一个以频道 ID 为键的字典中。

我希望这能回答你的问题。

你所在位置的 UTC 时间更新,所以对你来说,它会显示你的时间(例如:今天 8:00 AM)然后对于世界其他地方的其他人会显示(今天 9:00上午)。

不知道我回答的好不好,或者你是否看懂了。 但希望能回答你的问题! :D

您的机器人无法知道 运行 命令的人所在的时区。 discord 嵌入的时间戳始终以本地格式显示看到嵌入的人的时间,因此不同的人会根据他们的时区看到不同的时间。

一种解决方案是使用不同的命令记录用户时区并将其保存到数据库中。

然后根据您的命令将时间解析为您想要的时区的页脚。