optional discord webhook

master
BuckarooBanzay 2020-01-24 10:43:11 +01:00
parent f1ce36b132
commit a5d8285ad2
5 changed files with 76 additions and 10 deletions

View File

@ -84,6 +84,18 @@ Every mob punch gives you the amount of damage on the mob in xp
See:
* https://github.com/thomasrudin-mt/xp_redo_ranks_ores
## Settings
* **xp_redo.discord.webhook_url** discord webhook url (optional)
* **xp_redo.discord.texture_baseurl** baseurl for the webhook avatar image (optional)
For the webhook, the mod has to be in the `secure.http_mods` setting:
minetest.conf
```
secure.http_mods = xp_redo
```
## Lua api
## Ranks

View File

@ -88,6 +88,14 @@ xp_redo.get_xp = function(playername)
return currentXp
end
-- http://lua-users.org/lists/lua-l/2006-01/msg00525.html
function xp_redo.format_thousand(v)
local s = string.format("%d", math.floor(v))
local pos = string.len(s) % 3
if pos == 0 then pos = 3 end
return string.sub(s, 1, pos)
.. string.gsub(string.sub(s, pos+1), "(...)", "'%1")
end
xp_redo.add_xp = function(playername, xp)

13
hud.lua
View File

@ -16,14 +16,7 @@ local HUD_ALIGNMENT = {x = 1, y = 0}
local HUD_DISPLAY_STATE_NAME = "hud_state"
-- http://lua-users.org/lists/lua-l/2006-01/msg00525.html
local function format_thousand(v)
local s = string.format("%d", math.floor(v))
local pos = string.len(s) % 3
if pos == 0 then pos = 3 end
return string.sub(s, 1, pos)
.. string.gsub(string.sub(s, pos+1), "(...)", "'%1")
end
local setup_hud = function(player)
local playername = player:get_player_name()
@ -163,11 +156,11 @@ xp_redo.update_hud = function(player, xp, rank, next_rank)
return
end
local infoTxt = "XP: " .. format_thousand(xp)
local infoTxt = "XP: " .. xp_redo.format_thousand(xp)
local progress = 100
if next_rank ~= nil then
infoTxt = infoTxt .. "/" .. format_thousand(next_rank.xp)
infoTxt = infoTxt .. "/" .. xp_redo.format_thousand(next_rank.xp)
if next_rank.xp > xp then
-- progress from 0 to 100
progress = tonumber(xp / next_rank.xp * 100)

View File

@ -21,6 +21,15 @@ xp_redo = {
}
}
-- optional mapserver-bridge stuff below
local http = minetest.request_http_api()
if http then
print("Enabling discord webhook for XP-Redo")
local webhook = dofile(MP.."/webhook.lua")
webhook(http)
end
dofile(MP.."/hooks.lua")
dofile(MP.."/ranks.lua")
dofile(MP.."/json.lua") --json export

44
webhook.lua Normal file
View File

@ -0,0 +1,44 @@
local webhook_url = minetest.settings:get("xp_redo.discord.webhook_url")
local texture_baseurl = minetest.settings:get("xp_redo.discord.texture_baseurl")
-- discord webhook
return function(http)
if not webhook_url then
minetest.log("error", "xp_redo webhook_url not set!")
return
end
xp_redo.register_hook({
rank_change = function(playername, xp, rank)
if not playername or not rank or not rank.name or not xp then
return
end
local data = {
content = "Player __" .. playername .. "__ leveled up to **" ..
rank.name .. "** with an xp of " .. xp_redo.format_thousand(xp) .. "!"
}
if texture_baseurl and rank.icon then
data.avatar_url = texture_baseurl .. rank.icon
end
local json = minetest.write_json(data)
-- new rank
http.fetch({
url = webhook_url,
extra_headers = { "Content-Type: application/json" },
timeout = 5,
post_data = json
}, function()
-- ignore error
end)
end
})
end