Initial test of plugins handler
This commit is contained in:
parent
8ca367e83b
commit
d0a728e214
18
bot/bot.lua
18
bot/bot.lua
@ -22,11 +22,6 @@ end
|
||||
function ok_cb(extra, success, result)
|
||||
end
|
||||
|
||||
-- Callback to remove tmp files
|
||||
function rmtmp_cb(file_path, success, result)
|
||||
os.remove(file_path)
|
||||
end
|
||||
|
||||
function msg_valid(msg)
|
||||
-- if msg.from.id == our_id then
|
||||
-- return true
|
||||
@ -170,15 +165,12 @@ function on_binlog_replay_end ()
|
||||
-- See plugins/ping.lua as an example for cron
|
||||
end
|
||||
|
||||
-- load all plugins in the plugins/ directory
|
||||
-- Enable plugins in config.json
|
||||
function load_plugins()
|
||||
for k, v in pairs(scandir("plugins")) do
|
||||
-- Load only lua files
|
||||
if (v:match(".lua$")) then
|
||||
print("Loading plugin", v)
|
||||
t = loadfile("plugins/" .. v)()
|
||||
table.insert(plugins, t)
|
||||
end
|
||||
for k, v in pairs(config.enabled_plugins) do
|
||||
print("Loading plugin", v)
|
||||
t = loadfile("plugins/" .. v)()
|
||||
table.insert(plugins, t)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
{
|
||||
"enabled_plugins": [ "plugins.lua", "echo.lua", "hello.lua" ],
|
||||
"rmtmp_delay": 20,
|
||||
"google_api_key": "",
|
||||
"log_file": "/var/www/html/log.txt",
|
||||
|
@ -67,6 +67,11 @@ function download_to_file( url , noremove )
|
||||
return file_path
|
||||
end
|
||||
|
||||
-- Callback to remove a file
|
||||
function rmtmp_cb(file_path, success, result)
|
||||
os.remove(file_path)
|
||||
end
|
||||
|
||||
function vardump(value, depth, key)
|
||||
local linePrefix = ""
|
||||
local spaces = ""
|
||||
@ -133,6 +138,7 @@ function is_sudo(msg)
|
||||
return var
|
||||
end
|
||||
|
||||
-- Returns the name of the sender
|
||||
function get_name(msg)
|
||||
local name = msg.from.first_name
|
||||
if name == nil then
|
||||
@ -140,3 +146,26 @@ function get_name(msg)
|
||||
end
|
||||
return name
|
||||
end
|
||||
|
||||
-- Returns at table of lua files inside plugins
|
||||
function plugins_names( )
|
||||
local files = {}
|
||||
for k, v in pairs(scandir("plugins")) do
|
||||
-- Ends with .lua
|
||||
if (v:match(".lua$")) then
|
||||
table.insert(files, v)
|
||||
end
|
||||
end
|
||||
return files
|
||||
end
|
||||
|
||||
-- Function name explains what it does.
|
||||
function file_exists(name)
|
||||
local f = io.open(name,"r")
|
||||
if f ~= nil then
|
||||
io.close(f)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
62
plugins/plugins.lua
Normal file
62
plugins/plugins.lua
Normal file
@ -0,0 +1,62 @@
|
||||
function enable_plugin( filename )
|
||||
-- Checks if file exists
|
||||
if file_exists('plugins/'..filename) then
|
||||
-- Add to the config table
|
||||
table.insert(config.enabled_plugins, filename)
|
||||
-- Reload the plugins
|
||||
reload_plugins( )
|
||||
else
|
||||
return 'Plugin does not exists'
|
||||
end
|
||||
end
|
||||
|
||||
function reload_plugins( )
|
||||
plugins = {}
|
||||
load_plugins()
|
||||
return list_plugins(true)
|
||||
end
|
||||
|
||||
function list_plugins(only_enabled)
|
||||
local text = ''
|
||||
for k, v in pairs( plugins_names( )) do
|
||||
-- ✔ enabled, ❌ disabled
|
||||
local status = '❌'
|
||||
-- Check if is enabled
|
||||
for k2, v2 in pairs(config.enabled_plugins) do
|
||||
if v == v2 then
|
||||
status = '✔'
|
||||
end
|
||||
end
|
||||
if not only_enabled or status == '✔' then
|
||||
text = text..v..' '..status..'\n'
|
||||
end
|
||||
end
|
||||
return text
|
||||
end
|
||||
|
||||
function run(msg, matches)
|
||||
-- Show the available plugins
|
||||
if matches[1] == '!plugins' then
|
||||
return list_plugins()
|
||||
end
|
||||
-- Reload all the plugins!
|
||||
if matches[1] == 'reload' then
|
||||
return reload_plugins(true)
|
||||
end
|
||||
-- Enable a plugin
|
||||
if matches[1] == 'enable' then
|
||||
print("enable: "..matches[2])
|
||||
return enable_plugin(matches[2])
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
description = "Enable / Disable plugins",
|
||||
usage = "!plugins",
|
||||
patterns = {
|
||||
"^!plugins$",
|
||||
"^!plugins (enable) ([%w%.]+)$",
|
||||
"^!plugins (reload)$"
|
||||
},
|
||||
run = run
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
|
||||
function run(msg, matches)
|
||||
plugins = {}
|
||||
load_plugins()
|
||||
return 'Plugins reloaded'
|
||||
end
|
||||
|
||||
return {
|
||||
description = "Reloads bot plugins",
|
||||
usage = "!reload",
|
||||
patterns = {"^!reload$"},
|
||||
run = run
|
||||
}
|
||||
|
Reference in New Issue
Block a user