2018-11-11 06:00:28 +02:00
-- This feature auto-responds to key words or phrases. We use the name/actor Hodor because it is Redmew's beloved discord bot.
local Event = require ' utils.event '
2018-12-29 19:39:20 +02:00
local Color = require ' resources.color_presets '
2019-01-15 15:27:48 +02:00
local table = require ' utils.table '
2018-11-17 13:56:03 +02:00
local Hodor = require ' resources.hodor_messages '
2018-11-11 06:00:28 +02:00
2018-11-17 15:22:51 +02:00
local prefix = ' ## - '
2018-11-11 19:12:38 +02:00
local auto_replies = {
2019-03-03 22:50:19 +02:00
[ ' discord ' ] = { { ' chat_triggers.discord ' } } ,
[ ' patreon ' ] = { { ' chat_triggers.patreon ' } } ,
[ ' donate ' ] = { { ' chat_triggers.donate ' } } ,
2022-04-13 20:47:06 +02:00
[ ' donator ' ] = { { ' chat_triggers.donate ' } } ,
2019-03-03 22:50:19 +02:00
[ ' grief ' ] = { { ' chat_triggers.grief ' } }
2018-11-11 06:00:28 +02:00
}
2018-11-26 08:49:18 +02:00
--- Check for player and get player
local function get_player ( event )
2018-11-11 06:00:28 +02:00
-- player_index is nil if the message came from the server,
-- and indexing Game.players with nil is apparently an error.
local player_index = event.player_index
if not player_index then
2018-11-26 17:50:06 +02:00
return nil
2018-11-11 06:00:28 +02:00
end
2019-05-16 12:10:56 +02:00
local player = game.get_player ( event.player_index )
2018-11-11 06:00:28 +02:00
if not player or not player.valid then
2018-11-26 17:50:06 +02:00
return nil
2018-11-11 06:00:28 +02:00
end
2018-11-26 08:49:18 +02:00
return player
end
2018-11-17 13:56:03 +02:00
2018-11-26 08:49:18 +02:00
--- Emulates the discord bot hodor's reaction to his name
local function hodor ( event )
-- first check for a match, since 99% of messages aren't a match for 'hodor'
local message = event.message : lower ( )
if message : match ( ' hodor ' ) then
2018-12-09 20:10:16 +02:00
game.print ( ' Hodor: ' .. table.get_random_weighted ( Hodor ) )
2018-11-26 08:49:18 +02:00
end
end
--- Automatically responds to preset trigger words
local function auto_respond ( event )
local message = event.message : lower ( )
local player = get_player ( event )
2019-03-03 22:50:19 +02:00
if player and player.valid and not player.admin then
2018-11-11 19:12:38 +02:00
for trigger , replies in pairs ( auto_replies ) do
if message : match ( trigger ) then
for _ , reply in pairs ( replies ) do
player.print ( reply )
2018-11-11 06:00:28 +02:00
end
end
end
end
2018-11-26 08:49:18 +02:00
end
2018-11-11 06:00:28 +02:00
2018-11-26 08:49:18 +02:00
--- Create notifications when a player's name is mentioned
local function mentions ( event )
2018-11-27 21:12:50 +02:00
-- Gives a sound notification to a mentioned player using #[player-name], [player-name]#, @[player-name], [player-name]@ or to admins with admin with prefix or postfix
2018-11-26 08:49:18 +02:00
local missing_player_string
local not_found = 0
local cannot_mention = { }
local player = get_player ( event )
if not player or not player.valid then
return
2018-11-11 06:00:28 +02:00
end
2018-11-15 16:06:03 +02:00
2018-11-26 08:49:18 +02:00
for w in event.message : gmatch ( ' %S+ ' ) do
local word = w : lower ( )
local trimmed_word = string.sub ( word , 0 , string.len ( word ) - 1 )
local first_char = string.sub ( word , 0 , 1 )
local last_char = string.sub ( word , string.len ( word ) )
local success = false
local admin_call = false
2018-11-27 21:12:50 +02:00
if ( first_char ~= ' # ' and last_char ~= ' # ' ) and ( first_char ~= ' @ ' and last_char ~= ' @ ' ) then
2018-11-26 08:49:18 +02:00
success = true
end
if not success then
for _ , p in ipairs ( game.connected_players ) do
local word_front_trim = string.sub ( word , 2 , string.len ( word ) )
local word_back_trim = trimmed_word
local word_front_back_trim = string.sub ( word_front_trim , 0 , string.len ( word_front_trim ) - 1 )
local word_back_double_trim = string.sub ( word_back_trim , 0 , string.len ( word_back_trim ) - 1 )
2018-11-27 21:12:50 +02:00
if word_front_trim == ' admin ' or word_back_trim == ' admin ' or word_back_double_trim == ' admin ' or word_front_back_trim == ' admin ' then
admin_call = true
word = ' admin '
end
2018-11-26 08:49:18 +02:00
if admin_call and p.admin then
2019-05-16 12:10:56 +02:00
local message = { ' chat_triggers.mention_success ' , prefix , game.get_player ( event.player_index ) . name , word }
2024-10-22 21:22:35 +02:00
p.print ( message , { color = Color.yellow } )
2018-11-26 08:49:18 +02:00
p.play_sound { path = ' utility/new_objective ' , volume_modifier = 1 }
success = true
end
if not admin_call and ( p.name : lower ( ) == word_front_trim or p.name : lower ( ) == word_back_trim or p.name : lower ( ) == word_back_double_trim or p.name : lower ( ) == word_front_back_trim ) then
if p.name == player.name then
2018-11-18 14:26:15 +02:00
if _DEBUG then
2024-10-22 21:22:35 +02:00
player.print ( { ' chat_triggers.mention_fail_mention_self ' , prefix } , { color = Color.red } )
2018-11-18 14:26:15 +02:00
end
2018-11-26 08:49:18 +02:00
success = true
break
2018-11-15 16:06:03 +02:00
end
2021-01-19 15:36:25 +02:00
2024-10-22 21:22:35 +02:00
p.print ( { ' chat_triggers.mention_success_target ' , prefix , player.name } , { color = Color.yellow } )
2018-11-26 08:49:18 +02:00
p.play_sound { path = ' utility/new_objective ' , volume_modifier = 1 }
2021-01-21 16:46:18 +02:00
if p.character and p.character . valid then -- If player is dead and they don't have a character then they won't get the hovering notification.
local message = { ' chat_triggers.mention_success_target_floating ' , player.name }
rendering.draw_text ( { text = message , target_offset = { 0 , - 3 } , surface = p.surface , target = p.character , time_to_live = 360 , alignment = " center " , scale = 3 , players = { p.name } , color = { 1 , 1 , 1 , 1 } } )
end
2018-11-26 08:49:18 +02:00
success = true
if _DEBUG then
2024-10-22 21:22:35 +02:00
player.print ( prefix .. ' Successful mentioned ' .. p.name , { color = Color.red } )
2018-11-26 08:49:18 +02:00
end
break
2018-11-15 16:06:03 +02:00
end
end
2018-11-17 15:22:51 +02:00
end
2018-11-26 08:49:18 +02:00
if not success then
if admin_call then
word = ' no ' .. word .. ' s online! '
2018-11-15 17:03:46 +02:00
end
2018-11-26 08:49:18 +02:00
not_found = not_found + 1
table.insert ( cannot_mention , ( word .. ' , ' ) )
end
end
for _ , pname in ipairs ( cannot_mention ) do
missing_player_string = missing_player_string ~= nil and missing_player_string .. pname or pname
end
if missing_player_string ~= nil then
missing_player_string = string.sub ( missing_player_string , 1 , ( string.len ( missing_player_string ) - 2 ) )
if not_found > 1 then
2024-10-22 21:22:35 +02:00
player.print ( { ' chat_triggers.mention_not_found_plural ' , prefix , missing_player_string } , { color = Color.yellow } )
2018-11-26 08:49:18 +02:00
else
2024-10-22 21:22:35 +02:00
player.print ( { ' chat_triggers.mention_not_found_singular ' , prefix , missing_player_string } , { color = Color.yellow } )
2018-11-17 15:22:51 +02:00
end
2018-11-15 16:06:03 +02:00
end
2018-11-11 06:00:28 +02:00
end
2024-10-22 21:22:35 +02:00
if storage.config . hodor.enabled then
2018-12-06 13:18:52 +02:00
Event.add ( defines.events . on_console_chat , hodor )
2018-11-26 08:49:18 +02:00
end
2018-11-18 14:26:15 +02:00
2024-10-22 21:22:35 +02:00
if storage.config . auto_respond.enabled then
2018-12-06 13:18:52 +02:00
Event.add ( defines.events . on_console_chat , auto_respond )
end
2024-10-22 21:22:35 +02:00
if storage.config . mentions.enabled then
2018-12-06 13:18:52 +02:00
Event.add ( defines.events . on_console_chat , mentions )
end