forked from kangalio/rustbot
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to pin message to a thread (#37)
Closes #8, currently this bypasses any checks for moderators, although I can remove that if wanted.
- Loading branch information
Showing
4 changed files
with
82 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ pub mod crates; | |
pub mod godbolt; | ||
pub mod modmail; | ||
pub mod playground; | ||
pub mod thread_pin; | ||
pub mod utilities; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use anyhow::Result; | ||
|
||
use poise::serenity_prelude as serenity; | ||
|
||
use crate::types::Context; | ||
|
||
enum ThreadPinError { | ||
NoChannel, | ||
NotThread, | ||
ThreadLocked, | ||
NotThreadOwner, | ||
} | ||
|
||
async fn can_pin_in_thread(ctx: Context<'_>) -> Result<(), ThreadPinError> { | ||
if crate::checks::is_moderator(ctx) { | ||
return Ok(()); | ||
} | ||
|
||
let Some(channel) = ctx.guild_channel().await else { | ||
return Err(ThreadPinError::NoChannel); | ||
}; | ||
|
||
let Some(thread_metadata) = channel.thread_metadata else { | ||
return Err(ThreadPinError::NotThread); | ||
}; | ||
|
||
if thread_metadata.locked { | ||
return Err(ThreadPinError::ThreadLocked); | ||
} | ||
|
||
if channel.owner_id != Some(ctx.author().id) { | ||
return Err(ThreadPinError::NotThreadOwner); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[poise::command(context_menu_command = "Pin Message to Thread", guild_only)] | ||
pub async fn thread_pin(ctx: Context<'_>, message: serenity::Message) -> Result<()> { | ||
let reply = match can_pin_in_thread(ctx).await { | ||
Ok(()) => { | ||
message.pin(ctx.serenity_context()).await?; | ||
"Pinned message to your thread!" | ||
} | ||
Err(ThreadPinError::NoChannel) => "Error: Cannot fetch any information about this channel!", | ||
Err(ThreadPinError::NotThread) => "This channel is not a thread!", | ||
Err(ThreadPinError::NotThreadOwner) => { | ||
"You did not create this thread, so cannot pin messages to it." | ||
} | ||
Err(ThreadPinError::ThreadLocked) => { | ||
"This thread has been locked, so this cannot be performed." | ||
} | ||
}; | ||
|
||
let reply = poise::CreateReply::default().content(reply).ephemeral(true); | ||
ctx.send(reply).await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters