Files
factorio-scenario-ExpCluster/modules/ExpGamingBot/autoMessage/control.lua
2018-10-26 14:35:05 +01:00

79 lines
3.2 KiB
Lua

--- Prints a message every 15 minutes to chat.
-- @module ExpGamingBot.autoMessage
-- @author Cooldude2606
-- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE
-- @alais ThisModule
-- Module Require
local Server = require('ExpGamingCore.Server')
local Game = require('FactorioStdLib.Game')
local Role -- ExpGamingCore.Role@4.0.0
local Sync -- ExpGamingCore.Sync@4.0.0
-- Local Varibles
-- Module Define
local module_verbose = false
local ThisModule = {
on_init=function()
if loaded_modules['ExpGamingCore.Role@^4.0.0'] then Role = require('ExpGamingCore.Role@^4.0.0') end
if loaded_modules['ExpGamingCore.Sync@^4.0.0'] then Sync = require('ExpGamingCore.Sync@^4.0.0') end
end,
on_post=function()
--code
end
}
-- Event Handlers Define
script.on_init(function(event)
Server.new_thread{
name='auto-message',
timeout=54000, -- 3240000 = 15 hours dont make the mistake i did, 54000 is 15 minutes
reopen=true,
data={
high_role= 'Owner',
low_role= 'Regular',
low={
{'ExpGamingBot-autoMessage.join-us'},
{'ExpGamingBot-autoMessage.discord'},
{'ExpGamingBot-autoMessage.website'},
{'ExpGamingBot-autoMessage.custom-commands'},
{'ExpGamingBot-autoMessage.read-readme'}
}
}
}:on_event('timeout',function(self)
local data = self.data
if not data.high_role or not data.low_role
or not data.low then self.reopen = false return end
game.print{'ExpGamingBot-autoMessage.message',{'ExpGamingBot-autoMessage.players-online',#game.connected_players}}
game.print{'ExpGamingBot-autoMessage.message',{'ExpGamingBot-autoMessage.map-time',tick_to_display_format(game.tick)}}
self.reopen = true
end):on_event(defines.events.on_player_joined_game,function(self,event)
-- the _env should be auto loaded but it does not for some reason
local _ENV = self._env.setmetatable({self=self,event=event},{__index=self._env})
local player = Game.get_player(event)
if not player then return end
local data = self.data
if not data.high_role or not data.low_role
or not data.low then self.reopen = false return end
-- idk but this stoped working for no appent reason so i added more checks for nil values
if Role and Role.get_highest(player).index <= Role.get(data.low_role).index or player.admin then return end
for _,message in pairs(data.low) do
player_return({'ExpGamingBot-autoMessage.message',message},nil,player)
end
end):on_event('error',function(self,err)
-- the _env should be auto loaded but it does not for some reasonm this is an attempt to repair the upvalues
for index,name in pairs(self._env._order) do self._env.debug.setupvalue(1,index,self._env[name]) end
if Sync then Sync.emit_embeded{
title='Auto Message Error',
color=Color.to_hex(defines.textcolor.bg),
description='Auto Message Error - Closed Thread',
Error=err
} end
self.reopen = false
self:close()
end):open()
end)
-- Module Return
return ThisModule