diff --git a/data/config.example.lua b/data/config.example.lua index 204d576..46257fb 100644 --- a/data/config.example.lua +++ b/data/config.example.lua @@ -14,7 +14,7 @@ }, webhooks = { { - type = "discord", + type = "discord-modern", url = "" } }, diff --git a/src/backend.lua b/src/backend.lua index 681b6c0..04fdec2 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -143,6 +143,9 @@ logger:debug("Webhook: ", webhook.type, webhook.URL) if webhook.type == "discord" then webhooks.discord(webhook.URL, message) + elseif webhook.type == "discord-modern" then + webhooks.discordModern(webhook.URL, transaction.from, product.id, amount * product.price, transaction.id, + transaction.to) elseif webhook.type == "googleChat" then webhooks.googleChat(webhook.URL, message) end diff --git a/src/webhook.lua b/src/webhook.lua index b38ee9a..dee432b 100644 --- a/src/webhook.lua +++ b/src/webhook.lua @@ -15,7 +15,64 @@ username = username or "Kristify shop" avatar = avatar or "https://media.discordapp.net/attachments/1014151202855976973/1014162892414783559/Kristify.png" - http.post(URL, "content=" .. message .. "&username=" .. username .. "&avatar_url=" .. avatar) + -- http.post(URL, "content=" .. message .. "&username=" .. username .. "&avatar_url=" .. avatar) + http.post(URL, textutils.serialiseJSON({ content = message, username = username, avatar_url = avatar }), + { ["Content-Type"] = "application/json; charset=UTF-8" }) +end + +---Sends a webhook to a Discord Guild, with modern embeds! +---@param URL string The webhook URL +---@param user string The krist address that purchased +---@param item string The item that was purchased +---@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) + expect(1, URL, "string") + expect(2, user, "string") + expect(3, item, "string") + expect(4, total, "number") + expect(5, transactionID, "number") + expect(6, addrs, "string") + + local data = { + content = "", + username = "Kristify", + avatar_url = "https://media.discordapp.net/attachments/1014151202855976973/1014162892414783559/Kristify.png", + embeds = { + { + title = user .. " bought " .. item, + url = "https://krist.club/network/transactions/" .. transactionID, + color = "16750744", + fields = { + { + name = "Ends up at", + value = addrs + }, + { + name = "Transaction ID", + value = transactionID + }, + { + name = "Krist earned", + value = total + }, + }, + author = { + name = "Someone bought 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)