init.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- tell/init.lua
  2. tell = {}
  3. local list = {}
  4. -- Choose command name
  5. local commandname = "tell"
  6. if minetest.get_modpath("mesecons_commandblock") then
  7. commandname = "tellafk"
  8. end
  9. -- [local function] Load
  10. local function load()
  11. local res = io.open(minetest.get_worldpath().."/tell.txt", "r")
  12. if res then
  13. res = minetest.deserialize(res:read("*all"))
  14. if type(res) == "table" then
  15. list = res
  16. end
  17. end
  18. end
  19. -- [local function] Save
  20. local function save()
  21. io.open(minetest.get_worldpath().."/tell.txt", "w"):write(minetest.serialize(list))
  22. end
  23. -- Load
  24. load()
  25. -- [register] On shutdown
  26. minetest.register_on_shutdown(save)
  27. -- [function] Add item
  28. function tell.add(name, from, msg)
  29. if not list[name] then
  30. list[name] = {}
  31. end
  32. list[name][#list[name] + 1] = {
  33. from = from,
  34. msg = msg,
  35. time = os.date("%I:%M %p on %B %d, %Y"),
  36. }
  37. minetest.log("action", dump(list))
  38. return #list[name] + 1
  39. end
  40. -- [function] Remove item
  41. function tell.remove(name, id)
  42. if list[name] and list[name][id] then
  43. list[name][id] = nil
  44. end
  45. end
  46. -- [function] Show messages
  47. function tell.show(name)
  48. if list and list[name] then
  49. for _, i in ipairs(list[name]) do
  50. minetest.chat_send_player(name, minetest.colorize("#ff5400", "Message from "
  51. ..i.from.." at "..i.time..": "..i.msg))
  52. -- Remove entry
  53. tell.remove(name, _)
  54. end
  55. end
  56. end
  57. -- [register] Chatcommand
  58. minetest.register_chatcommand(commandname, {
  59. description = "Send a message to an offline or AFK player",
  60. params = "<name> <message>",
  61. func = function(name, param)
  62. local sendto, msg = param:match("^(%S+)%s(.+)$")
  63. if not sendto or not msg then
  64. return false, "Invalid usage, see /help " .. commandname .. "."
  65. end
  66. minetest.log("action", "Tell from " .. name .. " to " .. sendto
  67. .. ": " .. msg)
  68. if tell.add(sendto, name, msg) then
  69. return true, "I'll pass that on when "..sendto.." is around."
  70. else
  71. return false, "Unknown error."
  72. end
  73. end,
  74. })
  75. -- [register] On join player
  76. minetest.register_on_joinplayer(function(player)
  77. local name = player:get_player_name()
  78. tell.show(name)
  79. end)
  80. -- [register] On chat message
  81. minetest.register_on_chat_message(function(name)
  82. tell.show(name)
  83. end)