Refine discussion moderator messaging flow
This commit is contained in:
@@ -20,12 +20,16 @@ export function resolveDiscordUserId(api: OpenClawPluginApi, accountId: string):
|
||||
return userIdFromToken(acct.token);
|
||||
}
|
||||
|
||||
export type ModeratorMessageResult =
|
||||
| { ok: true; status: number; channelId: string; messageId?: string }
|
||||
| { ok: false; status?: number; channelId: string; error: string };
|
||||
|
||||
export async function sendModeratorMessage(
|
||||
token: string,
|
||||
channelId: string,
|
||||
content: string,
|
||||
logger: { info: (msg: string) => void; warn: (msg: string) => void },
|
||||
): Promise<boolean> {
|
||||
): Promise<ModeratorMessageResult> {
|
||||
try {
|
||||
const r = await fetch(`https://discord.com/api/v10/channels/${channelId}/messages`, {
|
||||
method: "POST",
|
||||
@@ -35,15 +39,27 @@ export async function sendModeratorMessage(
|
||||
},
|
||||
body: JSON.stringify({ content }),
|
||||
});
|
||||
if (!r.ok) {
|
||||
const text = await r.text();
|
||||
logger.warn(`dirigent: moderator send failed (${r.status}): ${text}`);
|
||||
return false;
|
||||
|
||||
const text = await r.text();
|
||||
let json: Record<string, unknown> | null = null;
|
||||
try {
|
||||
json = text ? (JSON.parse(text) as Record<string, unknown>) : null;
|
||||
} catch {
|
||||
json = null;
|
||||
}
|
||||
logger.info(`dirigent: moderator message sent to channel=${channelId}`);
|
||||
return true;
|
||||
|
||||
if (!r.ok) {
|
||||
const error = `discord api error (${r.status}): ${text || "<empty response>"}`;
|
||||
logger.warn(`dirigent: moderator send failed channel=${channelId} ${error}`);
|
||||
return { ok: false, status: r.status, channelId, error };
|
||||
}
|
||||
|
||||
const messageId = typeof json?.id === "string" ? json.id : undefined;
|
||||
logger.info(`dirigent: moderator message sent to channel=${channelId} messageId=${messageId ?? "unknown"}`);
|
||||
return { ok: true, status: r.status, channelId, messageId };
|
||||
} catch (err) {
|
||||
logger.warn(`dirigent: moderator send error: ${String(err)}`);
|
||||
return false;
|
||||
const error = String(err);
|
||||
logger.warn(`dirigent: moderator send error channel=${channelId}: ${error}`);
|
||||
return { ok: false, channelId, error };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user