forked from SmallJoker/channels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatcommands.lua
182 lines (153 loc) · 4.93 KB
/
chatcommands.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
minetest.register_chatcommand("channel", {
description = "Manages chat channels",
privs = {
interact = true,
shout = true
},
func = function(name, param)
if param == "" then
minetest.chat_send_player(name, "Online players: /channel online")
minetest.chat_send_player(name, "Join/switch: /channel join <channel>")
minetest.chat_send_player(name, "Leave channel: /channel leave")
minetest.chat_send_player(name, "Invite to channel: /channel invite <playername>")
return
elseif param == "online" then
channels.command_online(name)
return
elseif param == "leave" then
channels.command_leave(name)
return
end
local args = param:split(" ")
if args[1] == "join" and #args == 2 then
channels.command_join(name, args[2])
return
elseif args[1] == "invite" and #args == 2 then
channels.command_invite(name, args[2])
return
elseif args[1] == "wall" and #args >= 2 then
channels.command_wall(name, table.concat(args," ",2, #args) )
return
end
minetest.chat_send_player(name, "Error: Please check again '/channel' for correct usage.")
end,
})
function channels.say_chat(sendername, message, channel)
-- For chat messages: 'message' must begin with '<playername>'
-- if channel==nil then message is sent only to players in global chat
minetest.log("action","CHAT: #" .. (channel or "no channel") .. " " .. message)
local all_players = minetest.get_connected_players()
for _,player in ipairs(all_players) do
local playername = player:get_player_name()
if channels.players[playername] == channel then
minetest.chat_send_player(playername, message)
end
end
end
function channels.command_invite(hoster,guest)
local channelname = channels.players[hoster]
if not channelname then
if channels.allow_global_channel then
channelname = "the global chat"
else
minetest.chat_send_player(hoster, "The global channel is not usable.")
return
end
else
channelname = "the '" .. channelname .. "' chat channel."
end
minetest.chat_send_player(guest, hoster .. " invites you to join " .. channelname)
-- Let other players in channel know
channels.say_chat(hoster, hoster .. " invites " .. guest .. " to join " .. channelname, channelname)
end
function channels.command_wall(name, message)
local playerprivs = minetest.get_player_privs(name)
if not playerprivs.basic_privs then
minetest.chat_send_player(name, "Error - require 'basic_privs' privilege.")
return
end
minetest.chat_send_all("(Announcement from " .. name .. "): " .. message)
end
function channels.command_online(name)
local channel = channels.players[name]
local players = "You"
if channel then
for k,v in pairs(channels.players) do
if v == channel and k ~= name then
players = players .. ", " .. k
end
end
else
local oplayers = minetest.get_connected_players()
for _,player in ipairs(oplayers) do
local p_name = player:get_player_name()
if not channels.players[p_name] and p_name ~= name then
players = players .. ", " .. p_name
end
end
return
end
minetest.chat_send_player(name, "Online players in this channel: " .. players)
end
function channels.command_join(name, param)
if param == "" then
minetest.chat_send_player(name, "Error: Empty channel name")
return
end
local channel_old = channels.players[name]
if channel_old then
if channel_old == param then
minetest.chat_send_player(name, "Error: You are already in this channel")
return
end
channels.say_chat(name, "# " .. name .. " left the channel", channel_old)
else
local oplayers = minetest.get_connected_players()
for _,player in ipairs(oplayers) do
local p_name = player:get_player_name()
if not channels.players[p_name] and p_name ~= name and channels.allow_global_channel then
minetest.chat_send_player(p_name, "# " .. name .. " left the global chat")
end
end
end
local player = minetest.get_player_by_name(name)
if not player then
return
end
if channels.huds[name] then
player:hud_remove(channels.huds[name])
end
channels.players[name] = param
channels.huds[name] = player:hud_add({
hud_elem_type = "text",
name = "Channel",
number = 0xFFFFFF,
position = {x = 0.6, y = 0.03},
text = "Channel: " .. param,
scale = {x = 200,y = 25},
alignment = {x = 0, y = 0},
})
channels:save()
channels.say_chat("", "# " .. name .. " joined the channel", param)
end
function channels.command_leave(name)
local player = minetest.get_player_by_name(name)
if not player then
channels.players[name] = nil
channels.huds[name] = nil
return
end
if not (channels.players[name] and channels.huds[name]) then
minetest.chat_send_player(name, "Please join a channel first to leave it")
return
end
if channels.players[name] then
channels.say_chat("", "# " .. name .. " left the channel", channels.players[name])
channels.players[name] = nil
end
if channels.huds[name] then
player:hud_remove(channels.huds[name])
channels.huds[name] = nil
end
channels:save()
end