- secondary rename-bot token was set as RENAME_TOKEN in .env but utils/renamer.js reads RENAMER_BOT; silently no-op'd every rename (host .env renamed separately)
- services/tickets.js canRename gutted to an always-ok shim; Mongo 2/10min per-channel gate is redundant since renames flow through RENAMER_BOT's own bucket. Ticket.renameCount / renameWindowStart remain as orphan fields (no migration)
- handlers/buttons.js + commands.js: drop the four "Channel renamed too quickly" else-branches and the rename-countdown label suffix; replace .catch(() => {}) with .catch(err => logError('rename', err)...)
- services/channelQueue.js: executeRename falls back to channel.setName(currentName) when renamer throws err.fallback === true (401/403/429); classifies non-fallback errors as renameQueue:token/permission (401/403) or renameQueue:secondary-bot ratelimited (429)
- utils/renamer.js: on 401/403 throw err.fallback=true immediately; on 429 respect retry_after up to 2000ms then throw err.fallback=true
- docs: align CLAUDE.md, docs/api/DISCORD_API_VALIDATION.md, docs/architecture/CRITICAL_FILES_AND_HOW_IT_WORKS.md
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
81 lines
2.5 KiB
JavaScript
81 lines
2.5 KiB
JavaScript
/**
|
|
* Secondary-token channel rename helper.
|
|
*
|
|
* Routes channel/thread renames through a second bot token (RENAMER_BOT)
|
|
* so renames don't consume the primary bot's per-channel 2/10min budget.
|
|
*
|
|
* The secondary bot must be invited to the guild with Manage Channels
|
|
* and Manage Threads.
|
|
*
|
|
* Not called directly from feature code — invoked by services/channelQueue.js
|
|
* so all channel ops continue to flow through the queue.
|
|
*/
|
|
|
|
const { logWarn } = require('../services/debugLog');
|
|
|
|
const DISCORD_API = 'https://discord.com/api/v10';
|
|
|
|
async function renameChannel(channelId, newName) {
|
|
const token = (process.env.RENAMER_BOT || '').trim();
|
|
if (!token) {
|
|
throw new Error('RENAMER_BOT is not set; cannot rename via secondary token');
|
|
}
|
|
|
|
const res = await fetch(`${DISCORD_API}/channels/${channelId}`, {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Authorization': `Bot ${token}`,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ name: newName })
|
|
});
|
|
|
|
const text = await res.text();
|
|
let body;
|
|
try {
|
|
body = text ? JSON.parse(text) : null;
|
|
} catch (_) {
|
|
body = text;
|
|
}
|
|
|
|
if (res.status === 429) {
|
|
const retryAfterSec = (body && typeof body === 'object' && body.retry_after) || null;
|
|
const retryAfterMs = retryAfterSec != null ? Math.ceil(Number(retryAfterSec) * 1000) : null;
|
|
logWarn('renamer', `429 rename channel=${channelId} retry_after=${retryAfterSec}`).catch(() => {});
|
|
|
|
// Respect retry_after up to 2000ms; otherwise fail over immediately.
|
|
if (retryAfterMs != null && retryAfterMs > 0 && retryAfterMs <= 2000) {
|
|
await new Promise((resolve) => setTimeout(resolve, retryAfterMs));
|
|
}
|
|
|
|
const err = new Error(`rename 429: retry_after=${retryAfterSec}`);
|
|
err.status = 429;
|
|
err.retryAfter = retryAfterSec;
|
|
err.body = body;
|
|
err.fallback = true;
|
|
throw err;
|
|
}
|
|
|
|
if (res.status === 401 || res.status === 403) {
|
|
const bodyStr = typeof body === 'string' ? body : JSON.stringify(body);
|
|
logWarn('renamer', `${res.status} rename channel=${channelId} body=${bodyStr}`).catch(() => {});
|
|
const err = new Error(`rename ${res.status}: ${bodyStr}`);
|
|
err.status = res.status;
|
|
err.body = body;
|
|
err.fallback = true;
|
|
throw err;
|
|
}
|
|
|
|
if (!res.ok) {
|
|
const bodyStr = typeof body === 'string' ? body : JSON.stringify(body);
|
|
const err = new Error(`rename failed: status=${res.status} body=${bodyStr}`);
|
|
err.status = res.status;
|
|
err.body = body;
|
|
throw err;
|
|
}
|
|
|
|
return body;
|
|
}
|
|
|
|
module.exports = { renameChannel };
|