【问题标题】:How can I get a Discord.py time stamp without CTX?如何在没有 CTX 的情况下获得 Discord.py 时间戳?
【发布时间】:2021-05-29 21:08:33
【问题描述】:

我正在制作一个 Discord 机器人。它的功能之一是在日志通道中记录离开和进入成员。 on_member_join 和 on_member_remove 事件仅采用成员参数。我想知道如何在没有 ctx 的情况下在我的嵌入中添加时间戳。我知道这是可能的,我只是不知道怎么做。

【问题讨论】:

  • 你也可以分享你的代码/方法吗?

标签: python timestamp discord discord.py


【解决方案1】:

您可以通过datetime 获取当前时间,为此您只需导入一个模块。如果您将整个内容发布到嵌入中,您可以执行以下操作:

import datetime # 1
from datetime import datetime # 2

@commands.Cog.listener / @client.event / @bot.event
async def on_member_join
embed = discord.Embed(timestamp=datetime.utcnow())

如果你想发送这类消息,你必须定义一个频道,因为ctx.send 不起作用,所以宁愿去:

@commands.Cog.listener / @client.event / @bot.event
channel = self.bot.get_channel(ChannelID) # Or bot/client.get_channel
async def on_member_join
embed = discord.Embed(timestamp=datetime.utcnow())
[...]
await channel.send(embed=embed)

如果您已经定义了一个频道并且只想将其作为普通消息发送,您可以将datetime 放入一个 f 字符串中:

await channel.send(f"{datetime}")

【讨论】:

  • 我收到错误“模块日期时间没有属性“utcnow”
  • 然后尝试使用datetime.datetime.utcow
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多