Skip to content

Commit

Permalink
Check channel enabled as pre_processor
Browse files Browse the repository at this point in the history
  • Loading branch information
yagop committed Apr 12, 2015
1 parent 0438abe commit dca731c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
2 changes: 1 addition & 1 deletion bot/bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function msg_valid(msg)
print("Disabled channel")
return false
end

return true
end

Expand Down
14 changes: 0 additions & 14 deletions bot/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 54 additions & 22 deletions plugins/channels.lua
Original file line number Diff line number Diff line change
@@ -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))
-- Checks if bot was disabled on specific chat
local function is_channel_disabled( receiver )
if not _config.disabled_channels then
return false
end

if _config.disabled_channels[receiver] == nil then
return false
end

return _config.disabled_channels[receiver]
end

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 "..channel_name.." enabled"
return "Channel reenabled"
end

function disable_channel( channel_id, channel_name )
-- Disable
table.insert(_config.disabled_channels, channel_id)
save_config( )
return "Channel "..channel_name.." disabled"
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

function get_index( channel_id )
for k,v in pairs(_config.disabled_channels) do
if channel_id == v then
return k
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 not found
return false

if is_channel_disabled(receiver) then
msg.text = ""
end

return msg
end

function run(msg, matches)
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

Expand All @@ -44,5 +75,6 @@ return {
"^!channel? (enable)",
"^!channel? (disable)" },
run = run,
privileged = true
privileged = true,
pre_process = pre_process
}

0 comments on commit dca731c

Please sign in to comment.