(msgeditor): Support formatted timestamp.

This commit is contained in:
2024-08-27 17:31:11 +10:00
parent 58c0873987
commit daa370e09f

View File

@@ -54,7 +54,10 @@ class MsgEditor(MessageUI):
By default, uses the provided `formatter` callback (if provided).
"""
if self._formatter is not None:
await self._formatter(data)
return await self._formatter(data)
else:
return data
def copy_data(self):
return copy.deepcopy(self.history[-1])
@@ -78,7 +81,8 @@ class MsgEditor(MessageUI):
if 'embed' in new_data:
try:
discord.Embed.from_dict(new_data['embed'])
formatted_data = copy.deepcopy(new_data)
discord.Embed.from_dict(await self.format_data(formatted_data['embed']))
except Exception as e:
raise UserInputError(
t(_p(
@@ -445,8 +449,17 @@ class MsgEditor(MessageUI):
embed_data.pop('footer', None)
if (ts := timestamp_field.value):
if ts.isdigit():
# Treat as UTC timestamp
timestamp = dt.datetime.fromtimestamp(int(ts), dt.timezone.utc)
ts = timestamp.isoformat()
to_validate = ts
elif self._formatter:
to_validate = await self._formatter(ts)
else:
to_validate = ts
try:
dt.datetime.fromisoformat(ts)
dt.datetime.fromisoformat(to_validate)
except ValueError:
raise UserInputError(
t(_p(