【发布时间】:2021-10-25 21:01:53
【问题描述】:
timedif 命令采用两个消息 ID 并计算它们发送的时间差,精确到小数点后 2 位。
这是我做的:
@bot.command(name='timedif', help='', aliases=['snowflake', 'timediff'])
async def timedif(ctx, id1, id2):
try:
id1 = int(id1)
id2 = int(id2)
except:
await ctx.reply("Check your message ID's! They are incorrect!")
msg1 = await ctx.fetch_message(id1)
msg2 = await ctx.fetch_message(id2)
time1 = msg1.created_at
time2 = msg2.created_at
ts_diff = time2 - time1
secs = abs(ts_diff.total_seconds())
days,secs=divmod(secs,secs_per_day:=60*60*24)
hrs,secs=divmod(secs,secs_per_hr:=60*60)
mins,secs=divmod(secs,secs_per_min:=60)
secs=round(secs, 2)
answer='{} secs'.format(secs)
if secs > 60:
answer='{} mins and {} secs'.format(int(mins),secs)
if mins > 60:
answer='{} hrs, {} mins and {} secs'.format(int(hrs),int(mins),secs)
if hrs > 24:
answer='{} days, {} hrs, {} mins and {} secs'.format(int(days),int(hrs),int(mins),secs)
embed = discord.Embed(title="**Time Difference**", description=f"""IDs: {id1}, {id2}
Time difference between the 2 IDs:
{answer}""")
await ctx.reply(embed=embed)
我测试了机器人,但遇到了一个问题: 对于此代码,它只能从与“ctx”相同的通道中获取消息。但是,如果我从另一个频道(即 DM 频道)获取其中一个 ID,则机器人无法访问它。是否有算法/函数可以让我计算 any 2 个 ID 的时间差?我认为这是可能的,因为最近很多机器人都在这样做。
澄清一下:对于来自ctx.channel 的消息,它工作正常并且能够计算时间差异。唯一的问题是它无法从其他渠道获取消息。
【问题讨论】:
-
您不能从 DM 中获取 ID。此外,请确保您启用了意图。
标签: python discord discord.py