Google Apps Script开发Discord机器人遇40333内部网络错误
Discord机器人添加反应触发40333错误问题
问题描述
使用Google Apps Script开发Discord机器人,核心功能是发送审批消息后立即给自己的消息添加👍反应。消息发送功能正常,但添加反应的API调用失败,返回HTTP响应码403,响应体为{"message": "internal network error", "code": 40333}。机器人在Discord服务器已拥有Administrator角色,理论上具备所有必要权限。
工作流程
- 用户通过Google Sheets支持的Web应用触发操作
- 调用Google Apps Script的
processApproval函数 sendBotReply函数通过Discord API成功发送消息,返回新消息链接/ID- 调用
addDiscordReaction函数添加反应,此步骤失败
相关代码(Google Apps Script)
const BOT_TOKEN = '...'; // 令牌有效,可正常发送消息 /** * 主协调函数 */ function processApproval() { try { // 此部分正常工作:机器人发送消息并返回有效链接 const messageLinkFromBot = sendBotReply(); // 此调用失败 addDiscordReaction(messageLinkFromBot); } catch (error) { Logger.log('processApproval错误: ' + error.toString()); } } /** * 机器人发送消息,此函数正常工作 * 返回新创建消息的URL */ function sendBotReply() { const channelId = '123456789012345678'; // 示例频道ID const apiUrl = `https://discord.com/api/v10/channels/${channelId}/messages`; const payload = { content: `这是机器人发送的测试消息。` }; const options = { method: 'post', contentType: 'application/json', headers: { 'Authorization': 'Bot ' + BOT_TOKEN }, payload: JSON.stringify(payload), muteHttpExceptions: true }; const response = UrlFetchApp.fetch(apiUrl, options); const responseData = JSON.parse(response.getContentText()); // 示例返回:"https://discord.com/channels/SERVER_ID/CHANNEL_ID/MESSAGE_ID" return `https://discord.com/channels/${responseData.guild_id}/${responseData.channel_id}/${responseData.id}`; } /** * 尝试给消息添加反应,此函数失败 */ function addDiscordReaction(messageLink) { // 假设parseDiscordLink能正确从messageLink中提取ID const ids = parseDiscordLink(messageLink); // ids = { channelId: '...', messageId: '...' } if (!ids) { throw new Error(`无法从反应消息链接中解析ID: ${messageLink}`); } const reactionEmoji = encodeURIComponent('👍'); // 点赞表情 const url = `https://discord.com/api/v10/channels/${ids.channelId}/messages/${ids.messageId}/reactions/${reactionEmoji}/@me`; const options = { method: 'put', headers: { 'Authorization': 'Bot ' + BOT_TOKEN }, muteHttpExceptions: true }; const response = UrlFetchApp.fetch(url, options); const responseCode = response.getResponseCode(); const responseBody = response.getContentText(); // 问题出在这里: // responseCode为403 // responseBody为{"message": "internal network error", "code": 40333} if (responseCode !== 204) { Logger.log(`添加反应失败。状态码: ${responseCode}, 响应体: ${responseBody}`); throw new Error(`添加反应失败。API返回状态码 ${responseCode}。`); } Logger.log(`成功给消息 ${ids.messageId} 添加反应`); } // 提取ID的辅助函数 function parseDiscordLink(link) { if (!link) return null; const match = link.match(/channels\/\d+\/(\d+)\/(\d+)/); if (match && match.length === 3) { return { channelId: match[1], messageId: match[2] }; } return null; }
已排查内容
- 权限:机器人拥有Administrator角色,包含
Add Reactions和Read Message History权限 - Bot Token:令牌有效,具备
bot、applications.commands权限范围,可正常发送消息 - API端点:使用PUT方法调用
.../reactions/{emoji}/@me的URL符合Discord API文档 - ID与表情符号:
channelId、messageId及URL编码的emoji均有效 - 时序:尝试在发送与反应间加入
Utilities.sleep(2000),无改善 - 错误码:并非典型的
50013: Missing Permissions错误,40333: internal network error指向其他问题
疑问
为何添加反应时会触发特定的40333: internal network error?在机器人拥有完整管理员权限且能成功发送目标消息的情况下,根本原因是什么?这是否是Google Apps Script的UrlFetchApp已知问题,或是Discord API的特殊行为?
内容的提问来源于stack exchange,提问作者Kenneth
相关产品推荐
相关产品推荐

