|
| 1 | +const moment = require("moment"); |
| 2 | +const path = require("path"); |
| 3 | +const fetch = require("node-fetch"); |
| 4 | +const { I18n } = require("i18n"); |
| 5 | + |
| 6 | +const webhookURL = process.env.DISCORD_WEBHOOK; |
| 7 | +const language = process.env.LANGUAGE; |
| 8 | +const i18n = new I18n(); |
| 9 | + |
| 10 | +i18n.configure({ |
| 11 | + locales: ['en', 'ko', 'ja'], |
| 12 | + directory: path.join(__dirname, '../locales'), |
| 13 | + defaultLocale: 'en' |
| 14 | +}); |
| 15 | + |
| 16 | +i18n.setLocale(language || 'en'); |
| 17 | + |
| 18 | +function post(appInfo, submissionStartDate) { |
| 19 | + const status = i18n.__(appInfo.status); |
| 20 | + const message = i18n.__("Message", { appname: appInfo.name, status: status }); |
| 21 | + const embed = discordEmbed(appInfo, submissionStartDate); |
| 22 | + |
| 23 | + hook(message, embed); |
| 24 | +} |
| 25 | + |
| 26 | +async function hook(message, embed) { |
| 27 | + const payload = { |
| 28 | + content: message, |
| 29 | + embeds: [embed] |
| 30 | + }; |
| 31 | + |
| 32 | + await fetch(webhookURL, { |
| 33 | + method: 'POST', |
| 34 | + headers: { |
| 35 | + 'Content-Type': 'application/json', |
| 36 | + }, |
| 37 | + body: JSON.stringify(payload), |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +function discordEmbed(appInfo, submissionStartDate) { |
| 42 | + const embed = { |
| 43 | + title: "App Store Connect", |
| 44 | + author: { |
| 45 | + name: appInfo.name, |
| 46 | + icon_url: appInfo.iconURL |
| 47 | + }, |
| 48 | + url: `https://appstoreconnect.apple.com/apps/${appInfo.appID}/appstore`, |
| 49 | + fields: [ |
| 50 | + { |
| 51 | + name: i18n.__("Version"), |
| 52 | + value: appInfo.version, |
| 53 | + inline: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: i18n.__("Status"), |
| 57 | + value: i18n.__(appInfo.status), |
| 58 | + inline: true, |
| 59 | + } |
| 60 | + ], |
| 61 | + footer: { |
| 62 | + text: "appstore-status-bot", |
| 63 | + icon_url: "https://icons-for-free.com/iconfiles/png/512/app+store+apple+apps+game+games+store+icon-1320085881005897327.png" |
| 64 | + }, |
| 65 | + timestamp: new Date() |
| 66 | + }; |
| 67 | + |
| 68 | + // Set elapsed time since "Waiting For Review" start |
| 69 | + if ( |
| 70 | + submissionStartDate && |
| 71 | + appInfo.status !== "Prepare for Submission" && |
| 72 | + appInfo.status !== "Waiting For Review" |
| 73 | + ) { |
| 74 | + const elapsedHours = moment().diff(moment(submissionStartDate), "hours"); |
| 75 | + embed.fields.push({ |
| 76 | + name: "Elapsed Time", |
| 77 | + value: `${elapsedHours} hours`, |
| 78 | + inline: true, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + embed.color = colorForStatus(appInfo.status); |
| 83 | + |
| 84 | + return embed; |
| 85 | +} |
| 86 | + |
| 87 | +function colorForStatus(status) { |
| 88 | + const infoColor = 0x8e8e8e; |
| 89 | + const warningColor = 0xf4f124; |
| 90 | + const successColor1 = 0x1eb6fc; |
| 91 | + const successColor2 = 0x14ba40; |
| 92 | + const failureColor = 0xe0143d; |
| 93 | + const colorMapping = { |
| 94 | + "Prepare for Submission": infoColor, |
| 95 | + "Waiting For Review": infoColor, |
| 96 | + "In Review": successColor1, |
| 97 | + "Pending Contract": warningColor, |
| 98 | + "Waiting For Export Compliance": warningColor, |
| 99 | + "Pending Developer Release": successColor2, |
| 100 | + "Processing for App Store": successColor2, |
| 101 | + "Pending Apple Release": successColor2, |
| 102 | + "Ready for Sale": successColor2, |
| 103 | + Rejected: failureColor, |
| 104 | + "Metadata Rejected": failureColor, |
| 105 | + "Removed From Sale": failureColor, |
| 106 | + "Developer Rejected": failureColor, |
| 107 | + "Developer Removed From Sale": failureColor, |
| 108 | + "Invalid Binary": failureColor, |
| 109 | + }; |
| 110 | + |
| 111 | + return colorMapping[status]; |
| 112 | +} |
| 113 | + |
| 114 | +module.exports = { |
| 115 | + post: post, |
| 116 | +}; |
0 commit comments