diff --git a/data/config.example.lua b/data/config.example.lua index 30aed07..4f7da83 100644 --- a/data/config.example.lua +++ b/data/config.example.lua @@ -20,7 +20,8 @@ webhooks = { { type = "discord-modern", - url = "" + url = "", + events = { "purchase", "invalid", "error" } } }, sounds = { diff --git a/src/backend.lua b/src/backend.lua index 922e164..73d3320 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -39,15 +39,32 @@ end end -local function refund(pkey, transaction, amountToPay, message, playSound) - playSound = playSound or true +local function refund(pkey, transaction, amountToPay, message, isError) + isError = isError or true + local meta = utils.parseCommonmeta(transaction.metadata) local returnTo = meta["meta"]["return"] or transaction.from logger:debug("Refunding to: " .. returnTo) kristly.makeTransaction(pkey, returnTo, amountToPay, message) - if playSound then + if isError then speaker:play("error") + + for _, value in ipairs(config.webhooks) do + if utils.tableIncludes(value.events, "invalid") then + print("Webhook : Invalid : " .. value.type) + + if value.type == "discord" then + webhooks.discord(value.URL, + "Invalid purchase by `" .. returnTo .. "`. Refunding with meta: `" .. message .. "`.") + elseif value.type == "googleChat" then + webhooks.googleChat(value.URL, + "Invalid purchase by `" .. returnTo .. "`. Refunding with meta: `" .. message .. "`.") + elseif value.type == "discord-modern" then + webhooks.discordModernInvalid(value.URL, returnTo, transaction.id, message) + end + end + end end end @@ -179,8 +196,12 @@ if webhook.type == "discord" then webhooks.discord(webhook.URL, message) elseif webhook.type == "discord-modern" then - webhooks.discordModern(webhook.URL, transaction.from, product.displayName, amount * product.price, transaction.id, - transaction.to) + webhooks.discordModernPurchase( + webhook.URL, transaction.from, product.displayName, amount * product.price, + transaction.id, + transaction.to + ) + elseif webhook.type == "googleChat" then webhooks.googleChat(webhook.URL, message) end @@ -194,4 +215,4 @@ ws:start() end -parallel.waitForAny(startKristly, startListening) \ No newline at end of file +parallel.waitForAny(startKristly, startListening) diff --git a/src/utils.lua b/src/utils.lua index fafad9a..cb63f4b 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -52,6 +52,10 @@ return keyset end +---Parses commenmeta +---@param meta string The meta to parse +---@return table meta A table with metadata +---@source k.lua function utils.parseCommonmeta(meta) local domainMatch = "^([%l%d-_]*)@?([%l%d-]+).kst$" local commonMetaMatch = "^(.+)=(.+)$" @@ -84,4 +88,17 @@ return tbl end +---Checks if the tabe contains the value +---@param tbl table The table to check if includes value +---@param wanted string The string to check if the table includes +function utils.tableIncludes(tbl, wanted) + for _, value in ipairs(tbl) do + if value == wanted then + return true + end + end + + return false +end + return utils diff --git a/src/webhook.lua b/src/webhook.lua index a08daf4..312a5e5 100644 --- a/src/webhook.lua +++ b/src/webhook.lua @@ -27,7 +27,7 @@ ---@param total number The total amount of krist earned ---@param transactionID number The krist transaction ID of the purchase ---@param addrs string The full address that was sent krist to -function webhooks.discordModern(URL, user, item, total, transactionID, addrs) +function webhooks.discordModernPurchase(URL, user, item, total, transactionID, addrs) expect(1, URL, "string") expect(2, user, "string") expect(3, item, "string") @@ -81,6 +81,49 @@ http.post(URL, textutils.serialiseJSON(data), { ["Content-Type"] = "application/json; charset=UTF-8" }) end +function webhooks.discordModernInvalid(URL, user, transactionID, meta) + local kristweb = "[KristWeb](https://krist.club/network/transactions/" .. transactionID .. ")" + local openWithURI = " (URI)" + + local data = { + content = "", + username = "Kristify", + avatar_url = "https://media.discordapp.net/attachments/1014151202855976973/1014162892414783559/Kristify.png", + embeds = { + { + title = user .. " failed to buy something.", + color = "16750744", + fields = { + { + name = "Meta", + value = meta + }, + { + name = "Transaction ID", + value = transactionID + }, + { + name = "Open with", + value = kristweb .. " or " .. openWithURI + } + }, + author = { + name = "Someone failed to buy something!" + }, + thumbnail = { + url = "https://docs.krist.dev/favicon-128x128.png" + }, + timestamp = os.date("!%Y-%m-%dT%TZ"), + footer = { + text = "Powered by Kristify" + } + } + } + } + + http.post(URL, textutils.serialiseJSON(data), { ["Content-Type"] = "application/json; charset=UTF-8" }) +end + ---Sends a webhook to a Google Chat (requires Google Workspace) ---@param URL string The webhook URL ---@param message string The messge to send