aboutsummaryrefslogtreecommitdiff
path: root/src/model/channel
diff options
context:
space:
mode:
Diffstat (limited to 'src/model/channel')
-rw-r--r--src/model/channel/attachment.rs4
-rw-r--r--src/model/channel/channel_category.rs18
-rw-r--r--src/model/channel/channel_id.rs36
-rw-r--r--src/model/channel/embed.rs29
-rw-r--r--src/model/channel/guild_channel.rs46
-rw-r--r--src/model/channel/message.rs30
-rw-r--r--src/model/channel/mod.rs18
-rw-r--r--src/model/channel/private_channel.rs2
-rw-r--r--src/model/channel/reaction.rs2
9 files changed, 102 insertions, 83 deletions
diff --git a/src/model/channel/attachment.rs b/src/model/channel/attachment.rs
index eac513f..4d72f58 100644
--- a/src/model/channel/attachment.rs
+++ b/src/model/channel/attachment.rs
@@ -54,7 +54,7 @@ impl Attachment {
///
///
/// impl EventHandler for Handler {
- /// fn on_message(&self, _: Context, message: Message) {
+ /// fn message(&self, _: Context, message: Message) {
/// for attachment in message.attachments {
/// let content = match attachment.download() {
/// Ok(content) => content,
@@ -86,7 +86,7 @@ impl Attachment {
/// }
/// }
///
- /// fn on_ready(&self, _: Context, ready: Ready) {
+ /// fn ready(&self, _: Context, ready: Ready) {
/// println!("{} is connected!", ready.user.name);
/// }
/// }
diff --git a/src/model/channel/channel_category.rs b/src/model/channel/channel_category.rs
index f567cee..73c50a7 100644
--- a/src/model/channel/channel_category.rs
+++ b/src/model/channel/channel_category.rs
@@ -93,20 +93,14 @@ impl ChannelCategory {
}
}
- let mut map = Map::new();
- map.insert("name".to_string(), Value::String(self.name.clone()));
- map.insert(
- "position".to_string(),
- Value::Number(Number::from(self.position)),
- );
- map.insert(
- "type".to_string(),
- Value::String(self.kind.name().to_string()),
- );
+ let mut map = HashMap::new();
+ map.insert("name", Value::String(self.name.clone()));
+ map.insert("position", Value::Number(Number::from(self.position)));
+ map.insert("type", Value::String(self.kind.name().to_string()));
- let edited = f(EditChannel(map)).0;
+ let map = serenity_utils::hashmap_to_json_map(f(EditChannel(map)).0);
- http::edit_channel(self.id.0, &edited).map(|channel| {
+ http::edit_channel(self.id.0, &map).map(|channel| {
let GuildChannel {
id,
category_id,
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs
index 2e415dc..158ebcf 100644
--- a/src/model/channel/channel_id.rs
+++ b/src/model/channel/channel_id.rs
@@ -11,6 +11,8 @@ use builder::{CreateMessage, EditChannel, GetMessages};
use CACHE;
#[cfg(feature = "model")]
use http::{self, AttachmentType};
+#[cfg(feature = "model")]
+use utils;
#[cfg(feature = "model")]
impl ChannelId {
@@ -189,7 +191,9 @@ impl ChannelId {
/// [Manage Channel]: permissions/constant.MANAGE_CHANNELS.html
#[inline]
pub fn edit<F: FnOnce(EditChannel) -> EditChannel>(&self, f: F) -> Result<GuildChannel> {
- http::edit_channel(self.0, &f(EditChannel::default()).0)
+ let map = utils::hashmap_to_json_map(f(EditChannel::default()).0);
+
+ http::edit_channel(self.0, &map)
}
/// Edits a [`Message`] in the channel given its Id.
@@ -213,9 +217,9 @@ impl ChannelId {
/// [`the limit`]: ../builder/struct.CreateMessage.html#method.content
pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message>
where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> {
- let map = f(CreateMessage::default()).0;
+ let msg = f(CreateMessage::default());
- if let Some(content) = map.get("content") {
+ if let Some(content) = msg.0.get("content") {
if let Value::String(ref content) = *content {
if let Some(length_over) = Message::overflow_length(content) {
return Err(Error::Model(ModelError::MessageTooLong(length_over)));
@@ -223,19 +227,21 @@ impl ChannelId {
}
}
+ let map = utils::hashmap_to_json_map(msg.0);
+
http::edit_message(self.0, message_id.into().0, &Value::Object(map))
}
/// Search the cache for the channel with the Id.
#[cfg(feature = "cache")]
- pub fn find(&self) -> Option<Channel> { CACHE.read().unwrap().channel(*self) }
+ pub fn find(&self) -> Option<Channel> { CACHE.read().channel(*self) }
/// Search the cache for the channel. If it can't be found, the channel is
/// requested over REST.
pub fn get(&self) -> Result<Channel> {
#[cfg(feature = "cache")]
{
- if let Some(channel) = CACHE.read().unwrap().channel(*self) {
+ if let Some(channel) = CACHE.read().channel(*self) {
return Ok(channel);
}
}
@@ -315,13 +321,13 @@ impl ChannelId {
};
Some(match channel {
- Guild(channel) => channel.read().unwrap().name().to_string(),
- Group(channel) => match channel.read().unwrap().name() {
+ Guild(channel) => channel.read().name().to_string(),
+ Group(channel) => match channel.read().name() {
Cow::Borrowed(name) => name.to_string(),
Cow::Owned(name) => name,
},
- Category(category) => category.read().unwrap().name().to_string(),
- Private(channel) => channel.read().unwrap().name(),
+ Category(category) => category.read().name().to_string(),
+ Private(channel) => channel.read().name(),
})
}
@@ -445,9 +451,9 @@ impl ChannelId {
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html
pub fn send_files<'a, F, T, It: IntoIterator<Item=T>>(&self, files: It, f: F) -> Result<Message>
where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> {
- let mut map = f(CreateMessage::default()).0;
+ let mut msg = f(CreateMessage::default());
- if let Some(content) = map.get("content") {
+ if let Some(content) = msg.0.get("content") {
if let Value::String(ref content) = *content {
if let Some(length_over) = Message::overflow_length(content) {
return Err(Error::Model(ModelError::MessageTooLong(length_over)));
@@ -455,7 +461,8 @@ impl ChannelId {
}
}
- let _ = map.remove("embed");
+ let _ = msg.0.remove("embed");
+ let map = utils::hashmap_to_json_map(msg.0);
http::send_files(self.0, files, map)
}
@@ -481,14 +488,15 @@ impl ChannelId {
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html
pub fn send_message<F>(&self, f: F) -> Result<Message>
where F: FnOnce(CreateMessage) -> CreateMessage {
- let CreateMessage(map, reactions) = f(CreateMessage::default());
+ let msg = f(CreateMessage::default());
+ let map = utils::hashmap_to_json_map(msg.0);
Message::check_content_length(&map)?;
Message::check_embed_length(&map)?;
let message = http::send_message(self.0, &Value::Object(map))?;
- if let Some(reactions) = reactions {
+ if let Some(reactions) = msg.1 {
for reaction in reactions {
self.create_reaction(message.id, reaction)?;
}
diff --git a/src/model/channel/embed.rs b/src/model/channel/embed.rs
index 435f706..401f10d 100644
--- a/src/model/channel/embed.rs
+++ b/src/model/channel/embed.rs
@@ -4,6 +4,8 @@ use utils::Colour;
use internal::prelude::*;
#[cfg(feature = "model")]
use builder::CreateEmbed;
+#[cfg(feature = "model")]
+use utils;
/// Represents a rich embed which allows using richer markdown, multiple fields
/// and more. This was heavily inspired by [slack's attachments].
@@ -81,15 +83,14 @@ impl Embed {
/// let embed = Embed::fake(|e| e
/// .title("Embed title")
/// .description("Making a basic embed")
- /// .field(|f| f
- /// .name("A field")
- /// .value("Has some content.")
- /// .inline(false)));
+ /// .field("A field", "Has some content.", false));
/// ```
#[inline]
pub fn fake<F>(f: F) -> Value
where F: FnOnce(CreateEmbed) -> CreateEmbed {
- Value::Object(f(CreateEmbed::default()).0)
+ let map = utils::hashmap_to_json_map(f(CreateEmbed::default()).0);
+
+ Value::Object(map)
}
}
@@ -123,6 +124,24 @@ pub struct EmbedField {
pub value: String,
}
+impl EmbedField {
+ /// Creates a new embed field.
+ ///
+ /// **Note**: Refer to the [`name`] and [`value`] documentation for maximum
+ /// lengths.
+ ///
+ /// [`name`]: #structfield.name
+ /// [`value`]: #structfield.value
+ pub fn new<T, U>(name: T, value: U, inline: bool) -> Self
+ where T: Into<String>, U: Into<String> {
+ Self {
+ name: name.into(),
+ value: value.into(),
+ inline,
+ }
+ }
+}
+
/// Footer information for an embed.
#[derive(Clone, Debug, Deserialize)]
pub struct EmbedFooter {
diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs
index d6649b4..3f09bd7 100644
--- a/src/model/channel/guild_channel.rs
+++ b/src/model/channel/guild_channel.rs
@@ -116,7 +116,9 @@ impl GuildChannel {
}
}
- http::create_invite(self.id.0, &f(CreateInvite::default()).0)
+ let map = serenity_utils::hashmap_to_json_map(f(CreateInvite::default()).0);
+
+ http::create_invite(self.id.0, &map)
}
/// Creates a [permission overwrite][`PermissionOverwrite`] for either a
@@ -157,12 +159,12 @@ impl GuildChannel {
/// kind: PermissionOverwriteType::Member(user_id),
/// };
///
- /// let cache = CACHE.read().unwrap();
+ /// let cache = CACHE.read();
/// let channel = cache
/// .guild_channel(channel_id)
/// .ok_or(ModelError::ItemMissing)?;
///
- /// channel.read().unwrap().create_permission(&overwrite)?;
+ /// channel.read().create_permission(&overwrite)?;
/// # Ok(())
/// # }
/// #
@@ -199,12 +201,12 @@ impl GuildChannel {
/// kind: PermissionOverwriteType::Member(user_id),
/// };
///
- /// let cache = CACHE.read().unwrap();
+ /// let cache = CACHE.read();
/// let channel = cache
/// .guild_channel(channel_id)
/// .ok_or(ModelError::ItemMissing)?;
///
- /// channel.read().unwrap().create_permission(&overwrite)?;
+ /// channel.read().create_permission(&overwrite)?;
/// # Ok(())
/// # }
/// #
@@ -312,18 +314,12 @@ impl GuildChannel {
}
}
- let mut map = Map::new();
- map.insert("name".to_string(), Value::String(self.name.clone()));
- map.insert(
- "position".to_string(),
- Value::Number(Number::from(self.position)),
- );
- map.insert(
- "type".to_string(),
- Value::String(self.kind.name().to_string()),
- );
+ let mut map = HashMap::new();
+ map.insert("name", Value::String(self.name.clone()));
+ map.insert("position", Value::Number(Number::from(self.position)));
+ map.insert("type", Value::String(self.kind.name().to_string()));
- let edited = f(EditChannel(map)).0;
+ let edited = serenity_utils::hashmap_to_json_map(f(EditChannel(map)).0);
match http::edit_channel(self.id.0, &edited) {
Ok(channel) => {
@@ -365,7 +361,7 @@ impl GuildChannel {
/// **Note**: Right now this performs a clone of the guild. This will be
/// optimized in the future.
#[cfg(feature = "cache")]
- pub fn guild(&self) -> Option<Arc<RwLock<Guild>>> { CACHE.read().unwrap().guild(self.guild_id) }
+ pub fn guild(&self) -> Option<Arc<RwLock<Guild>>> { CACHE.read().guild(self.guild_id) }
/// Gets all of the channel's invites.
///
@@ -435,13 +431,13 @@ impl GuildChannel {
/// use serenity::CACHE;
///
/// impl EventHandler for Handler {
- /// fn on_message(&self, _: Context, msg: Message) {
- /// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) {
+ /// fn message(&self, _: Context, msg: Message) {
+ /// let channel = match CACHE.read().guild_channel(msg.channel_id) {
/// Some(channel) => channel,
/// None => return,
/// };
///
- /// let permissions = channel.read().unwrap().permissions_for(&msg.author).unwrap();
+ /// let permissions = channel.read().permissions_for(&msg.author).unwrap();
///
/// println!("The user's permissions: {:?}", permissions);
/// }
@@ -463,15 +459,15 @@ impl GuildChannel {
/// struct Handler;
///
/// impl EventHandler for Handler {
- /// fn on_message(&self, _: Context, msg: Message) {
- /// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) {
+ /// fn message(&self, _: Context, msg: Message) {
+ /// let channel = match CACHE.read().guild_channel(msg.channel_id) {
/// Some(channel) => channel,
/// None => return,
/// };
///
- /// let current_user_id = CACHE.read().unwrap().user.id;
+ /// let current_user_id = CACHE.read().user.id;
/// let permissions =
- /// channel.read().unwrap().permissions_for(current_user_id).unwrap();
+ /// channel.read().permissions_for(current_user_id).unwrap();
///
/// if !permissions.contains(Permissions::ATTACH_FILES |
/// Permissions::SEND_MESSAGES) {
@@ -512,7 +508,7 @@ impl GuildChannel {
pub fn permissions_for<U: Into<UserId>>(&self, user_id: U) -> Result<Permissions> {
self.guild()
.ok_or_else(|| Error::Model(ModelError::GuildNotFound))
- .map(|g| g.read().unwrap().permissions_for(self.id, user_id))
+ .map(|g| g.read().permissions_for(self.id, user_id))
}
/// Pins a [`Message`] to the channel.
diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs
index 480c15d..b5864fc 100644
--- a/src/model/channel/message.rs
+++ b/src/model/channel/message.rs
@@ -14,6 +14,8 @@ use constants;
use CACHE;
#[cfg(feature = "model")]
use http;
+#[cfg(feature = "model")]
+use utils as serenity_utils;
/// A representation of a message over a guild's text channel, a group, or a
/// private channel.
@@ -97,12 +99,12 @@ impl Message {
///
/// command!(channel_name(_ctx, msg) {
/// let _ = match msg.channel() {
- /// Some(Channel::Category(c)) => msg.reply(&c.read().unwrap().name),
- /// Some(Channel::Group(c)) => msg.reply(&c.read().unwrap().name()),
- /// Some(Channel::Guild(c)) => msg.reply(&c.read().unwrap().name),
+ /// Some(Channel::Category(c)) => msg.reply(&c.read().name),
+ /// Some(Channel::Group(c)) => msg.reply(&c.read().name()),
+ /// Some(Channel::Guild(c)) => msg.reply(&c.read().name),
/// Some(Channel::Private(c)) => {
- /// let channel = c.read().unwrap();
- /// let user = channel.recipient.read().unwrap();
+ /// let channel = c.read();
+ /// let user = channel.recipient.read();
///
/// msg.reply(&format!("DM with {}", user.name.clone()))
/// },
@@ -113,12 +115,12 @@ impl Message {
/// ```
#[cfg(feature = "cache")]
#[inline]
- pub fn channel(&self) -> Option<Channel> { CACHE.read().unwrap().channel(self.channel_id) }
+ pub fn channel(&self) -> Option<Channel> { CACHE.read().channel(self.channel_id) }
/// A util function for determining whether this message was sent by someone else, or the
/// bot.
#[cfg(all(feature = "cache", feature = "utils"))]
- pub fn is_own(&self) -> bool { self.author.id == CACHE.read().unwrap().user.id }
+ pub fn is_own(&self) -> bool { self.author.id == CACHE.read().user.id }
/// Deletes the message.
///
@@ -138,7 +140,7 @@ impl Message {
#[cfg(feature = "cache")]
{
let req = Permissions::MANAGE_MESSAGES;
- let is_author = self.author.id == CACHE.read().unwrap().user.id;
+ let is_author = self.author.id == CACHE.read().user.id;
let has_perms = utils::user_has_perms(self.channel_id, req)?;
if !is_author && !has_perms {
@@ -211,7 +213,7 @@ impl Message {
where F: FnOnce(CreateMessage) -> CreateMessage {
#[cfg(feature = "cache")]
{
- if self.author.id != CACHE.read().unwrap().user.id {
+ if self.author.id != CACHE.read().user.id {
return Err(Error::Model(ModelError::InvalidUser));
}
}
@@ -230,7 +232,7 @@ impl Message {
builder = builder.tts(true);
}
- let map = f(builder).0;
+ let map = serenity_utils::hashmap_to_json_map(f(builder).0);
match http::edit_message(self.channel_id.0, self.id.0, &Value::Object(map)) {
Ok(edited) => {
@@ -335,7 +337,7 @@ impl Message {
#[cfg(feature = "cache")]
pub fn guild(&self) -> Option<Arc<RwLock<Guild>>> {
self.guild_id()
- .and_then(|guild_id| CACHE.read().unwrap().guild(guild_id))
+ .and_then(|guild_id| CACHE.read().guild(guild_id))
}
/// Retrieves the Id of the guild that the message was sent in, if sent in
@@ -345,8 +347,8 @@ impl Message {
/// cache.
#[cfg(feature = "cache")]
pub fn guild_id(&self) -> Option<GuildId> {
- match CACHE.read().unwrap().channel(self.channel_id) {
- Some(Channel::Guild(ch)) => Some(ch.read().unwrap().guild_id),
+ match CACHE.read().channel(self.channel_id) {
+ Some(Channel::Guild(ch)) => Some(ch.read().guild_id),
_ => None,
}
}
@@ -354,7 +356,7 @@ impl Message {
/// True if message was sent using direct messages.
#[cfg(feature = "cache")]
pub fn is_private(&self) -> bool {
- match CACHE.read().unwrap().channel(self.channel_id) {
+ match CACHE.read().channel(self.channel_id) {
Some(Channel::Group(_)) | Some(Channel::Private(_)) => true,
_ => false,
}
diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs
index 07a73d7..ae46805 100644
--- a/src/model/channel/mod.rs
+++ b/src/model/channel/mod.rs
@@ -84,16 +84,16 @@ impl Channel {
pub fn delete(&self) -> Result<()> {
match *self {
Channel::Group(ref group) => {
- let _ = group.read().unwrap().leave()?;
+ let _ = group.read().leave()?;
},
Channel::Guild(ref public_channel) => {
- let _ = public_channel.read().unwrap().delete()?;
+ let _ = public_channel.read().delete()?;
},
Channel::Private(ref private_channel) => {
- let _ = private_channel.read().unwrap().delete()?;
+ let _ = private_channel.read().delete()?;
},
Channel::Category(ref category) => {
- category.read().unwrap().delete()?;
+ category.read().delete()?;
},
}
@@ -386,15 +386,15 @@ impl Display for Channel {
/// [`PrivateChannel`]: struct.PrivateChannel.html
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match *self {
- Channel::Group(ref group) => Display::fmt(&group.read().unwrap().name(), f),
- Channel::Guild(ref ch) => Display::fmt(&ch.read().unwrap().id.mention(), f),
+ Channel::Group(ref group) => Display::fmt(&group.read().name(), f),
+ Channel::Guild(ref ch) => Display::fmt(&ch.read().id.mention(), f),
Channel::Private(ref ch) => {
- let channel = ch.read().unwrap();
- let recipient = channel.recipient.read().unwrap();
+ let channel = ch.read();
+ let recipient = channel.recipient.read();
Display::fmt(&recipient.name, f)
},
- Channel::Category(ref category) => Display::fmt(&category.read().unwrap().name, f),
+ Channel::Category(ref category) => Display::fmt(&category.read().name, f),
}
}
}
diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs
index d6ba781..01b387a 100644
--- a/src/model/channel/private_channel.rs
+++ b/src/model/channel/private_channel.rs
@@ -279,6 +279,6 @@ impl PrivateChannel {
impl Display for PrivateChannel {
/// Formats the private channel, displaying the recipient's username.
fn fmt(&self, f: &mut Formatter) -> FmtResult {
- f.write_str(&self.recipient.read().unwrap().name)
+ f.write_str(&self.recipient.read().name)
}
}
diff --git a/src/model/channel/reaction.rs b/src/model/channel/reaction.rs
index 8edc2e9..1a40ecb 100644
--- a/src/model/channel/reaction.rs
+++ b/src/model/channel/reaction.rs
@@ -48,7 +48,7 @@ impl Reaction {
pub fn delete(&self) -> Result<()> {
let user_id = feature_cache! {
{
- let user = if self.user_id == CACHE.read().unwrap().user.id {
+ let user = if self.user_id == CACHE.read().user.id {
None
} else {
Some(self.user_id.0)