This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/plugins.lua

110 lines
2.4 KiB
Lua
Raw Normal View History

2014-12-24 01:38:41 +01:00
function enable_plugin( filename )
2014-12-24 12:44:57 +01:00
-- Check if plugin is enabled
if plugin_enabled(filename) then
return 'Plugin '..filename..' is enabled'
end
-- Checks if plugin exists
if plugin_exists(filename) then
2014-12-24 01:38:41 +01:00
-- Add to the config table
table.insert(_config.enabled_plugins, filename)
save_config()
2014-12-24 01:38:41 +01:00
-- Reload the plugins
2014-12-24 12:44:57 +01:00
return reload_plugins( )
2014-12-24 01:38:41 +01:00
else
2014-12-24 12:44:57 +01:00
return 'Plugin '..filename..' does not exists'
end
end
function disable_plugin( name )
2014-12-24 12:44:57 +01:00
-- Check if plugins exists
if not plugin_exists(name) then
return 'Plugin '..name..' does not exists'
2014-12-24 12:44:57 +01:00
end
local k = plugin_enabled(name)
2014-12-24 12:44:57 +01:00
-- Check if plugin is enabled
if not k then
return 'Plugin '..name..' not enabled'
2014-12-24 01:38:41 +01:00
end
2014-12-24 12:44:57 +01:00
-- Disable and reload
table.remove(_config.enabled_plugins, k)
save_config( )
2014-12-24 12:44:57 +01:00
return reload_plugins(true)
2014-12-24 01:38:41 +01:00
end
function reload_plugins( )
plugins = {}
load_plugins()
return list_plugins(true)
end
2014-12-24 12:44:57 +01:00
-- Retruns the key (index) in the config.enabled_plugins table
function plugin_enabled( name )
for k,v in pairs(_config.enabled_plugins) do
2014-12-24 12:44:57 +01:00
if name == v then
return k
end
end
-- If not found
return false
end
-- Returns true if file exists in plugins folder
function plugin_exists( name )
for k,v in pairs(plugins_names()) do
if name..'.lua' == v then
2014-12-24 12:44:57 +01:00
return true
end
end
return false
end
2014-12-24 01:38:41 +01:00
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..'.lua' then
2014-12-24 01:38:41 +01:00
status = ''
end
end
if not only_enabled or status == '' then
text = text..v..'\b\t'..status..'\n'
2014-12-24 01:38:41 +01:00
end
end
return text
end
function run(msg, matches)
-- Show the available plugins
if matches[1] == '!plugins' then
return list_plugins()
end
-- Enable a plugin
if matches[1] == 'enable' then
print("enable: "..matches[2])
return enable_plugin(matches[2])
end
2014-12-24 12:44:57 +01:00
-- Disable a plugin
if matches[1] == 'disable' then
print("disable: "..matches[2])
return disable_plugin(matches[2])
end
-- Reload all the plugins!
if matches[1] == 'reload' then
return reload_plugins(true)
end
2014-12-24 01:38:41 +01:00
end
return {
2014-12-24 12:44:57 +01:00
description = "Enables, disables and reloads plugins",
usage = "!plugins, !plugins enable [plugin], !plugins disable [plugin], !plugins reload",
2014-12-24 01:38:41 +01:00
patterns = {
"^!plugins$",
2014-12-24 12:44:57 +01:00
"^!plugins? (enable) (.*)$",
"^!plugins? (disable) (.*)$",
"^!plugins? (reload)$"
2014-12-24 01:38:41 +01:00
},
run = run
}