projects/bot/src/general-settings.ts

import { Clock, Effect, Schema, Struct } from "effect"
import { errors, Permissions, type BotEventContext, type Client } from "@neontechspace/fluxerly/effect"
import {
    GeneralGetResult,
    GeneralManageResult,
    GeneralNicknameRecordResult,
    GeneralNicknameSetResult,
    validNickname,
    validPrefix,
    type GeneralGetRequest,
    type GeneralManageRequest,
    type GeneralNickname,
    type GeneralNicknameRequest,
    type GeneralNicknameResultRequest,
} from "@neonflux/contracts/general"
import type { CommandRule, CommandRules } from "@neonflux/contracts/command-rules"
import type { ModerationActor } from "@neonflux/contracts/shared"
import { readSafetyAuthority } from "./safety-permissions.ts"
import { BackendRequestError, createBackendRequest } from "./backend-http.ts"
import type { BackendConfig } from "./config.ts"
import { sourceTimestamp } from "./responses.ts"
import { code, notSetUp, replyCard } from "./reply-style.ts"
import { warnFailure } from "./contained.ts"

// The prefix reader and the nickname command each decode their own part of the general settings
const settings = GeneralGetResult.mapFields(Struct.pick(["prefix", "replyStyle", "revision", "commandRules"])),
    nickname = GeneralGetResult.mapFields(Struct.pick(["nickname"]))
export function createGeneralSettingsStore(backend: BackendConfig, serverId: string) {
    const request = createBackendRequest(backend)
    return {
        get: () =>
            request("/general/get", { serverId } satisfies GeneralGetRequest).pipe(
                Effect.flatMap(Schema.decodeUnknownEffect(settings)),
            ),
        set: (actorId: string, change: { prefix: string } | { replyStyle: ReplyStyle }, expectedRevision: number) =>
            request("/general/manage", {
                serverId,
                originServerId: serverId,
                actorId,
                managerAuthorized: true,
                ...change,
                expectedRevision,
            } satisfies GeneralManageRequest).pipe(Effect.flatMap(Schema.decodeUnknownEffect(GeneralManageResult))),
        nickname: () =>
            request("/general/get", { serverId } satisfies GeneralGetRequest).pipe(
                Effect.flatMap(Schema.decodeUnknownEffect(nickname)),
                Effect.map((value): GeneralNickname => value.nickname),
            ),
        setNickname: (actorId: string, value: string | null, createdAt: number) =>
            request("/general/nickname", {
                serverId,
                originServerId: serverId,
                actorId,
                managerAuthorized: true,
                createdAt,
                nickname: value,
            } satisfies GeneralNicknameRequest).pipe(
                Effect.flatMap(Schema.decodeUnknownEffect(GeneralNicknameSetResult)),
            ),
        recordNickname: (nicknameRevision: number, value: string | null, result: NicknameOutcome) =>
            request("/general/nickname-result", {
                serverId,
                originServerId: serverId,
                revision: nicknameRevision,
                nickname: value,
                ...result,
            } satisfies GeneralNicknameResultRequest).pipe(
                Effect.flatMap(Schema.decodeUnknownEffect(GeneralNicknameRecordResult)),
            ),
    }
}
export type GeneralSettingsStore = ReturnType<typeof createGeneralSettingsStore>

export type NicknameOutcome = { state: "applied" } | { state: "failed"; error: string }
/** Names the fix the way other refusals do */
export const missingNicknamePermission = "Grant Change own nickname to the NeonFlux role. Fluxer kept the old nickname"
/**
 * Set or clear the bot's own nickname. Without Change own nickname, Fluxer reports success and keeps the old nickname,
 * so only a returned nickname equal to the requested one counts as applied
 */
