diff --git a/bot/bot.lua b/bot/bot.lua index d1acf70..bce7d64 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -47,7 +47,7 @@ function msg_valid(msg) print("Disabled channel") return false end - + return true end diff --git a/bot/utils.lua b/bot/utils.lua index 679c25e..1485132 100644 --- a/bot/utils.lua +++ b/bot/utils.lua @@ -177,20 +177,6 @@ function is_sudo(msg) return var end -function is_disabled(msg) - local var = false - if msg.text == "!channel enable" then - return var - end - -- Check users id in config - for v,channel in pairs(_config.disabled_channels) do - if channel == msg.to.id then - var = true - end - end - return var -end - -- Returns the name of the sender function get_name(msg) local name = msg.from.first_name diff --git a/plugins/channels.lua b/plugins/channels.lua index eb622ef..d5060a1 100644 --- a/plugins/channels.lua +++ b/plugins/channels.lua @@ -1,37 +1,68 @@ -function enable_channel( channel_id, channel_name ) - -- Add to the config table - table.remove(_config.disabled_channels, get_index(channel_id)) - save_config() - return "Channel "..channel_name.." enabled" -end - -function disable_channel( channel_id, channel_name ) - -- Disable - table.insert(_config.disabled_channels, channel_id) - save_config( ) - return "Channel "..channel_name.." disabled" -end - -function get_index( channel_id ) - for k,v in pairs(_config.disabled_channels) do - if channel_id == v then - return k - end +-- Checks if bot was disabled on specific chat +local function is_channel_disabled( receiver ) + if not _config.disabled_channels then + return false end - -- If not found - return false + + if _config.disabled_channels[receiver] == nil then + return false + end + + return _config.disabled_channels[receiver] end -function run(msg, matches) +local function enable_channel(receiver) + if not _config.disabled_channels then + _config.disabled_channels = {} + end + + if _config.disabled_channels[receiver] == nil then + return 'Channel isn\'t disabled' + end + + _config.disabled_channels[receiver] = false + + save_config() + return "Channel reenabled" +end + +local function disable_channel( receiver ) + if not _config.disabled_channels then + _config.disabled_channels = {} + end + + _config.disabled_channels[receiver] = false + + save_config() + return "Channel disabled" +end + +local function pre_process(msg) + local receiver = get_receiver(msg) + + -- If is sudo can reeanble the channel + if is_sudo(msg) then + if msg.text == "!channel enable" then + enable_channel(receiver) + end + end + + if is_channel_disabled(receiver) then + msg.text = "" + end + + return msg +end + +local function run(msg, matches) + local receiver = get_receiver(msg) -- Enable a channel if matches[1] == 'enable' then - print("enable: "..msg.to.id) - return enable_channel(msg.to.id, msg.to.title) + return enable_channel(receiver) end -- Disable a channel if matches[1] == 'disable' then - print("disable: "..msg.to.id) - return disable_channel(msg.to.id, msg.to.title) + return disable_channel(receiver) end end @@ -44,5 +75,6 @@ return { "^!channel? (enable)", "^!channel? (disable)" }, run = run, - privileged = true + privileged = true, + pre_process = pre_process } \ No newline at end of file