Skip to content

Commit

Permalink
Handle message type 7 (member join)
Browse files Browse the repository at this point in the history
When message type 7 is received from the gateway, transform the content
if the type is 7 to a proper greeting.

Additionally, when the type is 6, provide a proper content notifying that a
user has pinned a message to the channel.

These transformations are not done at REST-level when retrieving messages, and
are instead done in `ChannelId::message` and `ChannelId::messages`.
  • Loading branch information
fwrs authored and Zeyla Hellyer committed May 22, 2017
1 parent 302d771 commit 8f88c6b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/client/dispatch.rs
Expand Up @@ -108,10 +108,14 @@ pub fn dispatch(event: Event,
}

fn dispatch_message(context: Context,
message: Message,
mut message: Message,
event_store: &Arc<RwLock<EventStore>>) {
if let Some(handler) = handler!(on_message, event_store) {
thread::spawn(move || (handler)(context, message));
thread::spawn(move || {
message.transform_content();

(handler)(context, message);
});
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/constants.rs
Expand Up @@ -12,6 +12,32 @@ pub const MESSAGE_CODE_LIMIT: u16 = 2000;
/// [UserAgent]: ../hyper/header/struct.UserAgent.html
pub const USER_AGENT: &'static str = concat!("DiscordBot (https://github.com/zeyla/serenity, ", env!("CARGO_PKG_VERSION"), ")");

/// List of messages Discord shows on member join.
pub static JOIN_MESSAGES: &'static [&'static str] = &[
"$user just joined the server - glhf!",
"$user just joined. Everyone, look busy!",
"$user just joined. Can I get a heal?",
"$user joined your party.",
"$user joined. You must construct additional pylons.",
"Ermagherd. $user is here.",
"Welcome, $user. Stay awhile and listen.",
"Welcome, $user. We were expecting you ( ͡° ͜ʖ ͡°)",
"Welcome, $user. We hope you brought pizza.",
"Welcome $user. Leave your weapons by the door.",
"A wild $user appeared.",
"Swoooosh. $user just landed.",
"Brace yourselves. $user just joined the server.",
"$user just joined. Hide your bananas.",
"$user just arrived. Seems OP - please nerf.",
"$user just slid into the server.",
"A $user has spawned in the server.",
"Big $user showed up!",
"Where’s $user? In the server!",
"$user hopped into the server. Kangaroo!!",
"$user just showed up. Hold my beer."
];


#[allow(dead_code)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ErrorCode {
Expand Down
12 changes: 12 additions & 0 deletions src/model/channel/channel_id.rs
Expand Up @@ -255,6 +255,11 @@ impl ChannelId {
#[inline]
pub fn message<M: Into<MessageId>>(&self, message_id: M) -> Result<Message> {
rest::get_message(self.0, message_id.into().0)
.map(|mut msg| {
msg.transform_content();

msg
})
}

/// Gets messages from the channel.
Expand All @@ -279,6 +284,13 @@ impl ChannelId {
}

rest::get_messages(self.0, &query)
.map(|msgs| msgs
.into_iter()
.map(|mut msg| {
msg.transform_content();

msg
}).collect::<Vec<Message>>())
}

/// Pins a [`Message`] to the channel.
Expand Down
25 changes: 25 additions & 0 deletions src/model/channel/message.rs
@@ -1,4 +1,5 @@
use std::mem;
use time;
use ::constants;
use ::client::rest;
use ::model::*;
Expand Down Expand Up @@ -189,6 +190,28 @@ impl Message {
}
}

#[doc(hidden)]
pub fn transform_content(&mut self) {
match self.kind {
MessageType::PinsAdd => {
self.content = format!("{} pinned a message to this channel. See all the pins.", self.author);
},
MessageType::MemberJoin => {
if let Ok(tm) = time::strptime(&self.timestamp, "%Y-%m-%dT%H:%M:%S") {
let sec = tm.to_timespec().sec as usize;
let chosen = constants::JOIN_MESSAGES[sec % constants::JOIN_MESSAGES.len()];

self.content = if chosen.contains("$user") {
chosen.replace("$user", &self.author.mention())
} else {
chosen.to_owned()
};
}
},
_ => {},
}
}

/// Returns message content, but with user and role mentions replaced with
/// names and everyone/here mentions cancelled.
#[cfg(feature="cache")]
Expand Down Expand Up @@ -550,6 +573,8 @@ enum_number!(
GroupIconUpdate = 5,
/// An indicator that a message was pinned by the author.
PinsAdd = 6,
/// An indicator that a member joined the guild.
MemberJoin = 7,
}
);

Expand Down
23 changes: 23 additions & 0 deletions tests/resources/message_type_7.json
@@ -0,0 +1,23 @@
{
"type": 7,
"tts": false,
"timestamp": "2017-01-01T12:00:00.000000+00:00",
"pinned": false,
"nonce": null,
"mentions": [],
"mention_roles": [],
"mention_everyone": false,
"id": "123132131321321",
"embeds": [],
"edited_timestamp": null,
"content": "",
"channel_id": "321",
"author": {
"username": "aaa",
"id": "123",
"discriminator": "0123",
"bot": true,
"avatar": "u9aosdu9anbsduas8"
},
"attachments": []
}
5 changes: 5 additions & 0 deletions tests/test_deser.rs
Expand Up @@ -156,3 +156,8 @@ fn voice_state_update() {
fn webhooks_update() {
p!(WebhookUpdateEvent, "webhooks_update_1");
}

#[test]
fn message_type_7() {
p!(MessageCreateEvent, "message_type_7");
}

0 comments on commit 8f88c6b

Please sign in to comment.