Dough Git

clove/allInOne

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::{Bot, discord};

const USAGE: &str = "Use `{prefix}fban <@user or user ID> <reason>`.";

impl Bot {
    pub(super) async fn fake_ban(
        &self,
        ctx: &Context,
        message: &Message,
        input: &str,
    ) -> anyhow::Result<Option<String>> {
        let Some((target, reason)) = input.split_once(char::is_whitespace) else {
            return Ok(Some(self.command_text(USAGE)));
        };
        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(self.command_text(USAGE)));
        }

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