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

179 lines
4.5 KiB
Lua
Raw Normal View History

2015-04-05 16:27:04 +02:00
do
2014-12-24 12:44:57 +01:00
-- Retruns the key (index) in the config.enabled_plugins table
2015-04-05 16:27:04 +02:00
local function plugin_enabled( name )
2015-04-06 23:28:09 +02:00
for k,v in pairs(_config.enabled_plugins) do
if name == v then
return k
end
end
-- If not found
return false
2014-12-24 12:44:57 +01:00
end
-- Returns true if file exists in plugins folder
2015-04-05 16:27:04 +02:00
local function plugin_exists( name )
2014-12-24 12:44:57 +01:00
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
2015-04-05 16:27:04 +02:00
local function list_plugins(only_enabled)
2015-04-06 23:28:09 +02:00
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
status = ''
end
end
if not only_enabled or status == '' then
-- get the name
v = string.match (v, "(.*)%.lua")
text = text..v..' '..status..'\n'
end
end
return text
2014-12-24 01:38:41 +01:00
end
2015-04-06 23:26:13 +02:00
local function reload_plugins( )
2015-04-06 23:28:09 +02:00
plugins = {}
load_plugins()
return list_plugins(true)
2015-04-06 23:26:13 +02:00
end
local function enable_plugin( filename )
2015-04-06 23:28:09 +02: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
-- Add to the config table
table.insert(_config.enabled_plugins, filename)
save_config()
-- Reload the plugins
return reload_plugins( )
else
return 'Plugin '..filename..' does not exists'
end
2015-04-06 23:26:13 +02:00
end
local function disable_plugin( name, chat )
2015-04-06 23:28:09 +02:00
-- Check if plugins exists
if not plugin_exists(name) then
return 'Plugin '..name..' does not exists'
end
local k = plugin_enabled(name)
-- Check if plugin is enabled
if not k then
return 'Plugin '..name..' not enabled'
end
-- Disable and reload
table.remove(_config.enabled_plugins, k)
save_config( )
return reload_plugins(true)
2015-04-06 23:26:13 +02:00
end
local function disable_plugin_on_chat(receiver, plugin)
if not plugin_exists(plugin) then
return "Plugin doesn't exists"
end
if not _config.disabled_plugin_on_chat then
_config.disabled_plugin_on_chat = {}
end
if not _config.disabled_plugin_on_chat[receiver] then
_config.disabled_plugin_on_chat[receiver] = {}
end
_config.disabled_plugin_on_chat[receiver][plugin] = true
save_config()
return 'Plugin '..plugin..' disabled on this chat'
end
local function reenable_plugin_on_chat(receiver, plugin)
if not _config.disabled_plugin_on_chat then
return 'There aren\'t any disabled plugin.'
end
if not _config.disabled_plugin_on_chat[receiver] then
return 'There aren\'t any disabled plugin for this chat.'
end
if not _config.disabled_plugin_on_chat[receiver][plugin] then
return 'This plugin is not disabled'
end
_config.disabled_plugin_on_chat[receiver][plugin] = false
return 'Plugin '..plugin..' is enabled again'
end
2015-04-05 16:27:04 +02:00
local function run(msg, matches)
2015-04-06 23:28:09 +02:00
-- Show the available plugins
if matches[1] == '!plugins' then
return list_plugins()
end
-- Reenable a plugin for this chat
if matches[1] == 'enable' and matches[3] == 'chat' then
local receiver = get_receiver(msg)
local plugin = matches[2]
print("enable "..plugin..' on this chat')
return reenable_plugin_on_chat(receiver, plugin)
end
2015-04-06 23:28:09 +02:00
-- Enable a plugin
if matches[1] == 'enable' then
print("enable: "..matches[2])
return enable_plugin(matches[2])
end
-- Disable a plugin on a chat
if matches[1] == 'disable' and matches[3] == 'chat' then
local plugin = matches[2]
local receiver = get_receiver(msg)
print("disable "..plugin..' on this chat')
return disable_plugin_on_chat(receiver, plugin)
end
2015-04-06 23:28:09 +02:00
-- Disable a plugin
if matches[1] == 'disable' then
print("disable: "..matches[2])
return disable_plugin(matches[2])
end
2015-04-06 23:28:09 +02:00
-- Reload all the plugins!
if matches[1] == 'reload' then
return reload_plugins(true)
end
2014-12-24 01:38:41 +01:00
end
return {
2015-04-06 23:28:09 +02:00
description = "Plugin to manage other plugins. Enable, disable or reload.",
usage = {
"!plugins: list all plugins.",
"!plugins enable [plugin]: enable plugin.",
"!plugins disable [plugin]: disable plugin.",
"!plugins disable [plugin] chat: disable plugin only this chat.",
"!plugins reload: reloads all plugins." },
2015-04-06 23:28:09 +02:00
patterns = {
"^!plugins$",
"^!plugins? (enable) ([%w_%.%-]+)$",
"^!plugins? (disable) ([%w_%.%-]+)$",
"^!plugins? (disable) ([%w_%.%-]+) (chat)",
"^!plugins? (enable) ([%w_%.%-]+) (chat)",
2015-04-06 23:28:09 +02:00
"^!plugins? (reload)$" },
run = run,
privileged = true
2015-04-05 16:27:04 +02:00
}
end