{"record":{"id":"c349cae1e39a68f8","repo":"jackwener/OpenCLI","slug":"browser-session-required-for-bilibili-comment","errorCode":null,"errorMessage":"Browser session required for bilibili comment","messagePattern":"Browser session required for bilibili comment","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/bilibili/comment.js","lineNumber":34,"sourceCode":"}\n\ncli({\n    site: 'bilibili',\n    name: 'comment',\n    access: 'write',\n    description: '在 B站视频下发表评论或回复（官方 API，需登录；消息里的 @用户 会被解析为真实提及）',\n    domain: 'www.bilibili.com',\n    strategy: Strategy.COOKIE,\n    args: [\n        { name: 'bvid', required: true, positional: true, help: 'Video BV ID / URL / b23.tv short link' },\n        { name: 'message', required: true, positional: true, help: 'Comment text. Any @username in it is resolved to a real mention' },\n        { name: 'parent', type: 'int', help: 'top-level/root rpid to reply under (omit for a top-level comment)' },\n        { name: 'execute', type: 'boolean', help: 'Actually post the comment. Without it the command refuses to write.' },\n    ],\n    columns: ['rpid', 'bvid', 'oid', 'message', 'url'],\n    func: async (page, kwargs) => {\n        if (!page) {\n            throw new CommandExecutionError('Browser session required for bilibili comment');\n        }\n        const message = String(kwargs.message ?? '').trim();\n        if (!message)\n            throw new ArgumentError('bilibili comment message cannot be empty');\n        // Write guard: posting is public and irreversible-ish, so require an explicit opt-in.\n        if (!kwargs.execute)\n            throw new ArgumentError('Refusing to post: pass --execute to actually publish this comment');\n        const parent = kwargs.parent != null ? readPositiveInteger(kwargs.parent, 'parent') : null;\n        let bvid;\n        try {\n            bvid = await resolveBvid(kwargs.bvid);\n        }\n        catch (error) {\n            throw new ArgumentError(`Cannot resolve Bilibili BV ID from input: ${String(kwargs.bvid ?? '')}`, error instanceof Error ? error.message : String(error));\n        }\n        // Resolve bvid → aid (the reply API addresses videos by aid, as `oid`)\n        const view = await apiGet(page, '/x/web-interface/view', { params: { bvid } });\n        const viewData = requireOkPayload(view, 'view');","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/bilibili/comment.js#L16-L52","documentation":"Posting a comment on Bilibili requires a browser session (cookies/CSRF). The command's func receives `page`; when it is null/undefined (no browser session established) the library throws CommandExecutionError instead of attempting an unauthenticated write that would fail.","triggerScenarios":"Running `bilibili comment` in a context where no browser session exists — e.g. running without prior `bilibili login`, headless environment without a launched browser, or a caller invoking the func with page=null.","commonSituations":"CI/cron environments with no browser; forgetting to run the login subcommand first; session expired and page torn down; calling the exported func programmatically without a page.","solutions":["Run the bilibili login subcommand first to establish a browser session","Ensure the environment can launch a browser (headless mode in CI)","Re-login if the previous session expired","Pass a valid Playwright page object when calling func programmatically"],"exampleFix":"// before\nnpx opencli bilibili comment --bvid BV1xx --message hi\n// after\nnpx opencli bilibili login\nnpx opencli bilibili comment --bvid BV1xx --message hi","handlingStrategy":"validation","validationCode":"if (!page) throw new Error('Run `bilibili login` to establish a browser session before commenting');","typeGuard":"const hasSession = (page) => page != null && typeof page.evaluate === 'function';","tryCatchPattern":"try { await commentCmd(); } catch (e) { if (String(e.message).includes('Browser session required')) { await login(); return commentCmd(); } throw e; }","preventionTips":["Always run the login subcommand before write commands","Check session validity before automation runs","In CI, provision a headless browser session","Refresh expired sessions proactively"],"tags":["auth","browser-session","prerequisite"],"backgroundTag":"browser-session-required","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}