Dough Git

clove/proxyBot

src/commands/fban/mod.rs

back to src/commands/fban

// src/commands/fban/mod.rs

use serde_json::json;
use serenity::all::{Context, Message};

use crate::discord;

const USAGE: &str = "Use `d$fban <@user or user ID> <reason>`.";

pub(super) async fn run(
    ctx: &Context,
    message: &Message,
    input: &str,
) -> anyhow::Result<Option<String>> {
    let Some((target, reason)) = input.split_once(char::is_whitespace) else {
        return Ok(Some(USAGE.into()));
    };
    let reason = reason.trim_start();

    let user_id = target
    .strip_prefix("<@")
    .and_then(|mention| mention.strip_suffix('>'))
    .map_or(target, |mention| {
        mention.strip_prefix('!').unwrap_or(mention)
        });

    if !user_id.bytes().all(|byte| byte.is_ascii_digit())
    || !user_id.parse::<u64>().is_ok_and(|id| id > 0)
    || reason.is_empty()
    {
        return Ok(Some(USAGE.into()));
    }

    discord::reply(
        ctx,
        message,
        json!({
            "embeds": [{
                "title": "🔨 User fake-banned",
                "description": reason,
                "color": 0xED4245,
                "fields": [{
                    "name": "User",
                    "value": format!("<@{user_id}>"),
                }],
            }],
        }),
    )
    .await?;
    Ok(None)
}