skynet/chat.lua

165 lines
3.9 KiB
Lua

local cfg = require("cfg")
local boxes = { peripheral.find("chat_box") }
local function ssleep(seconds)
local fireAt = os.time() + (seconds / 50)
local enqueued = {}
os.setAlarm(fireAt)
while true do
local ev = { os.pullEventRaw() }
if ev[1] == "alarm" then
break
else
table.insert(enqueued, ev)
end
end
for _, ev in next, enqueued do
os.queueEvent(table.unpack(ev))
end
end
local curBox = 1
local lastResetAt = 0
local function dispatch(callback)
local bx = boxes[curBox]
if bx ~= nil then
callback(bx)
end
if curBox >= #boxes then
local sinceReset = os.clock() - lastResetAt
if sinceReset < 1 then
ssleep(1 - sinceReset)
end
curBox = 1
lastResetAt = os.clock()
else
curBox = curBox + 1
end
end
local function send(unm, msg, chan)
local prefix = "&bSky"
if chan then
prefix = prefix .. "&7/&3" .. chan
end
prefix = prefix .. "&r"
if type(msg) ~= "table" then
msg = { { text = msg } }
end
dispatch(function(bx)
bx.sendFormattedMessageToPlayer(
textutils.serialiseJSON(msg),
unm,
prefix, "[]"
)
end)
end
local function userBc(msg, cat, logChan)
for i, v in pairs(cfg.users) do
if logChan then
if v.subscribeLog == true then
send(i, msg, cat or "log")
end
else
send(i, msg, cat)
end
end
end
local function reportLog(msg, cat)
userBc(msg, cat, true)
end
local function say(msg)
dispatch(function(bx)
bx.sendMessage(msg, ":", "<>")
end)
end
local function sayas(plr, msg)
dispatch(function(bx)
bx.sendMessage(msg, plr, "<>")
end)
end
local function sayserver(msg)
dispatch(function(bx)
bx.sendMessage(msg, "Server", "[]")
end)
end
local function faketpa(tgtplr, sender, fakeplr, fakemsg)
dispatch(function(bx)
local clickEvent = {
action = "open_url",
value = "https://picture.wtf/p/27wzu9.png",
}
local msg = {
{ text = fakemsg },
{ text = "\nTPA request! [ " },
{ text = sender, color = "yellow" },
{ text = " !ARROWEMJ! " },
{ text = tgtplr, color = "yellow" },
{ text = " ]" },
{ text = "\nClick one of these: " },
{
text = "Accept !ACCEPTEMJ!",
color = "green",
bold = true,
hoverEvent = {
action = "show_text",
contents = "Click to Accept",
},
clickEvent = clickEvent,
},
{ text = " | " },
{
text = "Deny !DENYEMJ!",
color = "red",
bold = true,
hoverEvent = {
action = "show_text",
contents = "Click to Deny",
},
clickEvent = clickEvent,
},
{ text = " |" },
}
local jsonMsg = textutils.serialiseJSON(msg, { allow_repetitions = true })
jsonMsg = jsonMsg:gsub("!ARROWEMJ!", "\\u27A1")
jsonMsg = jsonMsg:gsub("!ACCEPTEMJ!", "\\u2714")
jsonMsg = jsonMsg:gsub("!DENYEMJ!", "\\u274C")
bx.sendFormattedMessageToPlayer(
jsonMsg,
tgtplr,
fakeplr, "<>",
nil, nil,
true
)
end)
end
return {
send = send,
userBc = userBc,
reportLog = reportLog,
say = say,
sayas = sayas,
sayserver = sayserver,
faketpa = faketpa,
}