首页 > 解决方案 > 如何制作在页脚中显示时间戳的狙击命令

问题描述

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

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 保存在字典中。

标签: python-3.xdiscord.pydiscord.py-rewrite

解决方案


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

您所在位置的 UTC 时间更新,因此对您而言,它将显示您的时间(例如:今天上午 8:00),然后为世界其他地方的其他人显示时间(今天上午 9:00)。

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


推荐阅读