Google Form转Discord Webhook失效 报错400(50006)求助
问题排查:Google Form提交到Discord Webhook报错50006
几年前从Github获取的代码,用于将Google Form提交内容通过Discord Webhook发送至指定频道,此前运行正常,数月前突然失效。提交表单时触发报错:
Exception: Request failed for https://discord.com returned code 400. Truncated server response:
{"message": "Cannot send an empty message", "code": 50006}
相关代码:
const POST_URL = "https://discord.com/api/webhooks/1237046117418401842/WFieSt7WaMHYWxmf58U6Cvui3t2LWGV1sZffw-XhCG9d5v5PB39ot7FGLvhKu5KO4VMB"; function onSubmit(e) { const response = e.response.getItemResponses(); let items = []; for (const responseAnswer of response) { const question = responseAnswer.getItem().getTitle(); const answer = responseAnswer.getResponse(); let parts = [] try { parts = answer.match(/[\s\S]{1,1024}/g) || []; } catch (e) { parts = answer; } if (!answer) { continue; } for (const [index, part] of Object.entries(parts)) { if (index == 0) { items.push({ "name": question, "value": part, "inline": false }); } else { items.push({ "name": question.concat(" (cont.)"), "value": part, "inline": false }); } } } const options = { "method": "post", "headers": { "contentType": "application/json", }, "payload": JSON.stringify({ "content": "", "embeds": [{ "title": "GotFA Application", "color": 33023, // This is optional, you can look for decimal colour codes at https://www.webtoolkitonline.com/hexadecimal-decimal-color-converter.html "fields": items, "footer": { "text": "Some footer here" }, "timestamp": new Date().toISOString() }] }) }; UrlFetchApp.fetch(POST_URL, options); };
问题分析
- Discord API规则变更:原代码中
content字段使用了零宽空格字符来规避空消息检查,但Discord现在不再认可这种“伪空”内容,判定消息为空。 - 空字段场景未处理:如果表单所有回答都被
if (!answer)过滤(比如用户未填写任何内容),items数组为空,导致embed无有效字段,进一步触发空消息报错。 - 潜在代码bug:catch块中直接将
answer赋值给parts(可能是字符串),后续循环会因非数组类型出错,间接导致消息内容异常。
修复方案
修改后的代码:
const POST_URL = "https://discord.com/api/webhooks/1237046117418401842/WFieSt7WaMHYWxmf58U6Cvui3t2LWGV1sZffw-XhCG9d5v5PB39ot7FGLvhKu5KO4VMB"; function onSubmit(e) { const response = e.response.getItemResponses(); let items = []; for (const responseAnswer of response) { const question = responseAnswer.getItem().getTitle(); const answer = responseAnswer.getResponse(); let parts = [] try { parts = answer.match(/[\s\S]{1,1024}/g) || []; } catch (e) { parts = [answer]; // 转为数组,避免循环出错 } if (!answer) { continue; } for (const [index, part] of Object.entries(parts)) { if (index == 0) { items.push({ "name": question, "value": part, "inline": false }); } else { items.push({ "name": `${question} (cont.)`, "value": part, "inline": false }); } } } // 处理无有效回答的情况 if (items.length === 0) { items.push({ "name": "无有效回答", "value": "表单提交但未填写任何内容", "inline": false }); } const options = { "method": "post", "headers": { "Content-Type": "application/json", // 标准HTTP头键名 }, "payload": JSON.stringify({ "content": "收到新的表单提交:", // 替换零宽空格为可见文本 "embeds": [{ "title": "GotFA Application", "color": 33023, "fields": items, "footer": { "text": "Some footer here" }, "timestamp": new Date().toISOString() }] }) }; UrlFetchApp.fetch(POST_URL, options); };
关键修改点
- 将
content字段的零宽空格替换为明确的提示文本,确保Discord识别为有效内容。 - 修正请求头键名为标准的
Content-Type,符合HTTP规范。 - 添加空字段处理逻辑,当无有效回答时插入默认提示字段。
- 修复catch块的
parts赋值,确保始终为数组类型,避免循环报错。
内容的提问来源于stack exchange,提问作者Virgo
相关产品推荐
相关产品推荐

