aboutsummaryrefslogtreecommitdiff
path: root/src/utils/builder/execute_webhook.rs
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-11-13 08:22:24 -0800
committerAustin Hellyer <[email protected]>2016-11-13 08:22:24 -0800
commitf633d1c9603079f584f4f715b308b33c0750ad3a (patch)
treef77bc0b630731676be880b11c24b90cffef8c537 /src/utils/builder/execute_webhook.rs
parentDon't overflow on message length check (diff)
downloadserenity-f633d1c9603079f584f4f715b308b33c0750ad3a.tar.xz
serenity-f633d1c9603079f584f4f715b308b33c0750ad3a.zip
Move the builders to the utils
The builders aren't a large enough portion of the library to deserve their own root-level module, so move them to the `utils` module. Additionally, split them into separate files, as the library will be receiving more builders and the single-file pattern was getting rather large.
Diffstat (limited to 'src/utils/builder/execute_webhook.rs')
-rw-r--r--src/utils/builder/execute_webhook.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/utils/builder/execute_webhook.rs b/src/utils/builder/execute_webhook.rs
new file mode 100644
index 0000000..d2a14aa
--- /dev/null
+++ b/src/utils/builder/execute_webhook.rs
@@ -0,0 +1,55 @@
+use serde_json::builder::ObjectBuilder;
+use serde_json::Value;
+use std::default::Default;
+
+/// A builder to create the inner content of a [`Webhook`]'s execution.
+///
+/// This is a structured way of cleanly creating the inner execution payload,
+/// to reduce potential argument counts.
+///
+/// Refer to the documentation for [`execute_webhook`] on restrictions with
+/// execution payloads and its fields.
+///
+/// [`Webhook`]: ../model/struct.Webhook.html
+/// [`execute_webhook`]: ../client/http/fn.execute_webhook.html
+pub struct ExecuteWebhook(pub ObjectBuilder);
+
+impl ExecuteWebhook {
+ /// Override the default avatar of the webhook with an image URL.
+ pub fn avatar_url(self, avatar_url: &str) -> Self {
+ ExecuteWebhook(self.0.insert("avatar_url", avatar_url))
+ }
+
+ /// Set the content of the message.
+ pub fn content(self, content: &str) -> Self {
+ ExecuteWebhook(self.0.insert("content", content))
+ }
+
+ // Set the embeds associated with the message.
+ pub fn embeds(self, embeds: Vec<Value>) -> Self {
+ ExecuteWebhook(self.0.insert("embeds", embeds))
+ }
+
+ /// Whether the message is a text-to-speech message.
+ ///
+ /// Think carefully before setting this to `true`.
+ pub fn tts(self, tts: bool) -> Self {
+ ExecuteWebhook(self.0.insert("tts", tts))
+ }
+
+ /// Override the default username of the webhook.
+ pub fn username(self, username: &str) -> Self {
+ ExecuteWebhook(self.0.insert("username", username))
+ }
+}
+
+impl Default for ExecuteWebhook {
+ /// Returns a default set of values for a [`Webhook`] execution.
+ ///
+ /// The only default value is `tts` being set to `true`. In the event that
+ /// there is a bug that Discord defaults `tts` to `true`, at least
+ /// serenity.rs won't be a part of it.
+ fn default() -> ExecuteWebhook {
+ ExecuteWebhook(ObjectBuilder::new().insert("tts", false))
+ }
+}