简体   繁体   English

使用“几天前”而不是日期

[英]Use "days ago" instead of date

I'm currently working on a discord bot that shows me user information like ID, Name, and that stuff, and I added a Join date and an account create date, they are working fine but I also want to include a "days ago" instead of the blank date.我目前正在开发一个 discord 机器人,它向我显示 ID、姓名等用户信息,我添加了一个加入日期和一个帐户创建日期,它们工作正常,但我也想包括一个“几天前”而不是空白日期。

Here is the code I have:这是我的代码:

from unicodedata import name
import discord
from datetime import datetime
from discord.ext import commands

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='.',intents=intents)

@bot.event
async def on_ready():
    print("Ready!")

@bot.command(name="user")
async def user(ctx,user:discord.Member=None):

    if user==None:
        user=ctx.author

    rlist = []
    for role in user.roles:
        if role.name != "@everyone":
            rlist.append(role.mention)
    b = ','.join(rlist)
    
    if rlist:
        b = ','.join(rlist)
    else:
        b = "No roles"


    embed = discord.Embed(colour=user.color,timestamp=ctx.message.created_at)

    embed.set_author(name=f"User Info - {user}"),
    embed.set_thumbnail(url=user.avatar.url),
    embed.set_footer(text=f'Requested by - {ctx.author}',
    icon_url=ctx.author.avatar.url)

    embed.add_field(name='ID:' ,value=user.id,inline=False)
    embed.add_field(name='Name:' ,value=user.display_name,inline=False)

    embed.add_field(name='Created at:' ,value=datetime.strftime(user.created_at, "%#d/%m/%Y"), inline=False)
    embed.add_field(name='Joined at:' ,value=datetime.strftime(user.joined_at, "%#d/%m/%Y"), inline=False)
    
    embed.add_field(name='Bot?' ,value=user.bot,inline=False)

    embed.add_field(name=f'Roles:({len(rlist)})',value=''.join([b]),inline=False)
    embed.add_field(name='Top Role:' ,value=user.top_role.mention,inline=False)

    await ctx.send(embed=embed)

I already tried我已经试过了

    duration = dt.datetime.now() - user.joined_at 

    hours, remainder = divmod(int(duration .total_seconds()), 3600)
    minutes, seconds = divmod(remainder, 60)
    days, hours = divmod(hours, 24)

    await ctx.send(f"Joined before {days}d, {hours}h, {minutes}m, {seconds}s")

This one does not work, I think it has to do with the time zones, I tried to remove time zone awareness but that also did not work for me.这个不起作用,我认为它与时区有关,我试图消除时区意识,但这对我也不起作用。

You can do this:你可以这样做:

joined_at = user.joined_at.timestamp()
delta = int(datetime.utcnow().timestamp()) - int(joined_at)
hours, remainder = divmod(delta, 3600)
minutes, seconds = divmod(remainder, 60)
days, hours = divmod(hours, 24)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM