diff options
| author | Zeyla Hellyer <[email protected]> | 2017-06-04 20:32:25 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-06-06 10:12:13 -0700 |
| commit | 990e611a56f37f64fbce74fbc487c7dcc4aa4e28 (patch) | |
| tree | 399c69583bd256d31668affc227f235386814552 /src/builder | |
| parent | Add CurrentUser::face(), User::face() (diff) | |
| download | serenity-990e611a56f37f64fbce74fbc487c7dcc4aa4e28.tar.xz serenity-990e611a56f37f64fbce74fbc487c7dcc4aa4e28.zip | |
Use chrono for struct timestamp fields
Chrono is easier to use than timestamped strings, so they should be
automatically deserialized and available for the user, instead of having
the user deserialize the strings themselves.
These fields have been changed to use a type of `DateTime<FixedOffset>`:
- `ChannelPinsUpdateEvent.last_pin_timestamp`
- `Group.last_pin_timestamp`
- `Guild.joined_at`
- `GuildChannel.last_pin_timestamp`
- `Invite.created_at`
- `Member.joined_at`
- `Message.edited_timestamp
- `Message.timestamp`
- `MessageUpdateEvent.edited_timestamp`
- `MessageUpdateEvent.timestamp`
- `PrivateChannel.last_pin_timestamp`
`Member.joined_at` is now also an `Option`. Previously, if a Guild
Member Update was received for a member not in the cache, a new Member
would be instantiated with a default String value. This is incorrect
behaviour, and has now been replaced with being set to `None` in that
case.
Id methods' `created_at()` method now return a `chrono::NaiveDateTime`
instead of a `time::Timespec`, and `User::created_at` has been updated
to reflect that.
Additionally, drop `time` as a direct dependency and use chrono for
internals.
Diffstat (limited to 'src/builder')
| -rw-r--r-- | src/builder/create_embed.rs | 88 |
1 files changed, 73 insertions, 15 deletions
diff --git a/src/builder/create_embed.rs b/src/builder/create_embed.rs index 0099936..7b676be 100644 --- a/src/builder/create_embed.rs +++ b/src/builder/create_embed.rs @@ -15,9 +15,10 @@ //! [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds //! [here]: https://discordapp.com/developers/docs/resources/channel#embed-object +use chrono::{DateTime, TimeZone}; use serde_json::Value; use std::default::Default; -use time::Tm; +use std::fmt::Display; use ::internal::prelude::*; use ::model::Embed; @@ -177,26 +178,75 @@ impl CreateEmbed { /// Set the timestamp. /// - /// **Note**: This timestamp must be in ISO-8601 format. It must also be - /// in UTC format. - /// - /// # Examples - /// /// You may pass a direct string: /// /// - `2017-01-03T23:00:00` /// - `2004-06-08T16:04:23` /// - `2004-06-08T16:04:23` /// - /// Or a `time::Tm`: + /// This timestamp must be in ISO-8601 format. It must also be in UTC format. /// - /// ```rust,ignore - /// extern crate time; + /// You can also pass anything that implements `chrono::TimeZone`. /// - /// let now = time::now(); + /// # Examples + /// + /// Passing a string timestamp: /// - /// embed = embed.timestamp(now); - /// // ... + /// ```rust,no_run + /// # use serenity::Client; + /// # + /// # let mut client = Client::login(""); + /// # + /// client.on_message(|_, msg| { + /// if msg.content == "~embed" { + /// let _ = msg.channel_id.send_message(|m| m + /// .embed(|e| e + /// .title("hello") + /// .timestamp("2004-06-08T16:04:23"))); + /// } + /// }); + /// ``` + /// + /// Creating a join-log: + /// + /// Note: this example isn't efficient and is for demonstrative purposes. + /// + /// ```rust,no_run + /// # use serenity::Client; + /// # + /// # let mut client = Client::login(""); + /// # + /// use serenity::CACHE; + /// + /// client.on_guild_member_add(|_, guild_id, member| { + /// let cache = CACHE.read().unwrap(); + /// + /// if let Some(guild) = cache.guild(guild_id) { + /// let guild = guild.read().unwrap(); + /// + /// let channel_search = guild + /// .channels + /// .values() + /// .find(|c| c.read().unwrap().name == "join-log"); + /// + /// if let Some(channel) = channel_search { + /// let user = member.user.read().unwrap(); + /// + /// let _ = channel.read().unwrap().send_message(|m| m + /// .embed(|e| { + /// let mut e = e + /// .author(|a| a.icon_url(&user.face()).name(&user.name)) + /// .title("Member Join"); + /// + /// if let Some(ref joined_at) = member.joined_at { + /// e = e.timestamp(joined_at); + /// } + /// + /// e + /// })); + /// } + /// } + /// }); /// ``` pub fn timestamp<T: Into<Timestamp>>(mut self, timestamp: T) -> Self { self.0.insert("timestamp".to_owned(), Value::String(timestamp.into().ts)); @@ -406,10 +456,18 @@ impl From<String> for Timestamp { } } -impl From<Tm> for Timestamp { - fn from(tm: Tm) -> Self { +impl<'a> From<&'a str> for Timestamp { + fn from(ts: &'a str) -> Self { + Timestamp { + ts: ts.to_owned(), + } + } +} + +impl<'a, Tz: TimeZone> From<&'a DateTime<Tz>> for Timestamp where Tz::Offset: Display { + fn from(dt: &'a DateTime<Tz>) -> Self { Timestamp { - ts: tm.to_utc().rfc3339().to_string(), + ts: dt.to_rfc3339(), } } } |