Add patreon-sharing option
This commit is contained in:
		
							
								
								
									
										64
									
								
								src/utils.rs
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								src/utils.rs
									
									
									
									
									
								
							@@ -12,7 +12,58 @@ use crate::{
 | 
			
		||||
    ApplicationContext, Context, Error,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub async fn check_subscription(cache_http: impl CacheHttp, user_id: impl Into<UserId>) -> bool {
 | 
			
		||||
/// Check if this user/guild combination should be considered subscribed.
 | 
			
		||||
/// If the guild has a patreon linked, check the user involved in the link.
 | 
			
		||||
/// Otherwise, check the user and the guild's owner
 | 
			
		||||
pub async fn check_subscription(
 | 
			
		||||
    ctx: &Context<'_>,
 | 
			
		||||
    user_id: UserId,
 | 
			
		||||
    guild_id: Option<GuildId>,
 | 
			
		||||
) -> bool {
 | 
			
		||||
    if let Some(subscription_guild) = *CNC_GUILD {
 | 
			
		||||
        let user_subscribed = check_user_subscription(ctx, user_id).await;
 | 
			
		||||
 | 
			
		||||
        let owner_subscribed = match guild_id {
 | 
			
		||||
            Some(guild_id) => {
 | 
			
		||||
                if let Some(owner) = ctx.cache().unwrap().guild(guild_id).map(|g| g.owner_id) {
 | 
			
		||||
                    check_user_subscription(ctx, owner).await
 | 
			
		||||
                } else {
 | 
			
		||||
                    false
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            None => false,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let link_subscribed = match guild_id {
 | 
			
		||||
            Some(guild_id) => {
 | 
			
		||||
                if let Ok(row) = sqlx::query!(
 | 
			
		||||
                    "SELECT user_id FROM patreon_link WHERE user_id = ?",
 | 
			
		||||
                    guild_id.get()
 | 
			
		||||
                )
 | 
			
		||||
                .fetch_one(&ctx.data().database)
 | 
			
		||||
                .await
 | 
			
		||||
                {
 | 
			
		||||
                    check_user_subscription(ctx, row.user_id).await
 | 
			
		||||
                } else {
 | 
			
		||||
                    false
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            None => false,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        user_subscribed || owner_subscribed || link_subscribed
 | 
			
		||||
    } else {
 | 
			
		||||
        true
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Check a user's subscription status, ignoring Patreon linkage
 | 
			
		||||
pub async fn check_user_subscription(
 | 
			
		||||
    cache_http: impl CacheHttp,
 | 
			
		||||
    user_id: impl Into<UserId>,
 | 
			
		||||
) -> bool {
 | 
			
		||||
    if let Some(subscription_guild) = *CNC_GUILD {
 | 
			
		||||
        let guild_member = GuildId::new(subscription_guild).member(cache_http, user_id).await;
 | 
			
		||||
 | 
			
		||||
@@ -30,17 +81,6 @@ pub async fn check_subscription(cache_http: impl CacheHttp, user_id: impl Into<U
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn check_guild_subscription(
 | 
			
		||||
    cache_http: impl CacheHttp,
 | 
			
		||||
    guild_id: impl Into<GuildId>,
 | 
			
		||||
) -> bool {
 | 
			
		||||
    if let Some(owner) = cache_http.cache().unwrap().guild(guild_id).map(|g| g.owner_id) {
 | 
			
		||||
        check_subscription(&cache_http, owner).await
 | 
			
		||||
    } else {
 | 
			
		||||
        false
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn reply_to_interaction_response_message(
 | 
			
		||||
    reply: CreateReply,
 | 
			
		||||
) -> CreateInteractionResponseMessage {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user