From 560cf2575c1608b571cb9ba2d156a023a94659b2 Mon Sep 17 00:00:00 2001 From: SimonFlapse Date: Thu, 15 Nov 2018 15:06:03 +0100 Subject: [PATCH 1/5] Added logic for detecting a @mention in chat --- features/chat_triggers.lua | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/features/chat_triggers.lua b/features/chat_triggers.lua index a4699691..7fe8c41b 100644 --- a/features/chat_triggers.lua +++ b/features/chat_triggers.lua @@ -3,6 +3,10 @@ local Game = require 'utils.game' local Event = require 'utils.event' +local prefix = '## - ' + +global.mention_enabled = true + local hodor_messages = { {'Hodor.', 16}, {'Hodor?', 16}, @@ -72,6 +76,7 @@ end local function hodor(event) local message = event.message:lower() + local player = Game.get_player_by_index(event.player_index) if message:match('hodor') then local index = math.random(1, message_weight_sum) local message_weight_sum = 0 @@ -91,7 +96,6 @@ local function hodor(event) return end - local player = Game.get_player_by_index(event.player_index) if not player or not player.valid then return end @@ -115,6 +119,34 @@ local function hodor(event) end end end + + -- Gives a sound notification to a mentioned player using @[player-name] + if global.mention_enabled then + local missing_player_string + for word in event.message:gmatch('@%S+') do + local cannot_mention = {} + for _, p in ipairs(game.connected_players) do + if '@'..p.name == word then + if p.name == player.name then + player.print(prefix..'Can\'t mention yourself!', {r = 1, g = 0, b = 0, a = 1}) + break; + end + p.print('## - '..Game.get_player_by_index(event.player_index).name..' mentioned you!', {r = 1, g = 1, b = 0, a = 1}) + p.play_sound{path='utility/new_objective', volume_modifier = 1} + player.print(prefix..'Successful mentioned '..p.name, {r = 0, g = 1, b = 0, a = 1}) + else + table.insert(cannot_mention, string.sub((word .. ', '), 2)) + end + end + for _, pname in ipairs(cannot_mention) do + missing_player_string = missing_player_string~=nil and missing_player_string .. pname or pname + end + end + if missing_player_string ~= nil then + missing_player_string = string.sub(missing_player_string, 1, (string.len(missing_player_string)-2)) + player.print(prefix..'Failed to mention: ' .. missing_player_string, {r = 1, g = 1, b = 0, a = 1}) + end + end end Event.add(defines.events.on_console_chat, hodor) From 948ed4ab1f0f3d948f7c9d98b1eb5465b036300d Mon Sep 17 00:00:00 2001 From: SimonFlapse Date: Thu, 15 Nov 2018 16:03:46 +0100 Subject: [PATCH 2/5] Minor text changes and _DEBUG options --- features/chat_triggers.lua | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/features/chat_triggers.lua b/features/chat_triggers.lua index 7fe8c41b..86cb264a 100644 --- a/features/chat_triggers.lua +++ b/features/chat_triggers.lua @@ -76,7 +76,7 @@ end local function hodor(event) local message = event.message:lower() - local player = Game.get_player_by_index(event.player_index) + if message:match('hodor') then local index = math.random(1, message_weight_sum) local message_weight_sum = 0 @@ -91,6 +91,7 @@ local function hodor(event) -- player_index is nil if the message came from the server, -- and indexing Game.players with nil is apparently an error. + local player = Game.get_player_by_index(event.player_index) local player_index = event.player_index if not player_index then return @@ -123,6 +124,7 @@ local function hodor(event) -- Gives a sound notification to a mentioned player using @[player-name] if global.mention_enabled then local missing_player_string + local not_found = 0 for word in event.message:gmatch('@%S+') do local cannot_mention = {} for _, p in ipairs(game.connected_players) do @@ -131,11 +133,14 @@ local function hodor(event) player.print(prefix..'Can\'t mention yourself!', {r = 1, g = 0, b = 0, a = 1}) break; end - p.print('## - '..Game.get_player_by_index(event.player_index).name..' mentioned you!', {r = 1, g = 1, b = 0, a = 1}) - p.play_sound{path='utility/new_objective', volume_modifier = 1} - player.print(prefix..'Successful mentioned '..p.name, {r = 0, g = 1, b = 0, a = 1}) + p.print(prefix..Game.get_player_by_index(event.player_index).name..' mentioned you!', {r = 1, g = 1, b = 0, a = 1}) + p.play_sound{path='utility/new_objective', volume_modifier = 1 } + if _DEBUG then + player.print(prefix..'Successful mentioned '..p.name, {r = 0, g = 1, b = 0, a = 1}) + end else - table.insert(cannot_mention, string.sub((word .. ', '), 2)) + not_found = not_found + 1 + table.insert(cannot_mention, (word .. ', ')) end end for _, pname in ipairs(cannot_mention) do @@ -144,7 +149,11 @@ local function hodor(event) end if missing_player_string ~= nil then missing_player_string = string.sub(missing_player_string, 1, (string.len(missing_player_string)-2)) - player.print(prefix..'Failed to mention: ' .. missing_player_string, {r = 1, g = 1, b = 0, a = 1}) + if not_found > 1 then + player.print(prefix..'Players not found: ' .. missing_player_string, {r = 1, g = 1, b = 0, a = 1}) + else + player.print(prefix..'Player not found: ' .. missing_player_string, {r = 1, g = 1, b = 0, a = 1}) + end end end end From 7a0a945da8cc27eacbf40e5bdee82be9878ec71e Mon Sep 17 00:00:00 2001 From: SimonFlapse Date: Thu, 15 Nov 2018 16:26:40 +0100 Subject: [PATCH 3/5] Changed trigger to #, preventing discord mentions --- features/chat_triggers.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/features/chat_triggers.lua b/features/chat_triggers.lua index 86cb264a..05baed76 100644 --- a/features/chat_triggers.lua +++ b/features/chat_triggers.lua @@ -121,14 +121,14 @@ local function hodor(event) end end - -- Gives a sound notification to a mentioned player using @[player-name] + -- Gives a sound notification to a mentioned player using #[player-name] if global.mention_enabled then local missing_player_string local not_found = 0 - for word in event.message:gmatch('@%S+') do + for word in event.message:gmatch('#%S+') do local cannot_mention = {} for _, p in ipairs(game.connected_players) do - if '@'..p.name == word then + if '#'..p.name == word then if p.name == player.name then player.print(prefix..'Can\'t mention yourself!', {r = 1, g = 0, b = 0, a = 1}) break; From f17f4ea31257bb886f426dbf9fcfbeda29ce23c8 Mon Sep 17 00:00:00 2001 From: SimonFlapse Date: Thu, 15 Nov 2018 16:46:04 +0100 Subject: [PATCH 4/5] Added admin and moderator mentions --- features/chat_triggers.lua | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/features/chat_triggers.lua b/features/chat_triggers.lua index 05baed76..656234af 100644 --- a/features/chat_triggers.lua +++ b/features/chat_triggers.lua @@ -125,10 +125,23 @@ local function hodor(event) if global.mention_enabled then local missing_player_string local not_found = 0 + local admin_call = false for word in event.message:gmatch('#%S+') do + local lower_word = word:lower() + if lower_word == '#admin' or lower_word == '#moderator' then + admin_call = true + end + local cannot_mention = {} for _, p in ipairs(game.connected_players) do - if '#'..p.name == word then + if admin_call then + if p.admin then + p.print(prefix..Game.get_player_by_index(event.player_index).name..' mentioned #admin!', {r = 1, g = 1, b = 0, a = 1}) + p.play_sound{path='utility/new_objective', volume_modifier = 1 } + end + end + + if not admin_call and '#'..p.name == word then if p.name == player.name then player.print(prefix..'Can\'t mention yourself!', {r = 1, g = 0, b = 0, a = 1}) break; @@ -138,7 +151,7 @@ local function hodor(event) if _DEBUG then player.print(prefix..'Successful mentioned '..p.name, {r = 0, g = 1, b = 0, a = 1}) end - else + elseif not admin_call then not_found = not_found + 1 table.insert(cannot_mention, (word .. ', ')) end @@ -146,6 +159,7 @@ local function hodor(event) for _, pname in ipairs(cannot_mention) do missing_player_string = missing_player_string~=nil and missing_player_string .. pname or pname end + admin_call = false end if missing_player_string ~= nil then missing_player_string = string.sub(missing_player_string, 1, (string.len(missing_player_string)-2)) From 3e6b0c32c4584a04a80e260db83a58c9fb42d535 Mon Sep 17 00:00:00 2001 From: SimonFlapse Date: Thu, 15 Nov 2018 16:54:16 +0100 Subject: [PATCH 5/5] Unnested if statement --- features/chat_triggers.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/features/chat_triggers.lua b/features/chat_triggers.lua index 656234af..14929b5f 100644 --- a/features/chat_triggers.lua +++ b/features/chat_triggers.lua @@ -134,11 +134,9 @@ local function hodor(event) local cannot_mention = {} for _, p in ipairs(game.connected_players) do - if admin_call then - if p.admin then - p.print(prefix..Game.get_player_by_index(event.player_index).name..' mentioned #admin!', {r = 1, g = 1, b = 0, a = 1}) - p.play_sound{path='utility/new_objective', volume_modifier = 1 } - end + if admin_call and p.admin then + p.print(prefix..Game.get_player_by_index(event.player_index).name..' mentioned #admin!', {r = 1, g = 1, b = 0, a = 1}) + p.play_sound{path='utility/new_objective', volume_modifier = 1 } end if not admin_call and '#'..p.name == word then