Mindre spammy på alle fronter

This commit is contained in:
Jon Michael Aanes 2018-06-10 16:26:50 +02:00
parent 33b6ed79fe
commit f351bb0623

View File

@ -22,6 +22,7 @@ if CONFIG.LUA_EXTRA_CPATH then package.cpath = package.cpath .. CONFIG.LUA_EXTR
local FARVEL_INTERVAL = 120
local MEME_INTERVAL = 30
local MEME_CHANCE = 0.3
--------------------------------------------------------------------------------
-- Make sure all required modules can be loaded
@ -551,18 +552,59 @@ local ERROR_MSG = {
'Do androids dream of electric sheep?',
}
local HILSNER = {
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hejsa, %s!',
'Hovsa, der var en %s',
'Long time no see, %s',
}
local LAST_USER_LOGIN
local LAST_TIME_USER_LOGGED_OUT = {}
local IRC_ALLOWED_TIMEOUT = 20 --20 * 60
bot:hook('OnJoin', function(user, channel)
io.write '...\r'; io.flush()
--
if user.nick == CONFIG.IRC_NICK then
-- On self join
io.write(string.format(' !! Joined %s\n', channel))
io.write(string.format(' !! Joined %s\n', channel))
if #imlib.font.list_fonts() == 0 then
common_error(channel, 'Hjælp! Min computer har ingen fonts! Hvorfor er fonts så svære på linux? Jeg har kigget i: "'..table.concat(imlib.font.list_paths(), '", "')..'"')
end
else
elseif user.nick ~= LAST_USER_LOGIN and (LAST_TIME_USER_LOGGED_OUT[user.nick] or 0) + IRC_ALLOWED_TIMEOUT < os.time() then
-- On other join
-- And that user wasn't the last one to join
-- And that user haven't been logged in for IRC_ALLOWED_TIMEOUT seconds.
LAST_USER_LOGIN = user.nick
human_delay()
bot:sendChat(channel, 'Hej, '..user.nick..'')
bot:sendChat(channel, choose(HILSNER):format(user.nick))
end
--
io.write ' \r'; io.flush()
end)
bot:hook('OnPart', function(user, channel)
assert(user)
if user.nick == CONFIG.IRC_NICK then return end
LAST_TIME_USER_LOGGED_OUT[user.nick] = os.time()
end)
bot:hook('OnQuit', function(user, message)
assert(user)
if user.nick == CONFIG.IRC_NICK then return end
LAST_TIME_USER_LOGGED_OUT[user.nick] = os.time()
end)
local function escape_pattern (text)
@ -573,18 +615,15 @@ local FORRIGE_FARVEL = 0
local FORRIGE_MEME = 0
local BOT_INTRODUCTION = [[Jeg er %s, en eksperimentel meme-robot, designet til at sprede memes. Er dog lidt sky, så kan ikke aflsøre mine memes, du må bare lede.]]
local function handle_message(bot, user, channel, message, is_slow_channel)
local function handle_message(bot, user, channel, message, is_fast_channel)
-- Direct commands
if message:lower():match('^'..escape_pattern(CONFIG.IRC_NICK)..'%f[%A]') then
local msg = message:sub(#CONFIG.IRC_NICK+1)
if msg:match '^%s*join%s+(#..-)%s*$' then
local channel = msg:match '^%s*join%s+(#..-)%s*$'
if is_fast_channel == 'direct' then
if message:match '^%s*join%s+(#..-)%s*$' then
local channel = message:match '^%s*join%s+(#..-)%s*$'
bot:sendChat(channel, "Det kan du tro! Jeg skal nok lige kigge indenom "..tostring(channel))
join_channel(channel)
else
bot:sendChat(channel, ERROR_MSG[math.random(#ERROR_MSG)])
return 'BOT'
end
return 'BOT'
end
-- Farvel msg
@ -603,7 +642,7 @@ local function handle_message(bot, user, channel, message, is_slow_channel)
end
-- Bot introduction
if message:lower():match('%f[%a]hvem%f[%A]') and (message:lower():match('%f[%a]'..escape_pattern(CONFIG.IRC_NICK)..'%f[%A]') or not is_slow_channel and message:lower():match '%f[%a]du%f[%A]') then
if message:lower():match('%f[%a]hvem%f[%A]') and (message:lower():match('%f[%a]'..escape_pattern(CONFIG.IRC_NICK)..'%f[%A]') or is_fast_channel and message:lower():match '%f[%a]du%f[%A]') then
human_delay()
local msg = BOT_INTRODUCTION:format(CONFIG.IRC_NICK)
bot:sendChat(channel, msg)
@ -611,9 +650,13 @@ local function handle_message(bot, user, channel, message, is_slow_channel)
end
-- Rest of this function is memes
-- Memes are restricted, a bit.
if is_slow_channel then
if not (FORRIGE_MEME + MEME_INTERVAL < os.time()) then return 'NOMEME' end
-- Memes are restricted, a bit, and wont trigger if:
-- 1. This is a slow channel, and the last meme was recently
-- 2. This is a non-direct channel, and the chance of memes are low.
if not is_fast_channel and not (FORRIGE_MEME + MEME_INTERVAL < os.time()) then
return 'NOMEME'
elseif is_fast_channel ~= 'direct' and math.random() >= MEME_CHANCE then
return 'LUCK'
end
-- Bait msg
@ -670,11 +713,18 @@ end
local DEBUG_CHANNEL = '#bot-test'
bot:hook("OnChat", function(user, channel, message)
local is_slow_channel = not (user == CONFIG.IRC_NICK or channel == DEBUG_CHANNEL)
if channel == CONFIG.IRC_NICK then channel = user.nick end
io.write '...\r'
io.flush()
local success, status = pcall(handle_message, bot, user, channel, message, is_slow_channel)
local is_fast_channel = user == channel == DEBUG_CHANNEL
if channel == CONFIG.IRC_NICK then
channel = user.nick
is_fast_channel = 'direct'
end
if message:match('^'..escape_pattern(CONFIG.IRC_NICK)..':%s*(.*)$') then
message = message:match('^'..escape_pattern(CONFIG.IRC_NICK)..':%s*(.*)$')
is_fast_channel = 'direct'
end
io.write '...\r'; io.flush()
local success, status = pcall(handle_message, bot, user, channel, message, is_fast_channel)
-- Handle error
if not success then