export function applyNativeNickname(client: Client, serverId: string, value: string | null) {
    return client.members.editSelf(serverId, { nickname: value }).pipe(
        Effect.map((member): NicknameOutcome =>
            member.guildId !== serverId || member.nickname === undefined
                ? { state: "failed", error: "Fluxer did not confirm the nickname change" }
                : member.nickname === value
                  ? { state: "applied" }
                  : { state: "failed", error: missingNicknamePermission },
        ),
        Effect.catch((error): Effect.Effect<NicknameOutcome> =>
            Effect.succeed({
                state: "failed",
                error:
                    error._tag === "GuildOperationError" && error.reason === "rejected" && error.status === 403
                        ? missingNicknamePermission
                        : "Fluxer did not confirm the nickname change",
            }),
        ),
    )
}

// Each managed server's prefix, reply style and command rules in bot memory. Chat changes apply at once and dashboard changes within the TTL
export const prefixTtlMs = 30000
export type ReplyStyle = "embed" | "text"
type General = {
    readonly prefix: string
    readonly replyStyle: ReplyStyle
    readonly commandRules?: CommandRules | undefined
}
const prefixes = new Map<string, { -readonly [K in keyof General]: General[K] } & { readAt: number }>()
const remember = (serverId: string, { prefix, replyStyle, commandRules }: General) =>
    Clock.currentTimeMillis.pipe(
        Effect.map((readAt) => {
            prefixes.set(serverId, { prefix, replyStyle, commandRules, readAt })
        }),
    )
/** The rule on a command, from memory, so a command without one costs nothing */
export const serverCommandRule = (serverId: string, command: string | undefined): CommandRule | undefined =>
    prefixes.get(serverId)?.commandRules?.find((rule) => rule.command === command)
/** Keep the rules a command just read or saved, keeping when the prefix and reply style were last read */
export const rememberCommandRules = (serverId: string, commandRules: CommandRules) =>
    Effect.sync(() => {
        const cached = prefixes.get(serverId)
        if (cached) cached.commandRules = commandRules
    })

/** The prefix that commands in a reply to this message use. Private DMs keep the fixed ! that also selects a server */
export const replyPrefix = (serverId: string, guildId: string | undefined) =>
    guildId === serverId ? (prefixes.get(serverId)?.prefix ?? "!") : "!"
/** The server's last known reply style, in its channels and in DMs about it. Embeds until one is read */
export const serverReplyStyle = (serverId: string) => prefixes.get(serverId)?.replyStyle ?? "embed"

/** Print bot-authored usage text with a reply's prefix. Lines that send the reader to a DM keep the fixed ! */
export const withPrefix = (text: string, prefix: string) =>
    prefix === "!"
        ? text
        : text
              .split("\n")
              .map((line) => (/\bDM\b/.test(line) ? line : line.replace(/(?<![\w!])!(?=[a-z])/g, () => prefix)))
              .join("\n")

/** Forget a retired server's prefix and reply style, so memory holds only servers this process serves */
export const forgetPrefix = (serverId: string) => {
    prefixes.delete(serverId)
}

/** Make the next command read a server's prefix and reply style again, keeping the last known ones until it does */
export const refreshPrefix = (serverId: string) => {
    const cached = prefixes.get(serverId)
    if (cached) cached.readAt = Number.NEGATIVE_INFINITY
}

/** Start one server's prefix and reply style memory for a new bot run and return its prefix reader, which refreshes both */
export function createPrefixReader(store: GeneralSettingsStore | undefined, serverId: string) {
    prefixes.delete(serverId)
    return Effect.gen(function* () {
        if (!store) return "!"
        const cached = prefixes.get(serverId)
        if (cached && (yield* Clock.currentTimeMillis) - cached.readAt < prefixTtlMs) return cached.prefix
        // A failed refresh keeps the last known settings, or ! and embeds without them, for another TTL
        const value = yield* store
            .get()
            .pipe(
                Effect.catch((error) =>
                    warnFailure(
                        "The configured prefix could not be read. Commands use the last known prefix or !",
                        error,
                    ).pipe(Effect.as<General>(cached ?? { prefix: "!", replyStyle: "embed" })),
                ),
            )
        yield* remember(serverId, value)
        return value.prefix
    })
}

