Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.84 KB | None | 0 0
  1. -- This module holds any type of chatting functions
  2. CATEGORY_NAME = "Chat"
  3.  
  4. mute_exceptions = {"!r", "!nominate", "!style"}
  5.  
  6. ------------------------------ Psay ------------------------------
  7. function ulx.psay( calling_ply, target_ply, message )
  8. ulx.fancyLog( { target_ply, calling_ply }, "#P to #P: " .. message, calling_ply, target_ply )
  9. end
  10. local psay = ulx.command( "Chat", "ulx psay", ulx.psay, "!pm", true )
  11. psay:addParam{ type=ULib.cmds.PlayerArg, target="!^", ULib.cmds.ignoreCanTarget }
  12. psay:addParam{ type=ULib.cmds.StringArg, hint="message", ULib.cmds.takeRestOfLine }
  13. psay:defaultAccess( ULib.ACCESS_ALL )
  14. psay:help( "Send a private message to target." )
  15.  
  16. ------------------------------ Asay ------------------------------
  17. local seeasayAccess = "ulx seeasay"
  18. if SERVER then ULib.ucl.registerAccess( seeasayAccess, ULib.ACCESS_OPERATOR, "Ability to see 'ulx asay'", "Other" ) end -- Give operators access to see asays echoes by default
  19.  
  20. function ulx.asay( calling_ply, message )
  21. local format
  22. local me = "/me "
  23. if message:sub( 1, me:len() ) == me then
  24. format = "(ADMINS) *** #P #s"
  25. message = message:sub( me:len() + 1 )
  26. else
  27. format = "#P to admins: #s"
  28. end
  29.  
  30. local players = player.GetAll()
  31. for i=#players, 1, -1 do
  32. local v = players[ i ]
  33. if not ULib.ucl.query( v, seeasayAccess ) and v ~= calling_ply then -- Calling player always gets to see the echo
  34. table.remove( players, i )
  35. end
  36. end
  37.  
  38. ulx.fancyLog( players, format, calling_ply, message )
  39. end
  40. local asay = ulx.command( "Chat", "ulx asay", ulx.asay, "@", true, true )
  41. asay:addParam{ type=ULib.cmds.StringArg, hint="message", ULib.cmds.takeRestOfLine }
  42. asay:defaultAccess( ULib.ACCESS_ALL )
  43. asay:help( "Send a message to currently connected admins." )
  44.  
  45. ------------------------------ Tsay ------------------------------
  46. function ulx.tsay( calling_ply, message )
  47. ULib.tsay( _, message )
  48.  
  49. if ULib.toBool( GetConVarNumber( "ulx_logChat" ) ) then
  50. ulx.logString( string.format( "(tsay from %s) %s", calling_ply:IsValid() and calling_ply:Nick() or "Console", message ) )
  51. end
  52. end
  53. local tsay = ulx.command( "Chat", "ulx tsay", ulx.tsay, "@@", true, true )
  54. tsay:addParam{ type=ULib.cmds.StringArg, hint="message", ULib.cmds.takeRestOfLine }
  55. tsay:defaultAccess( ULib.ACCESS_ADMIN )
  56. tsay:help( "Send a message to everyone in the chat box." )
  57.  
  58. ------------------------------ Csay ------------------------------
  59. function ulx.csay( calling_ply, message )
  60. ULib.csay( _, message )
  61.  
  62. if ULib.toBool( GetConVarNumber( "ulx_logChat" ) ) then
  63. ulx.logString( string.format( "(csay from %s) %s", calling_ply:IsValid() and calling_ply:Nick() or "Console", message ) )
  64. end
  65. end
  66. local csay = ulx.command( "Chat", "ulx csay", ulx.csay, "@@@", true, true )
  67. csay:addParam{ type=ULib.cmds.StringArg, hint="message", ULib.cmds.takeRestOfLine }
  68. csay:defaultAccess( ULib.ACCESS_ADMIN )
  69. csay:help( "Send a message to everyone in the middle of their screen." )
  70. ------------------------------ Adverts ------------------------------
  71. ulx.adverts = ulx.adverts or {}
  72. local adverts = ulx.adverts -- For XGUI, too lazy to change all refs
  73.  
  74. local function doAdvert( group, id )
  75.  
  76. if adverts[ group ][ id ] == nil then
  77. if adverts[ group ].removed_last then
  78. adverts[ group ].removed_last = nil
  79. id = 1
  80. else
  81. id = #adverts[ group ]
  82. end
  83. end
  84.  
  85. local info = adverts[ group ][ id ]
  86.  
  87. local message = string.gsub( info.message, "%%curmap%%", game.GetMap() )
  88. message = string.gsub( message, "%%host%%", GetConVarString( "hostname" ) )
  89. message = string.gsub( message, "%%ulx_version%%", ULib.pluginVersionStr( "ULX" ) )
  90.  
  91. if not info.len then -- tsay
  92. local lines = ULib.explode( "\\n", message )
  93.  
  94. for i, line in ipairs( lines ) do
  95. local trimmed = line:Trim()
  96. if trimmed:len() > 0 then
  97. ULib.tsayColor( _, true, info.color, trimmed ) -- Delaying runs one message every frame (to ensure correct order)
  98. end
  99. end
  100. else
  101. ULib.csay( _, message, info.color, info.len )
  102. end
  103.  
  104. ULib.queueFunctionCall( function()
  105. local nextid = math.fmod( id, #adverts[ group ] ) + 1
  106. timer.Remove( "ULXAdvert" .. type( group ) .. group )
  107. timer.Create( "ULXAdvert" .. type( group ) .. group, adverts[ group ][ nextid ].rpt, 1, function() doAdvert( group, nextid ) end )
  108. end )
  109. end
  110.  
  111. -- Whether or not it's a csay is determined by whether there's a value specified in "len"
  112. function ulx.addAdvert( message, rpt, group, color, len )
  113. local t
  114.  
  115. if group then
  116. t = adverts[ tostring( group ) ]
  117. if not t then
  118. t = {}
  119. adverts[ tostring( group ) ] = t
  120. end
  121. else
  122. group = table.insert( adverts, {} )
  123. t = adverts[ group ]
  124. end
  125.  
  126. local id = table.insert( t, { message=message, rpt=rpt, color=color, len=len } )
  127.  
  128. if not timer.Exists( "ULXAdvert" .. type( group ) .. group ) then
  129. timer.Create( "ULXAdvert" .. type( group ) .. group, rpt, 1, function() doAdvert( group, id ) end )
  130. end
  131. end
  132.  
  133. ------------------------------ Gimp ------------------------------
  134. ulx.gimpSays = ulx.gimpSays or {} -- Holds gimp says
  135. local gimpSays = ulx.gimpSays -- For XGUI, too lazy to change all refs
  136. local ID_GIMP = 1
  137. local ID_MUTE = 2
  138.  
  139. function ulx.addGimpSay( say )
  140. table.insert( gimpSays, say )
  141. end
  142.  
  143. function ulx.clearGimpSays()
  144. table.Empty( gimpSays )
  145. end
  146.  
  147. function ulx.gimp( calling_ply, target_plys, should_ungimp )
  148. for i=1, #target_plys do
  149. local v = target_plys[ i ]
  150. if should_ungimp then
  151. v.gimp = nil
  152. else
  153. v.gimp = ID_GIMP
  154. end
  155. v:SetNWBool("ulx_gimped", not should_ungimp)
  156. end
  157.  
  158. if not should_ungimp then
  159. ulx.fancyLogAdmin( calling_ply, "#A gimped #T", target_plys )
  160. else
  161. ulx.fancyLogAdmin( calling_ply, "#A ungimped #T", target_plys )
  162. end
  163. end
  164. local gimp = ulx.command( "Chat", "ulx gimp", ulx.gimp, "!gimp" )
  165. gimp:addParam{ type=ULib.cmds.PlayersArg }
  166. gimp:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  167. gimp:defaultAccess( ULib.ACCESS_ADMIN )
  168. gimp:help( "Gimps target(s) so they are unable to chat normally." )
  169. gimp:setOpposite( "ulx ungimp", {_, _, true}, "!ungimp" )
  170. -- Add Stuff Here
  171. ------------------------------ Mute ------------------------------
  172. function ulx.mute( calling_ply, target_plys, should_unmute )
  173. for i=1, #target_plys do
  174. local v = target_plys[ i ]
  175. if should_unmute then
  176. v.gimp = nil
  177. else
  178. v.gimp = ID_MUTE
  179. end
  180. v:SetNWBool("ulx_muted", not should_unmute)
  181. end
  182.  
  183. if not should_unmute then
  184. ulx.fancyLogAdmin( calling_ply, "#A muted #T", target_plys )
  185. else
  186. ulx.fancyLogAdmin( calling_ply, "#A unmuted #T", target_plys )
  187. end
  188. end
  189. local mute = ulx.command( "Chat", "ulx mute", ulx.mute, "!mute" )
  190. mute:addParam{ type=ULib.cmds.PlayersArg }
  191. mute:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  192. mute:defaultAccess( ULib.ACCESS_ADMIN )
  193. mute:help( "Mutes target(s) so they are unable to chat." )
  194. mute:setOpposite( "ulx unmute", {_, _, true}, "!unmute" )
  195.  
  196. if SERVER then
  197. local function gimpCheck( ply, strText )
  198. if ply.gimp == ID_MUTE then
  199. for k, v in pairs(mute_exceptions) do
  200. if strText == v then
  201. print(v)
  202. return strText
  203. end
  204. end
  205.  
  206. return ""
  207. end
  208. if ply.gimp == ID_GIMP then
  209. if #gimpSays < 1 then return nil end
  210. return gimpSays[ math.random( #gimpSays ) ]
  211. end
  212. end
  213. hook.Add( "PlayerSay", "ULXGimpCheck", gimpCheck, HOOK_LOW )
  214. end
  215.  
  216. ------------------------------ Gag ------------------------------
  217. function ulx.gag( calling_ply, target_plys, should_ungag )
  218. local players = player.GetAll()
  219. for i=1, #target_plys do
  220. local v = target_plys[ i ]
  221. v.ulx_gagged = not should_ungag
  222. v:SetNWBool("ulx_gagged", v.ulx_gagged)
  223. end
  224.  
  225. if not should_ungag then
  226. ulx.fancyLogAdmin( calling_ply, "#A gagged #T", target_plys )
  227. else
  228. ulx.fancyLogAdmin( calling_ply, "#A ungagged #T", target_plys )
  229. end
  230. end
  231. local gag = ulx.command( "Chat", "ulx gag", ulx.gag, "!gag" )
  232. gag:addParam{ type=ULib.cmds.PlayersArg }
  233. gag:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  234. gag:defaultAccess( ULib.ACCESS_ADMIN )
  235. gag:help( "Gag target(s), disables microphone." )
  236. gag:setOpposite( "ulx ungag", {_, _, true}, "!ungag" )
  237.  
  238. local function gagHook( listener, talker )
  239. if talker.ulx_gagged then
  240. return false
  241. end
  242. end
  243. hook.Add( "PlayerCanHearPlayersVoice", "ULXGag", gagHook )
  244.  
  245. -- Anti-spam stuff
  246. if SERVER then
  247. local chattime_cvar = ulx.convar( "chattime", "1.5", "<time> - Players can only chat every x seconds (anti-spam). 0 to disable.", ULib.ACCESS_ADMIN )
  248. local function playerSay( ply )
  249. if not ply.lastChatTime then ply.lastChatTime = 0 end
  250.  
  251. local chattime = chattime_cvar:GetFloat()
  252. if chattime <= 0 then return end
  253.  
  254. if ply.lastChatTime + chattime > CurTime() then
  255. return ""
  256. else
  257. ply.lastChatTime = CurTime()
  258. return
  259. end
  260. end
  261. hook.Add( "PlayerSay", "ulxPlayerSay", playerSay, HOOK_LOW )
  262.  
  263. local function meCheck( ply, strText, bTeam )
  264. local meChatEnabled = GetConVarNumber( "ulx_meChatEnabled" )
  265.  
  266. if ply.gimp or meChatEnabled == 0 or (meChatEnabled ~= 2 and GAMEMODE.Name ~= "Sandbox") then return end -- Don't mess
  267.  
  268. if strText:sub( 1, 4 ) == "/me " then
  269. strText = string.format( "*** %s %s", ply:Nick(), strText:sub( 5 ) )
  270. if not bTeam then
  271. ULib.tsay( _, strText )
  272. else
  273. strText = "(TEAM) " .. strText
  274. local teamid = ply:Team()
  275. local players = team.GetPlayers( teamid )
  276. for _, ply2 in ipairs( players ) do
  277. ULib.tsay( ply2, strText )
  278. end
  279. end
  280.  
  281. if game.IsDedicated() then
  282. Msg( strText .. "\n" ) -- Log to console
  283. end
  284. if ULib.toBool( GetConVarNumber( "ulx_logChat" ) ) then
  285. ulx.logString( strText )
  286. end
  287.  
  288. return ""
  289. end
  290.  
  291. end
  292. hook.Add( "PlayerSay", "ULXMeCheck", meCheck, HOOK_LOW ) -- Extremely low priority
  293. end
  294.  
  295. local function showWelcome( ply )
  296. local message = GetConVarString( "ulx_welcomemessage" )
  297. if not message or message == "" then return end
  298.  
  299. message = string.gsub( message, "%%curmap%%", game.GetMap() )
  300. message = string.gsub( message, "%%host%%", GetConVarString( "hostname" ) )
  301. message = string.gsub( message, "%%ulx_version%%", ULib.pluginVersionStr( "ULX" ) )
  302.  
  303. ply:ChatPrint( message ) -- We're not using tsay because ULib might not be loaded yet. (client side)
  304. end
  305. hook.Add( "PlayerInitialSpawn", "ULXWelcome", showWelcome )
  306. if SERVER then
  307. ulx.convar( "meChatEnabled", "1", "Allow players to use '/me' in chat. 0 = Disabled, 1 = Sandbox only (Default), 2 = Enabled", ULib.ACCESS_ADMIN )
  308. ulx.convar( "welcomemessage", "", "<msg> - This is shown to players on join.", ULib.ACCESS_ADMIN )
  309. end
  310.  
  311.  
  312.  
  313.  
  314. function ulx.micon(calling_ply, target_ply)
  315. target_ply:SendLua([[timer.Create("mictoggle", 0.1, 0, function()LocalPlayer():ConCommand("+voicerecord")end)]])
  316. ulx.fancyLogAdmin( calling_ply, true, "#A Toggled +voicerecord on #T", target_ply )
  317.  
  318. end
  319. local micon = ulx.command("Chat", "ulx micon", ulx.micon, "!micon",true)
  320. micon:addParam{ type=ULib.cmds.PlayerArg }
  321. micon:defaultAccess( ULib.ACCESS_SUPERADMIN )
  322. micon:help( "Force microphone on." )
  323.  
  324. function ulx.micoff(calling_ply, target_ply)
  325. target_ply:SendLua([[timer.Destroy("mictoggle")LocalPlayer():ConCommand("-voicerecord")]])
  326. ulx.fancyLogAdmin( calling_ply, true, "#A Toggled -voicerecord on #T", target_ply )
  327. end
  328. local micoff = ulx.command("Chat", "ulx micoff", ulx.micoff, "!micoff",true)
  329. micoff:addParam{ type=ULib.cmds.PlayerArg }
  330. micoff:defaultAccess( ULib.ACCESS_SUPERADMIN )
  331. micoff:help( "Force microphone off." )
  332. -- Anti-spam stuff
  333. if SERVER then
  334. local chattime_cvar = ulx.convar( "chattime", "1.5", "<time> - Players can only chat every x seconds (anti-spam). 0 to disable.", ULib.ACCESS_ADMIN )
  335. local function playerSay( ply )
  336. if not ply.lastChatTime then ply.lastChatTime = 0 end
  337.  
  338. local chattime = chattime_cvar:GetFloat()
  339. if chattime <= 0 then return end
  340.  
  341. if ply.lastChatTime + chattime > CurTime() then
  342. return ""
  343. else
  344. ply.lastChatTime = CurTime()
  345. return
  346. end
  347. end
  348. hook.Add( "PlayerSay", "ulxPlayerSay", playerSay, HOOK_LOW )
  349.  
  350. local function meCheck( ply, strText, bTeam )
  351. local meChatEnabled = GetConVarNumber( "ulx_meChatEnabled" )
  352.  
  353. if ply.gimp or meChatEnabled == 0 or (meChatEnabled ~= 2 and GAMEMODE.Name ~= "Sandbox") then return end -- Don't mess
  354.  
  355. if strText:sub( 1, 4 ) == "/me " then
  356. strText = string.format( "*** %s %s", ply:Nick(), strText:sub( 5 ) )
  357. if not bTeam then
  358. ULib.tsay( _, strText )
  359. else
  360. strText = "(TEAM) " .. strText
  361. local teamid = ply:Team()
  362. local players = team.GetPlayers( teamid )
  363. for _, ply2 in ipairs( players ) do
  364. ULib.tsay( ply2, strText )
  365. end
  366. end
  367.  
  368. if game.IsDedicated() then
  369. Msg( strText .. "\n" ) -- Log to console
  370. end
  371. if ULib.toBool( GetConVarNumber( "ulx_logChat" ) ) then
  372. ulx.logString( strText )
  373. end
  374.  
  375. return ""
  376. end
  377.  
  378. end
  379. hook.Add( "PlayerSay", "ULXMeCheck", meCheck, HOOK_LOW ) -- Extremely low priority
  380. end
  381.  
  382. local function showWelcome( ply )
  383. local message = GetConVarString( "ulx_welcomemessage" )
  384. if not message or message == "" then return end
  385.  
  386. message = string.gsub( message, "%%curmap%%", game.GetMap() )
  387. message = string.gsub( message, "%%host%%", GetConVarString( "hostname" ) )
  388. message = string.gsub( message, "%%ulx_version%%", ULib.pluginVersionStr( "ULX" ) )
  389.  
  390. ply:ChatPrint( message ) -- We're not using tsay because ULib might not be loaded yet. (client side)
  391. end
  392. hook.Add( "PlayerInitialSpawn", "ULXWelcome", showWelcome )
  393. if SERVER then
  394. ulx.convar( "meChatEnabled", "1", "Allow players to use '/me' in chat. 0 = Disabled, 1 = Sandbox only (Default), 2 = Enabled", ULib.ACCESS_ADMIN )
  395. ulx.convar( "welcomemessage", "", "<msg> - This is shown to players on join.", ULib.ACCESS_ADMIN )
  396. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement