Skip to content

Commit

Permalink
Handle SEND_MESSAGES_IN_THREADS in Message::author_permissions (#3039)
Browse files Browse the repository at this point in the history
See doc comment.
  • Loading branch information
GnomedDev authored Nov 15, 2024
1 parent 9549110 commit 7cf9921
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ impl Message {

/// Calculates the permissions of the message author in the current channel.
///
/// This handles the [`Permissions::SEND_MESSAGES_IN_THREADS`] permission for threads, setting
/// [`Permissions::SEND_MESSAGES`] accordingly if this message was sent in a thread.
///
/// This may return `None` if:
/// - The [`Cache`] does not have the current [`Guild`]
/// - The [`Guild`] does not have the current channel cached (should never happen).
Expand All @@ -230,19 +233,25 @@ impl Message {
};

let guild = cache.as_ref().guild(guild_id)?;
let channel = if let Some(channel) = guild.channels.get(&self.channel_id) {
channel
let (channel, is_thread) = if let Some(channel) = guild.channels.get(&self.channel_id) {
(channel, false)
} else if let Some(thread) = guild.threads.iter().find(|th| th.id == self.channel_id) {
thread
(thread, true)
} else {
return None;
};

if let Some(member) = &self.member {
Some(guild.partial_member_permissions_in(channel, self.author.id, member))
let mut permissions = if let Some(member) = &self.member {
guild.partial_member_permissions_in(channel, self.author.id, member)
} else {
Some(guild.user_permissions_in(channel, guild.members.get(&self.author.id)?))
guild.user_permissions_in(channel, guild.members.get(&self.author.id)?)
};

if is_thread {
permissions.set(Permissions::SEND_MESSAGES, permissions.send_messages_in_threads());
}

Some(permissions)
}

/// Deletes the message.
Expand Down

0 comments on commit 7cf9921

Please sign in to comment.