support logs for manual color changes

it writes to our discord server, owo!
This commit is contained in:
Emi Matchu 2020-08-20 22:25:41 -07:00
parent 3f2b4df8f3
commit 4fa07de299
2 changed files with 80 additions and 1 deletions

View file

@ -10,6 +10,7 @@ const {
getPoseFromPetData, getPoseFromPetData,
getEmotion, getEmotion,
getGenderPresentation, getGenderPresentation,
logToDiscord,
} = require("./util"); } = require("./util");
const typeDefs = gql` const typeDefs = gql`
@ -616,12 +617,14 @@ const resolvers = {
setManualSpecialColor: async ( setManualSpecialColor: async (
_, _,
{ itemId, colorId, supportSecret }, { itemId, colorId, supportSecret },
{ db } { itemLoader, itemTranslationLoader, colorTranslationLoader, db }
) => { ) => {
if (supportSecret !== process.env["SUPPORT_SECRET"]) { if (supportSecret !== process.env["SUPPORT_SECRET"]) {
throw new Error(`Support secret is incorrect. Try setting up again?`); throw new Error(`Support secret is incorrect. Try setting up again?`);
} }
const item = await itemLoader.load(itemId);
const [ const [
result, result,
] = await db.execute( ] = await db.execute(
@ -635,6 +638,55 @@ const resolvers = {
); );
} }
if (process.env["SUPPORT_TOOLS_DISCORD_WEBHOOK_URL"]) {
try {
const [
itemTranslation,
oldColorTranslation,
newColorTranslation,
] = await Promise.all([
itemTranslationLoader.load(itemId),
item.manualSpecialColorId
? colorTranslationLoader.load(item.manualSpecialColorId)
: Promise.resolve(null),
colorId
? colorTranslationLoader.load(colorId)
: Promise.resolve(null),
]);
const oldColorName = oldColorTranslation
? capitalize(oldColorTranslation.name)
: "Auto-detect";
const newColorName = newColorTranslation
? capitalize(newColorTranslation.name)
: "Auto-detect";
await logToDiscord({
embeds: [
{
title: `🛠 ${itemTranslation.name}`,
thumbnail: {
url: item.thumbnailUrl,
height: 80,
width: 80,
},
fields: [
{
name: "Special color",
value: `${oldColorName} → **${newColorName}**`,
},
],
timestamp: new Date().toISOString(),
url: `https://impress.openneo.net/items/${item.id}`,
},
],
});
} catch (e) {
console.error("Error sending Discord support log", e);
}
} else {
console.warn("No Discord support webhook provided, skipping");
}
return { id: itemId }; return { id: itemId };
}, },

View file

@ -1,3 +1,6 @@
const beeline = require("honeycomb-beeline");
const fetch = require("node-fetch");
function capitalize(str) { function capitalize(str) {
return str[0].toUpperCase() + str.slice(1); return str[0].toUpperCase() + str.slice(1);
} }
@ -82,6 +85,29 @@ function getPoseFromPetData(petMetaData, petCustomData) {
} }
} }
async function logToDiscord(body) {
const span = beeline.startSpan({ name: "logToDiscord" });
try {
const res = await fetch(process.env["SUPPORT_TOOLS_DISCORD_WEBHOOK_URL"], {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!res.ok) {
const resText = await res.text();
throw new Error(
`Discord returned ${res.status} ${res.statusText}: ` + `${resText}`
);
}
} finally {
beeline.finishSpan(span);
}
}
function normalizeRow(row) { function normalizeRow(row) {
const normalizedRow = {}; const normalizedRow = {};
for (let [key, value] of Object.entries(row)) { for (let [key, value] of Object.entries(row)) {
@ -100,5 +126,6 @@ module.exports = {
getGenderPresentation, getGenderPresentation,
getPoseFromPetState, getPoseFromPetState,
getPoseFromPetData, getPoseFromPetData,
logToDiscord,
normalizeRow, normalizeRow,
}; };