jackwener/OpenCLI · error · CommandExecutionError

Bilibili user search returned malformed mid for @${name}

Error message

Bilibili user search returned malformed mid for @${name}

What it means

@mentions are resolved to user ids (mid) via resolveUid (user search). If the search returns a value that is not a positive integer, the CLI throws CommandExecutionError; EmptyResultError (user not found) is tolerated and skipped, but other errors propagate.

Source

Thrown at clis/bilibili/comment.js:68

        // Resolve bvid → aid (the reply API addresses videos by aid, as `oid`)
        const view = await apiGet(page, '/x/web-interface/view', { params: { bvid } });
        const viewData = requireOkPayload(view, 'view');
        const oid = viewData?.aid;
        if (!oid)
            throw new CommandExecutionError(`Cannot resolve aid for bvid: ${bvid}`);
        // Resolve @username mentions to uids. Bilibili only turns "@name" into a real
        // mention — one that notifies the mentioned user — when the request carries
        // at_name_to_mid; a plain-text "@name" is otherwise inert and notifies nobody.
        /** @type {Record<string, number>} */
        const atNameToMid = {};
        for (const match of message.matchAll(/@([^\s@]+)/g)) {
            const name = match[1];
            if (name in atNameToMid)
                continue;
            try {
                const mid = Number(await resolveUid(page, name));
                if (!Number.isInteger(mid) || mid <= 0) {
                    throw new CommandExecutionError(`Bilibili user search returned malformed mid for @${name}`);
                }
                atNameToMid[name] = mid;
            }
            catch (error) {
                if (!(error instanceof EmptyResultError)) {
                    throw error;
                }
                // Unresolvable @name (typo, or not a user) — leave it as plain text.
            }
        }
        // For a reply, Bilibili needs both `root` (top-level comment) and `parent`.
        // Replying to a top-level comment means root === parent.
        const params = {
            oid,
            type: 1,
            message,
            plat: 1,
            ...(parent != null

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Verify the @username exists via bilibili search and matches exactly
  2. Remove or correct the @mention in --message
  3. Retry after a short delay if the search API was rate-limiting
  4. Confirm resolveUid works standalone for the problem username

Example fix

// before
--message "thanks @old_name"
// after
--message "thanks @current_name"  // verified via search
Defensive patterns

Strategy: fallback

Validate before calling

const users = await searchUser(name);
if (!users.length) throw new Error(`@${name} not found; remove the mention`);

Type guard

const isValidMid = (v) => Number.isInteger(Number(v)) && Number(v) > 0;

Try / catch

try { await commentCmd(); } catch (e) { if (String(e.message).includes('malformed mid')) { console.error('Drop or fix the @mention and retry'); } else throw e; }

Prevention

When it happens

Trigger: A --message containing @name where the search API returns a malformed/non-numeric mid for that username — duplicate or renamed usernames, search API returning unexpected payload, or resolveUid returning empty string.

Common situations: Mentioning a username that no longer exists or was renamed; usernames with special characters confusing search; rate-limited or anti-bot response degrading the search payload.

Understand the failure class

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/1ce8b28ef4e7fd51. Report an issue: GitHub.