export function handlePrefixCommand(
    store: GeneralSettingsStore | undefined,
    serverId: string,
    args: readonly string[],
    context: BotEventContext<"messageCreate">,
) {
    const respond = (content: string) => context.reply({ content })
    return Effect.gen(function* () {
        if (!store) {
            yield* respond(notSetUp("The prefix setting"))
            return
        }
        if (context.message.guildId !== serverId) return
        const current = yield* store.get()
        yield* remember(serverId, current)
        if (!args.length) {
            yield* respond(`Current prefix: ${current.prefix}`)
            return
        }
        if (args.length !== 1 || !validPrefix(args[0])) {
            yield* respond(`Use ${code(`${current.prefix}prefix <one to five punctuation characters>`)}`)
            return
        }
        const manager = yield* readServerManagerAuthority(context.client, serverId, context.message.author.id)
        if (!manager) {
            yield* respond(
                manager === undefined
                    ? managerReadFailed
                    : "Only the server owner or members with Manage community can change the prefix",
            )
            return
        }
        const saved = yield* store.set(context.message.author.id, { prefix: args[0]! }, current.revision)
        if (saved.saved) yield* remember(serverId, { ...current, prefix: args[0]! })
        yield* respond(
            saved.saved
                ? `Prefix changed to ${args[0]}`
                : "The prefix changed while this command ran. Check the current prefix and try again",
        )
    })
}

const styleNames = { embed: "embeds", text: "plain text" } as const
/** Show or change whether status, detail and list replies use embeds or plain text, with the same access rule as the prefix */
export function handleRepliesCommand(
    store: GeneralSettingsStore | undefined,
    serverId: string,
    args: readonly string[],
    context: BotEventContext<"messageCreate">,
) {
    const respond = (content: string) => context.reply({ content })
    return Effect.gen(function* () {
        if (!store) {
            yield* respond(notSetUp("The reply style setting"))
            return
        }
        if (context.message.guildId !== serverId) return
        const current = yield* store.get(),
            style = args[0]?.toLowerCase(),
            choices = `${code(`${current.prefix}replies embed`)} or ${code(`${current.prefix}replies text`)}`
        yield* remember(serverId, current)
        if (!args.length) {
            yield* respond(
                `Replies use ${styleNames[current.replyStyle]}. Server managers can change it with ${choices}`,
            )
            return
        }
        if (args.length !== 1 || (style !== "embed" && style !== "text")) {
            yield* respond(`Use ${choices}`)
            return
        }
        const manager = yield* readServerManagerAuthority(context.client, serverId, context.message.author.id)
        if (!manager) {
            yield* respond(
                manager === undefined
                    ? managerReadFailed
                    : "Only the server owner or members with Manage community can change the reply style",
            )
            return
        }
        const saved = yield* store.set(context.message.author.id, { replyStyle: style }, current.revision)
        if (saved.saved) yield* remember(serverId, { ...current, replyStyle: style })
        yield* respond(
            saved.saved
                ? `Replies now use ${styleNames[style]}`
                : "The general settings changed while this command ran. Check the current reply style and try again",
        )
    })
}

/** The answer when a member's permissions could not be read, which says nothing about what they hold */
export const managerReadFailed = "Your permissions could not be read right now. Try again shortly"
/**
 * Server settings follow one rule: the server owner, Administrator or Manage community, read fresh from Fluxer. The owner holds every
 * permission. Undefined means the read failed, as when it timed out, so the member is told to try again rather than refused. Someone
 * Fluxer does not find as a member holds nothing
 */
export function readServerManagerAuthority(client: Client, serverId: string, userId: string) {
    return client.permissions.fetch({ guildId: serverId, userId }, { timeoutMs: 5000 }).pipe(
        Effect.map(
            (bits): boolean | undefined => (bits & (Permissions.Administrator | Permissions.ManageGuild)) !== 0n,
        ),
        Effect.catch((error) => Effect.succeed(errors.isNotFound(error) ? false : undefined)),
    )
}

