Skip to content

Commit

Permalink
user authorized message
Browse files Browse the repository at this point in the history
  • Loading branch information
cycle-five committed Mar 1, 2024
1 parent a785d7d commit 4102156
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
33 changes: 24 additions & 9 deletions crack-core/src/commands/admin/authorize.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::errors::CrackedError;
use crate::guild::settings::GuildSettings;
use crate::utils::check_reply;
use crate::messaging::message::CrackedMessage;
use crate::utils::send_response_poise;
use crate::Context;
use crate::Error;
use poise::CreateReply;
use poise::serenity_prelude::UserId;

/// Authorize a user to use the bot.
#[poise::command(prefix_command, owners_only, ephemeral)]
Expand Down Expand Up @@ -41,13 +42,27 @@ pub async fn authorize(
})?;
guild_settings.save(&pool).await?;

check_reply(
ctx.send(
CreateReply::default()
.content("User authorized.")
.reply(true),
)
.await,
let user_id = UserId::new(id);
let user_name = ctx

Check warning on line 46 in crack-core/src/commands/admin/authorize.rs

View check run for this annotation

Codecov / codecov/patch

crack-core/src/commands/admin/authorize.rs#L45-L46

Added lines #L45 - L46 were not covered by tests
.http()
.get_user(user_id)
.await
.map(|u| u.name)
.unwrap_or_else(|_| "Unknown".to_string());
let guild_name = guild_id
.to_partial_guild(ctx.http())
.await
.map(|g| g.name)
.unwrap_or_else(|_| "Unknown".to_string());

Check warning on line 56 in crack-core/src/commands/admin/authorize.rs

View check run for this annotation

Codecov / codecov/patch

crack-core/src/commands/admin/authorize.rs#L48-L56

Added lines #L48 - L56 were not covered by tests
send_response_poise(
ctx,
CrackedMessage::UserAuthorized {
user_id,
user_name,
guild_id,

Check warning on line 62 in crack-core/src/commands/admin/authorize.rs

View check run for this annotation

Codecov / codecov/patch

crack-core/src/commands/admin/authorize.rs#L58-L62

Added lines #L58 - L62 were not covered by tests
guild_name,
},
true,
);

Ok(())
Expand Down
15 changes: 15 additions & 0 deletions crack-core/src/messaging/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ pub enum CrackedMessage {
channel_id: serenity::ChannelId,
channel_name: String,
},
UserAuthorized {
user_id: UserId,
user_name: String,
guild_id: serenity::GuildId,
guild_name: String,
},
UserTimeout {
user: String,
user_id: String,
Expand Down Expand Up @@ -224,6 +230,15 @@ impl Display for CrackedMessage {
CATEGORY_CREATED, channel_id, channel_name
)),
Self::WaybackSnapshot { url } => f.write_str(&format!("{} {}", WAYBACK_SNAPSHOT, url)),
Self::UserAuthorized {

Check warning on line 233 in crack-core/src/messaging/message.rs

View check run for this annotation

Codecov / codecov/patch

crack-core/src/messaging/message.rs#L233

Added line #L233 was not covered by tests
user_id,
user_name,
guild_id,
guild_name,
} => f.write_str(&format!(
"{}\n User: {} ({}) Guild: {} ({})",
AUTHORIZED, user_name, user_id, guild_name, guild_id
)),
Self::UserTimeout {
user: _,
user_id,
Expand Down
3 changes: 2 additions & 1 deletion crack-core/src/messaging/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub const CLEARED: &str = "🗑️ Cleared!";
pub const CLEANED: &str = "🗑️ Messages Cleaned: ";
pub const CHANNEL_DELETED: &str = "🗑️ Deleted channel!";

pub const AUTHORIZED: &str = "✅ User has been authorized.";
pub const BANNED: &str = "Banned";
pub const UNBANNED: &str = "Unbanned";
pub const DEAFENED: &str = "Deafened";
Expand Down Expand Up @@ -40,7 +41,7 @@ pub const FAIL_PARSE_TIME: &str = "⚠️ Failed to parse time, speak English
pub const FAIL_PLAYLIST_FETCH: &str = "⚠️ Failed to fetch playlist!";
pub const FAIL_INVALID_IP: &str = "⚠️ Invalid IP address!";
pub const GUILD_ONLY: &str = "⚠️ This command can only be used in a server!";
pub const IDLE_ALERT: &str = "⚠️ I've been idle for a while, pay for premium if you want me to idle indefinitely plus better audio and more!\n[CrackTunes Patreon](https://patreon.com/CrackTunes)";
pub const IDLE_ALERT: &str = "⚠️ I've been idle for a while, pay for premium if you want to stay, plus better audio and more!\n[CrackTunes Patreon](https://patreon.com/CrackTunes)";
pub const IP_DETAILS: &str = "🌐 IP details for";
pub const JOINING: &str = "Joining";
pub const LEAVING: &str = "👋 See you soon!";
Expand Down
2 changes: 2 additions & 0 deletions crack-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,8 @@ pub fn check_msg(result: Result<Message, Error>) {
}
}

#[cfg(not(tarpaulin_include))]
/// Takes a Result ReplyHandle and logs the error if it's an Err.
pub fn check_reply(result: Result<ReplyHandle, SerenityError>) {
if let Err(why) = result {
tracing::error!("Error sending message: {:?}", why);
Expand Down

0 comments on commit 4102156

Please sign in to comment.