From 1e24a125047342b798e68078e6d6adea7f201dd5 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 8 Jun 2018 13:09:05 +0200 Subject: [PATCH] Better merge and made nick configurable. --- config.example.lua | 12 +++++---- main.lua | 66 ++++++++++++++++++++++++---------------------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/config.example.lua b/config.example.lua index b0e74ac..0f78b06 100644 --- a/config.example.lua +++ b/config.example.lua @@ -1,20 +1,22 @@ return { -- IRC connection + IRC_NICK = 'memebot', + IRC_SERVER = 'some-domain.com', IRC_PORT = 6697, IRC_SECURE = true, -- If using SSL IRC_CHANNELS = { '#bot-test' }, - + -- Image generation IMGGEN_PATH_BRAINS = './images/brains/', IMGGEN_PATH_OUTPUT = './images/output/', IMGGEN_PATH_FONTS = { - '/usr/share/fonts/TTF', - '/usr/share/fonts/truetype/dejavu', - '/usr/local/share/fonts/TTF', + '/usr/share/fonts/TTF', + '/usr/share/fonts/truetype/dejavu', + '/usr/local/share/fonts/TTF', }, - + -- Image storage and presentation STORAGE_SERVER = 'image-host.com', STORAGE_SERVER_PATH = '~someuser/images/', diff --git a/main.lua b/main.lua index bc4ca54..2f05a3d 100644 --- a/main.lua +++ b/main.lua @@ -3,13 +3,21 @@ math.randomseed(os.time()) -local CONFIG = require 'config' +local CONFIG do + local success, config_or_error = pcall(require, 'config') + if not success then + error('Could not load config file: "./config.lua".\n'..config_or_error..'\nCould be that the config files doesn\'t exist. Make sure one exists, and try again.') + end + CONFIG = config_or_error +end if CONFIG.LUA_EXTRA_PATH then package.path = package.path .. CONFIG.LUA_EXTRA_PATH end if CONFIG.LUA_EXTRA_CPATH then package.cpath = package.cpath .. CONFIG.LUA_EXTRA_CPATH end local FARVEL_INTERVAL = 90 +-- TODO: Invite to bogus channels + -------------------------------------------------------------------------------- -- Meme utils @@ -262,9 +270,7 @@ end ---- -local BOT_NICK = 'ro-bot' - -local bot = irc.new { nick = BOT_NICK } +local bot = irc.new { nick = CONFIG.IRC_NICK } local FARVEL = { ['[Ff]arvel'] = {'ses', 'farvel'}, @@ -304,12 +310,12 @@ end local COMP_SENTENCES do local ORD_MED_ATTITYDE = { bedre = 'positiv', - dårligere = 'negativ' - hurtigere = 'positiv' - langsommere = 'negativt' - robust = 'positivt' + dårligere = 'negativ', + hurtigere = 'positiv', + langsommere = 'negativt', + robust = 'positivt', } - local COMP_SENTENCES = {} + COMP_SENTENCES = {} for ord, attityde in pairs(ORD_MED_ATTITYDE) do local first, second = '{A}', '{B}' if attityde == 'negativ' then first, second = second, first end @@ -373,7 +379,7 @@ local ERROR_MSG = { } bot:hook('OnJoin', function(user, channel) - if user.nick == BOT_NICK then + if user.nick == CONFIG.IRC_NICK then -- On self join io.write(string.format(' !! Joined %s\n', channel)) if #imlib.font.list_fonts() == 0 then @@ -394,8 +400,8 @@ local FORRIGE_FARVEL = 0 local function handle_message(bot, user, channel, message) -- Direct commands - if message:lower():match('^'..escape_pattern(BOT_NICK)..'%f[%A]') then - local msg = message:sub(#BOT_NICK+1) + 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+(#%a)' then local channel = msg:match '^%s*join%s+(#%a+)%s*$' bot:sendChat(channel, "Will do! I'll join "..tostring(channel)) @@ -452,7 +458,7 @@ local function handle_message(bot, user, channel, message) end bot:hook("OnChat", function(user, channel, message) - if channel == BOT_NICK then channel = user.nick end + 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) @@ -477,37 +483,33 @@ bot:hook('OnRaw', function (line) io.write((" [RAW]: %s\n"):format(line)) end) - --- TODO: Invite to bogus channels - -------------------------------------------------------------------------------- -- Main run local function init_memebot () -bot:connect { - host = CONFIG.IRC_SERVER, - port = CONFIG.IRC_PORT, - secure = CONFIG.IRC_SECURE, -} + bot:connect { + host = CONFIG.IRC_SERVER, + port = CONFIG.IRC_PORT, + secure = CONFIG.IRC_SECURE, + } -io.write ('Memebot has started up as "'..BOT_NICK..'"\n') + io.write ('Memebot has started up as "'..CONFIG.IRC_NICK..'"\n') -assert(CONFIG.IRC_CHANNELS == nil or type(CONFIG.IRC_CHANNELS) == 'table') -for _, channel in ipairs(CONFIG.IRC_CHANNELS or {}) do - bot:join(channel) -end + assert(CONFIG.IRC_CHANNELS == nil or type(CONFIG.IRC_CHANNELS) == 'table') + for _, channel in ipairs(CONFIG.IRC_CHANNELS or {}) do + bot:join(channel) + end - -while true do - bot:think() - sleep(0.5) -end + while true do + bot:think() + sleep(0.5) + end end -------------------------------------------------------------------------------- if ... then - error 'Cannot be loaded like library' + error 'Cannot load Memebot as a library' else init_memebot() end