Check channel enabled as pre_processor

This commit is contained in:
yago 2015-04-12 23:08:42 +02:00
parent 0438abeee3
commit dca731c18e
3 changed files with 60 additions and 42 deletions

View File

@ -47,7 +47,7 @@ function msg_valid(msg)
print("Disabled channel") print("Disabled channel")
return false return false
end end
return true return true
end end

View File

@ -177,20 +177,6 @@ function is_sudo(msg)
return var return var
end 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 -- Returns the name of the sender
function get_name(msg) function get_name(msg)
local name = msg.from.first_name local name = msg.from.first_name

View File

@ -1,37 +1,68 @@
function enable_channel( channel_id, channel_name ) -- Checks if bot was disabled on specific chat
-- Add to the config table local function is_channel_disabled( receiver )
table.remove(_config.disabled_channels, get_index(channel_id)) if not _config.disabled_channels then
save_config() return false
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
end end
-- If not found
return false if _config.disabled_channels[receiver] == nil then
return false
end
return _config.disabled_channels[receiver]
end 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 -- Enable a channel
if matches[1] == 'enable' then if matches[1] == 'enable' then
print("enable: "..msg.to.id) return enable_channel(receiver)
return enable_channel(msg.to.id, msg.to.title)
end end
-- Disable a channel -- Disable a channel
if matches[1] == 'disable' then if matches[1] == 'disable' then
print("disable: "..msg.to.id) return disable_channel(receiver)
return disable_channel(msg.to.id, msg.to.title)
end end
end end
@ -44,5 +75,6 @@ return {
"^!channel? (enable)", "^!channel? (enable)",
"^!channel? (disable)" }, "^!channel? (disable)" },
run = run, run = run,
privileged = true privileged = true,
pre_process = pre_process
} }