chat_modes_2025/channel_mode.lua

82 lines
2.0 KiB
Lua
Raw Normal View History

2017-02-28 20:03:44 +00:00
-- Channel mode
local allchannels = {} -- channel_name --> list of players
local playerschannel = {} -- playername --> channel_name
2017-03-01 10:13:39 +00:00
-- Define a special moderator channel that cannot be joined by regular players
local moderatorchannel = minetest.setting_get("chat_modes.channels.moderator") or "moderators"
2017-03-03 11:28:29 +00:00
local function channelcheck(playername, params)
if #params < 1 then
minetest.chat_send_player("Please specify a channel name")
return false
end
if params[1] == moderatorchannel then
return minetest.get_player_privs(playername, {basic_privs=true})
else
return true
end
end
2017-03-03 10:51:36 +00:00
chat_modes.register_mode("channel", {
2017-02-28 20:03:44 +00:00
help = "Send messages to a specific channel only.",
2017-03-01 10:13:39 +00:00
can_register = function(playername, params)
2017-03-03 11:28:29 +00:00
if not channelcheck(playername, params) then
return false
2017-03-01 10:13:39 +00:00
end
2017-03-03 11:28:29 +00:00
return true
2017-03-01 10:13:39 +00:00
end,
2017-02-28 20:03:44 +00:00
register = function(playername, params)
2017-03-03 11:28:29 +00:00
if not channelcheck(playername, params) then
return false
2017-03-01 10:13:39 +00:00
end
2017-02-28 20:03:44 +00:00
local channelname = params[1]
2017-03-03 12:05:06 +00:00
playerschannel[playername] = channelname
if not allchannels[channelname] then
allchannels[channelname] = {}
end
local channelplayers = allchannels[ channelname ]
2017-02-28 20:03:44 +00:00
channelplayers[playername] = minetest.get_player_by_name(playername)
playerschannel[playername] = channelname
2017-03-01 10:13:39 +00:00
return true
2017-02-28 20:03:44 +00:00
end,
deregister = function(playername)
2017-03-03 12:05:06 +00:00
local channelname = playerschannel[playername]
local channelplayers = allchannels[channelname]
2017-02-28 20:03:44 +00:00
channelplayers[playername] = nil
playerschannel[playername] = nil
end,
getPlayers = function(playername, message)
local targetplayers = {}
2017-03-03 12:05:06 +00:00
local channelname = playerschannel[playername]
local channelplayers = allchannels[channelname]
chat_modes.dodebug("Got channel for "..playername..": ", {channel=channelname, channelplayers=allchannels[channelname] })
-- Use an explicit counter because #targetplayers is always 0
local i = 1
for playername,player in pairs(channelplayers) do
targetplayers[i] = player
i = i+1
2017-02-28 20:03:44 +00:00
end
2017-02-28 20:32:46 +00:00
2017-03-03 12:05:06 +00:00
chat_modes.dodebug("Valid players are ", targetplayers)
2017-02-28 20:32:46 +00:00
return targetplayers
2017-02-28 20:03:44 +00:00
end
})