Fixe ein ziemlich seltsames Problem mit /plugins

This commit is contained in:
Andreas Bielawski 2016-08-15 00:36:33 +02:00
parent b720647523
commit c931694418
3 changed files with 14 additions and 19 deletions

View File

@ -28,14 +28,12 @@ function bot:init(config) -- The function run when the bot is started or reloade
self.plugins = {} -- Load plugins. self.plugins = {} -- Load plugins.
enabled_plugins = load_plugins() enabled_plugins = load_plugins()
t = {}
for k,v in pairs(enabled_plugins) do for k,v in pairs(enabled_plugins) do
local p = require('otouto.plugins.'..v) local p = require('otouto.plugins.'..v)
-- print('loading plugin',v) -- print('loading plugin',v)
self.plugins[k] = p self.plugins[k] = p
self.plugins[k].name = v self.plugins[k].name = v
if p.init then p.init(self, config) end if p.init then p.init(self, config) end
if not p.triggers then p.triggers = t end
end end
print('Bot started successfully as:\n@' .. self.info.username .. ', AKA ' .. self.info.first_name ..' ('..self.info.id..')') print('Bot started successfully as:\n@' .. self.info.username .. ', AKA ' .. self.info.first_name ..' ('..self.info.id..')')
@ -45,7 +43,6 @@ function bot:init(config) -- The function run when the bot is started or reloade
self.last_cron = self.last_cron or os.date('%M') -- Last cron job. self.last_cron = self.last_cron or os.date('%M') -- Last cron job.
self.last_database_save = self.last_database_save or os.date('%H') -- Last db save. self.last_database_save = self.last_database_save or os.date('%H') -- Last db save.
self.is_started = true -- and whether or not the bot should be running. self.is_started = true -- and whether or not the bot should be running.
end end
function bot:on_msg_receive(msg, config) -- The fn run whenever a message is received. function bot:on_msg_receive(msg, config) -- The fn run whenever a message is received.
@ -79,10 +76,9 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
msg = service_modify_msg(msg) msg = service_modify_msg(msg)
end end
for n=1, #self.plugins do for _, plugin in ipairs(self.plugins) do
local plugin = self.plugins[n] match_plugins(self, msg, config, plugin)
match_plugins(self, msg, config, plugin) end
end
end end
function bot:on_callback_receive(callback, msg, config) -- whenever a new callback is received function bot:on_callback_receive(callback, msg, config) -- whenever a new callback is received
@ -182,12 +178,12 @@ function bot:process_inline_query(inline_query, config) -- When an inline query
utilities.answer_inline_query(self, inline_query, nil, 0, true) utilities.answer_inline_query(self, inline_query, nil, 0, true)
end end
-- main
function bot:run(config) function bot:run(config)
bot.init(self, config) bot.init(self, config)
while self.is_started do while self.is_started do
-- Update loop -- Update loop
local res = bindings.getUpdates(self, { timeout = 20, offset = self.last_update + 1 } ) local res = bindings.getUpdates(self, { timeout = 20, offset = self.last_update+1 } )
if res then if res then
-- Iterate over every new message. -- Iterate over every new message.
for n=1, #res.result do for n=1, #res.result do
@ -205,7 +201,7 @@ function bot:run(config)
print('Connection error while fetching updates.') print('Connection error while fetching updates.')
end end
-- Run cron jobs every minute. -- Run cron jobs every minute.
if self.last_cron ~= os.date('%M') then if self.last_cron ~= os.date('%M') then
self.last_cron = os.date('%M') self.last_cron = os.date('%M')
utilities.save_data(self.info.username..'.db', self.database) -- Save the database. utilities.save_data(self.info.username..'.db', self.database) -- Save the database.
@ -262,7 +258,7 @@ function match_inline_plugins(self, inline_query, config, plugin)
end end
function match_plugins(self, msg, config, plugin) function match_plugins(self, msg, config, plugin)
local match_table = plugin.triggers local match_table = plugin.triggers or {}
for n=1, #match_table do for n=1, #match_table do
local trigger = plugin.triggers[n] local trigger = plugin.triggers[n]
if string.match(msg.text_lower, trigger) then if string.match(msg.text_lower, trigger) then

View File

@ -5,12 +5,12 @@ local bot = require('otouto.bot')
function plugin_manager:init(config) function plugin_manager:init(config)
plugin_manager.triggers = { plugin_manager.triggers = {
"^/plugins$", "^/plugins$",
"^/plugins? (enable) ([%w_%.%-]+) (chat) (%d+)$",
"^/plugins? (enable) ([%w_%.%-]+) (chat)$",
"^/plugins? (disable) ([%w_%.%-]+) (chat) (%d+)$",
"^/plugins? (disable) ([%w_%.%-]+) (chat)$",
"^/plugins? (enable) ([%w_%.%-]+)$", "^/plugins? (enable) ([%w_%.%-]+)$",
"^/plugins? (disable) ([%w_%.%-]+)$", "^/plugins? (disable) ([%w_%.%-]+)$",
"^/plugins? (enable) ([%w_%.%-]+) (chat) (%d+)",
"^/plugins? (enable) ([%w_%.%-]+) (chat)",
"^/plugins? (disable) ([%w_%.%-]+) (chat) (%d+)",
"^/plugins? (disable) ([%w_%.%-]+) (chat)",
"^/plugins? (reload)$", "^/plugins? (reload)$",
"^/(reload)$" "^/(reload)$"
} }

View File

@ -4,10 +4,9 @@ preview.command = 'preview <link>'
function preview:init(config) function preview:init(config)
preview.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('preview', true).table preview.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('preview', true).table
preview.doc = [[``` preview.doc = [[*
]]..config.cmd_pat..[[preview <link> ]]..config.cmd_pat..[[preview* _<URL>_
Returns a full-message, "unlinked" preview. Erstellt einen Preview-Link]]
```]]
end end
function preview:action(msg) function preview:action(msg)