如何在Google Script的Discord Webhook消息中嵌入动图?
解决Discord Webhook嵌入可播放GIF的问题
你之前用[替代文本](图片URL)是普通链接格式,Discord只会把它解析成可点击的跳转链接,不会渲染成图片,自然没法播放GIF。要让GIF正常动起来,得用Discord支持的Markdown图片语法:。
具体修改步骤
找到代码中定义response的那一行:
let response = `**Check-in Progress :calendar_spiral: ** for <@${myDiscordID}> (${accountName}) !`;
把GIF的图片语法插入到合适位置(比如标题前或后),示例如下:
// 把GIF放在标题上方,替换成你的GIF实际URL let response = `\n**Check-in Progress :calendar_spiral: ** for <@${myDiscordID}> (${accountName}) !`;
修改后的完整代码
const profiles = [ { token: "ltoken=xxxx;", genshin: true, honkai_star_rail: true, honkai_3: false, accountName: "xx" } ]; const discord_notify = true const myDiscordID = "xxxx" const discordWebhook = "xxxx" const urlDict = { Genshin_Impact: 'https://sg-hk4e-api.hoyolab.com/event/sol/sign?lang=en-us&act_id=e202102251931481', Honkai_Star_Rail: 'https://sg-public-api.hoyolab.com/event/luna/os/sign?lang=en-us&act_id=e202303301540311', Honkai_3: 'https://sg-public-api.hoyolab.com/event/mani/sign?lang=en-us&act_id=e202110291205111' }; async function main() { const messages = await Promise.all(profiles.map(autoSignFunction)); const hoyolabResp = `${messages.join('\n\n')}`; if (discord_notify && discordWebhook){ postWebhook(hoyolabResp); } } function discordPing() { return myDiscordID ? `<@${myDiscordID}> ` : ''; } function autoSignFunction({ token, genshin, honkai_star_rail, honkai_3, accountName }) { const urls = []; if (genshin) urls.push(urlDict.Genshin_Impact); if (honkai_star_rail) urls.push(urlDict.Honkai_Star_Rail); if (honkai_3) urls.push(urlDict.Honkai_3); const header = { Cookie: token, 'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'gzip, deflate, br', 'Connection': 'keep-alive', 'x-rpc-app_version': '2.34.1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36', 'x-rpc-client_type': '4', 'Referer': 'https://act.hoyolab.com/', 'Origin': 'https://act.hoyolab.com' }; const options = { method: 'POST', headers: header, muteHttpExceptions: true, }; // 这里是修改后的行,替换成你的GIF URL let response = `\n**Check-in Progress :calendar_spiral: ** for <@${myDiscordID}> (${accountName}) !`; const httpResponses = UrlFetchApp.fetchAll(urls.map(url => ({ url, ...options }))); for (const [i, hoyolabResponse] of httpResponses.entries()) { const responseJson = JSON.parse(hoyolabResponse) const checkInResult = responseJson.message; const gameName = Object.keys(urlDict).find(key => urlDict[key] === urls[i])?.replace(/_/g, ' '); const isError = checkInResult != "**OK!** :white_check_mark:"; const bannedCheck = responseJson.data?.gt_result?.is_risk; if (bannedCheck) { response += `**\n${gameName}**: ${discordPing()} Auto check-in failed due to CAPTCHA blocking.`; } else { response += `**\n${gameName}**: ${isError ? discordPing() : ""}_${checkInResult}_`; } }; return response; } function postWebhook(data) { let payload = JSON.stringify({ 'username': 'Hoyoverse Check-In', 'avatar_url': 'https://i.imgur.com/rce83Dn.png', 'content': data }); const options = { method: 'POST', contentType: 'application/json', payload: payload, muteHttpExceptions: true }; UrlFetchApp.fetch(discordWebhook, options); } function runScriptA() { ScriptApp.run("1pUkVXPAKuaFkFu82KENTwJsvK2u5IDk3hdUPezzbYVNcPgZ1aenuu9Qb").myFunctionToRun(); // Replace "Script_A_ID" with the actual script ID of Script A }
额外注意事项
- 确保你的GIF URL是直接文件地址:打开链接后能直接看到动图,不是包含图片的网页
- 避免使用有防盗链限制的图床,否则Discord无法拉取资源,GIF会显示为破碎图标
内容的提问来源于stack exchange,提问作者Roy Seven
相关产品推荐
相关产品推荐