/** The same rule with the fresh reads a backend change carries, optionally with one channel of this server */
export function readServerManager(client: Client, serverId: string, userId: string, channelId?: string) {
    return readSafetyAuthority(client, serverId, userId, channelId ? { channelId } : {}).pipe(
        Effect.map((authority) => ({
            authority,
            actor: {
                originServerId: authority.guild.id,
                userId,
                roleIds: authority.roleIds,
                isOwner: authority.isOwner,
                isAdministrator: authority.isAdmin,
                nativePermissionAuthorized: true,
            } satisfies ModerationActor,
            manager:
                authority.isOwner ||
                (client.permissions.calculate({
                    guild: authority.guild,
                    member: authority.actor,
                    roles: authority.roles,
                }) &
                    (Permissions.Administrator | Permissions.ManageGuild)) !==
                    0n,
        })),
    )
}

const nicknameUsage = (prefix: string) =>
    `Use ${code(`${prefix}nickname`)} to show the bot's nickname, ${code(`${prefix}nickname set <name>`)} with 1 to 32 characters, or ${code(`${prefix}nickname reset`)} to show the bot's username`

/** Set, reset or show the bot's nickname in this server. Changes apply at once and the result is recorded for the website */
export function handleNicknameCommand(
    store: GeneralSettingsStore | undefined,
    serverId: string,
    args: readonly string[],
    context: BotEventContext<"messageCreate">,
) {
    const respond = (content: string) => context.reply({ content })
    return Effect.gen(function* () {
        if (!store) {
            yield* respond(notSetUp("The nickname setting"))
            return
        }
        if (context.message.guildId !== serverId) return
        const verb = args[0]?.toLowerCase()
        if (!args.length) {
            const current = yield* store.nickname(),
                result = current.result
            yield* replyCard(context, serverId, {
                title: "Bot nickname",
                fields: [
                    ["Nickname", current.nickname ?? "None, so the bot's username shows"],
                    ...(result
                        ? [
                              [
                                  "Last change",
                                  result.state === "pending"
                                      ? "Not confirmed yet"
                                      : result.state === "applied"
                                        ? "Applied"
                                        : `Failed. ${result.error ?? "Fluxer did not confirm the nickname change"}`,
                              ] as const,
                          ]
                        : []),
                ],
            })
            return
        }
        const name = verb === "set" ? args.slice(1).join(" ") : undefined
        if (!(
            (verb === "set" && name !== undefined && validNickname(name)) ||
            (verb === "reset" && args.length === 1)
        )) {
            yield* respond(nicknameUsage(replyPrefix(serverId, context.message.guildId)))
            return
        }
        const manager = yield* readServerManagerAuthority(context.client, serverId, context.message.author.id)
        if (!manager) {
            yield* respond(
                manager === undefined
                    ? managerReadFailed
                    : "Only the server owner or members with Manage community can change the bot nickname",
            )
            return
        }
        const value = verb === "set" ? name! : null
        const saved = yield* store
            .setNickname(context.message.author.id, value, yield* sourceTimestamp(context.message))
            .pipe(
                Effect.map((saved) => saved.revision),
                Effect.catchIf(
                    (error): error is BackendRequestError =>
                        error instanceof BackendRequestError && error.status === 409,
                    () => Effect.succeed(undefined),
                ),
            )
        if (saved === undefined) {
            yield* respond(
                "The nickname changed on the website while this command ran. Check the current nickname and try again",
            )
            return
        }
        const result = yield* applyNativeNickname(context.client, serverId, value)
        yield* store
            .recordNickname(saved, value, result)
            .pipe(Effect.catch((error) => warnFailure("The nickname result could not be recorded", error)))
        yield* respond(
            result.state === "failed"
                ? `The nickname was not applied. ${result.error}`
                : value === null
                  ? "Bot nickname reset. The bot's username is shown"
                  : `Bot nickname set to ${value}`,
        )
    })
}