Camas/mods/pm/init.lua

85 lines
2.6 KiB
Lua
Raw Normal View History

2015-10-05 00:08:37 +02:00
--
-- Private messages mod
-- By Kaadmy, for Pixture
--
2019-08-28 17:31:41 +02:00
local S = minetest.get_translator("pm")
2015-10-05 00:08:37 +02:00
local enable_saving = minetest.settings:get_bool("pm_enable_saving")
2015-10-05 00:08:37 +02:00
if enable_saving == nil then enable_saving = true end
if minetest.is_singleplayer() then
enable_saving = false
end
2015-10-05 00:08:37 +02:00
local messages = {}
minetest.register_chatcommand(
2015-10-05 00:08:37 +02:00
"pm",
{
2019-08-28 17:31:41 +02:00
params = S("<player> <message>"),
description = S("Send somebody a private message"),
2015-10-05 00:08:37 +02:00
privs = {shout=true},
func = function(name, param)
local sendto, message = param:match("^(%S+)%s(.+)$")
2019-08-28 17:31:41 +02:00
if not sendto then return false, S("Invalid usage, see /help pm.") end
if not minetest.get_player_by_name(sendto) then
if enable_saving then
if messages[sendto] == nil then messages[sendto] = {} end
table.insert(messages[sendto], name .. ": " .. message)
2019-08-28 17:31:41 +02:00
return false, S("The player @1 is not online, saving message instead.", sendto)
else
2019-08-28 17:31:41 +02:00
return false, S("The player @1 is not online, and PM saving is disabled.", sendto)
end
end
minetest.log("action", "PM from " .. name .. " to " .. sendto
.. ": " .. message)
2019-08-28 17:31:41 +02:00
minetest.chat_send_player(sendto, S("PM from @1: @2", name, message))
return true, S("PM sent.")
end
})
2015-10-05 00:08:37 +02:00
minetest.register_chatcommand(
2015-10-05 00:08:37 +02:00
"pms",
{
2019-08-28 17:31:41 +02:00
description = S("Show saved private messages"),
2015-10-05 00:08:37 +02:00
func = function(name, param)
2019-08-28 17:31:41 +02:00
if not enable_saving then return false, S("PM saving is disabled.") end
if messages[name] == nil then return false, S("No saved PMs.") end
2015-10-05 00:08:37 +02:00
2019-08-28 17:31:41 +02:00
minetest.chat_send_player(name, S("Saved PMs:"))
2015-10-05 00:08:37 +02:00
local str = ""
local amt_pms = 0
for _, msg in pairs(messages[name]) do
amt_pms = amt_pms + 1
str = str .. " " .. msg .. "\n"
end
2015-10-05 00:08:37 +02:00
minetest.chat_send_player(name, str)
2015-10-05 00:08:37 +02:00
messages[name] = nil
2015-10-05 00:08:37 +02:00
2019-08-28 17:31:41 +02:00
return true, S("@1 saved PMs", amt_pms)
end
})
2015-10-05 00:08:37 +02:00
if enable_saving then
minetest.register_on_joinplayer(
2015-10-05 00:08:37 +02:00
function(player)
local name = player:get_player_name()
if messages[name] ~= nil and #messages[name] >= 1 then
2019-08-28 17:31:41 +02:00
minetest.chat_send_player(name, minetest.colorize("#0ff", S("Number of saved PMs: @1. Enter /pms command to view.", #messages[name])))
return false
else
2019-08-28 17:31:41 +02:00
minetest.chat_send_player(name, minetest.colorize("#0ff", S("You have no saved PMs. Send PMs with the /pm command.")))
return true
end
end)
2015-10-05 00:08:37 +02:00
end
default.log("mod:pm", "loaded")