-- 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 "Self Is Not Off :)"
	end
	
	_config.disabled_channels[receiver] = false

	save_config()
	return "Self Is On Now :D"
end

local function disable_channel( receiver )
	if not _config.disabled_channels then
		_config.disabled_channels = {}
	end
	
	_config.disabled_channels[receiver] = true

	save_config()
	return "Self Is Off Now :/"
end

local function pre_process(msg)
	local receiver = get_receiver(msg)
	
	-- If sender is moderator then re-enable the channel
	if is_sudo(msg) then
	  if msg.text == "/self on" or msg.text == "/Self on" or msg.text == "!self on" or msg.text == "!Self on" then
	  
	    enable_channel(receiver)
	  end
	end

  if is_channel_disabled(receiver) then
  	msg.text = ""
  end
-----------------------
         local autoleave = 'autoleave'
         local bot_id = our_id 
         local receiver = get_receiver(msg)
           if not redis:get(autoleave) then
    if msg.service and msg.action.type == "chat_add_user" and msg.action.user.id == tonumber(bot_id) and not is_sudo(msg) then
       send_large_msg(receiver, "Don't Invite Me.", ok_cb, false)
       chat_del_user(receiver, 'user#id'..bot_id, ok_cb, false)
	   leave_channel(receiver, ok_cb, false)
    end
end
	return msg
end
-------------------
local function run(msg, matches)
	local receiver = get_receiver(msg)
	-- Enable a channel
	
	local hash = 'usecommands:'..msg.from.id..':'..msg.to.id
    redis:incr(hash)
	if not is_sudo(msg) then
	return nil
	end
	if matches[1] == 'on' then
		return enable_channel(receiver)
	end
	-- Disable a channel
	if matches[1] == 'off' then
		return disable_channel(receiver)
	end
	           ---------------------
if matches[1] == 'help'  then
    if msg.to.type == 'channel' or msg.to.type == 'chat' then
	local text = "ğŸ”´!self on - self off(Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ ÛŒØ§ ØºÛŒØ±Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ Ø±Ø¨Ø§Øª)\nğŸ”´!plist(Ù†Ù…Ø§ÛŒØ´ Ù„ÛŒØ³Øª Ø§Ù�Ø²ÙˆÙ†Ù‡(Ù¾Ù„Ø§Ú¯ÛŒÙ†)\nğŸ”´!pl + (plugin name)(Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ Ù¾Ù„Ø§Ú¯ÛŒÙ†(Ø§Ù�Ø²ÙˆÙ†Ù‡)\nğŸ”´!pl - (plugin name)(ØºÛŒØ±Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ Ù¾Ù„Ø§Ú¯ÛŒÙ†(Ø§Ù�Ø²ÙˆÙ†Ù‡)\nğŸ”´!reload(Ø¨Ø§Ø²Ù†Ú¯Ø±ÛŒ Ùˆ Ø§Ø¹Ù…Ø§Ù„ ØªØºÛŒÛŒØ±Ø§Øª Ø§Ù�Ø²ÙˆÙ†Ù‡ Ù‡Ø§) \nğŸ”´!pl - (name) chat (Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ Ù¾Ù„Ø§Ú¯ÛŒÙ†-Ø§Ù�Ø²ÙˆÙ†Ù‡ Ø¯Ø± Ú¯Ø±ÙˆÙ‡ Ø®Ø§Øµ)\nğŸ”´!pl + (name) chat (ØºÛŒØ±Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ Ù¾Ù„Ø§Ú¯ÛŒÙ†-Ø§Ù�Ø²ÙˆÙ†Ù‡ Ø¯Ø± Ú¯Ø±ÙˆÙ‡ Ø®Ø§Øµ)\nğŸ”´!save (name) (by reply) ()\n ğŸ”´!dl plugin name(Ø¯Ø§Ù†Ù„ÙˆØ¯ Ù¾Ù„Ø§Ú¯ÛŒÙ†-Ø§Ù�Ø²ÙˆÙ†Ù‡)\nğŸ”´!pvblock [username|id|reply] (Ù…Ø³Ø¯ÙˆØ¯ Ø³Ø§Ø²ÛŒ Ú©Ø§Ø±Ø¨Ø± Ø¨Ø§ ÛŒÙˆØ²Ø±Ù†ÛŒÙ…|Ø´Ù†Ø§Ø³Ù‡|Ø±ÛŒÙ¾Ù„ÛŒ)\n ğŸ”´Ø¯Ø³ØªÙˆØ±Ø§Øª Ù…Ø¯ÛŒØ±ÛŒØªÛŒ Ú¯Ø±ÙˆÙ‡ğŸ‘‡\n ğŸ”´!setname (name)(ØªØºÛŒÛŒØ±Ø§Øª Ù†Ø§Ù… Ú¯Ø±ÙˆÙ‡)\nğŸ”´!link (Ø¯Ø±ÛŒØ§Ù�Øª Ù„ÛŒÙ†Ú© Ú¯Ø±ÙˆÙ‡) \nğŸ”´!newlink(Ø¯Ø±ÛŒØ§Ù�Øª Ù„ÛŒÙ†Ú© Ø¬Ø¯ÛŒØ¯) \n ğŸ”´!setlink [link](ØªÙ†Ø¸ÛŒÙ… Ù„ÛŒÙ†Ú© Ú¯Ø±ÙˆÙ‡)\nğŸ”´!tosuper (Ø§Ø±ØªÙ‚Ø§Ø¡ Ú¯Ø±ÙˆÙ‡ Ø¨Ù‡ Ø³ÙˆÙ¾Ø±Ú¯Ø±ÙˆÙ‡)\nğŸ”´!setdes (text) (ØªÙ†Ø¸ÛŒÙ… Ø¯Ø±Ø¨Ø§Ø±Ù‡ Ú¯Ø±ÙˆÙ‡)\n ğŸ”´!kick [username|id|reply](Ø§Ø®Ø±Ø§Ø¬ Ø¹Ø¶Ùˆ Ø¨Ø§ ÛŒÙˆØ²Ø±Ù†ÛŒÙ…|Ø´Ù†Ø§Ø³Ù‡|Ø±ÛŒÙ¾Ù„ÛŒ)\n ğŸ”´!inv [username|id|reply](Ø¯Ø¹ÙˆØª Ø¹Ø¶Ùˆ ÛŒØ§ ÛŒÙˆØ²Ø±Ù†ÛŒÙ…|Ø´Ù†Ø§Ø³Ù‡|Ø±ÛŒÙ¾Ù„ÛŒ)\nğŸ”´!id @username (by reply) (Ø¯Ø±ÛŒØ§Ù�Øª Ø´Ù†Ø§Ø³Ù‡ Ù�Ø±Ø¯ Ø¨Ø§ Ø¯Ø³ØªÙˆØ± Ù…Ù‚Ø§Ø¨Ù„)\nğŸ”´!gpid (Ø¯Ø±ÛŒØ§Ù�Øª Ø´Ù†Ø§Ø³Ù‡ Ú¯Ø±ÙˆÙ‡)\nğŸ”´Ù�Ù‚Ø· Ø¯Ø± Ø³ÙˆÙ¾Ø± Ú¯Ø±ÙˆÙ‡ ğŸ‘‡ \nğŸ”´!silent [username|id|reply] (Ø³Ø§ÛŒÙ„Ù†Øª Ú©Ø±Ø¯Ù† Ú©Ø§Ø±Ø¨Ø± Ø¨Ø§ ÛŒÙˆØ²Ø±Ù†ÛŒÙ…|Ø´Ù†Ø§Ø³Ù‡|Ø±ÛŒÙ¾Ù„ÛŒ)\nğŸ”´!unsilent [username|id|reply] (Ø®Ø§Ø±Ø¬ Ú©Ø±Ø¯Ù† Ø§Ø² Ø­Ø§Ù„Øª Ø³Ø§ÛŒÙ„Ù†Øª Ú©Ø±Ø¯Ù† Ú©Ø§Ø±Ø¨Ø± Ø¨Ø§ ÛŒÙˆØ²Ø±Ù†ÛŒÙ…|Ø´Ù†Ø§Ø³Ù‡|Ø±ÛŒÙ¾Ù„ÛŒ)\nğŸ”´!silentlist(Ù†Ù…Ø§ÛŒØ´ Ù„ÛŒØ³Øª Ø§Ù�Ø±Ø§Ø¯ Ø³Ø§ÛŒÙ„Ù†Øª Ø´Ø¯Ù‡) \nğŸ”´!clean silentlist(Ø­Ø°Ù� Ø§Ù�Ø±Ø§Ø¯ Ù„ÛŒØ³Øª Ø³Ø§ÛŒÙ„Ù†Øª) \n ğŸ”´!mute all(Ù�ÛŒÙ„ØªØ± ØªÙ…Ø§Ù…ÛŒ Ú¯Ù�ØªÚ¯Ùˆ Ù‡Ø§)\nğŸ”´!unmute all(ØºÛŒØ±Ù�Ø¹Ø§ Ú©Ø±Ø¯Ù† Ù�ÛŒÙ„ØªØ± Ú¯Ù�ØªÚ¯Ùˆ Ù‡Ø§) \nğŸ”´!mute status(Ø¨Ø±Ø±Ø³ÛŒ ÙˆØ¶Ø¹ÛŒØª Ù�Ø¹Ø§Ù„ Ø¨ÙˆØ¯Ù† ÛŒØ§ ØºÛŒØ±Ù�Ø¹Ø§Ù„ Ø¨ÙˆØ¯Ù† Ù�ÛŒÙ„ØªØ± Ú¯Ù�ØªÚ¯Ùˆ Ù‡Ø§) \nğŸ”´Ø¯Ø³ØªÙˆØ±Ø§Øª Ø³Ø±Ú¯Ø±Ù…ÛŒğŸ‘‡ \nğŸ”´!sticker (by reply)(ØªØ¨Ø¯ÛŒÙ„ Ø¹Ú©Ø³ Ø¨Ù‡ Ø§Ø³ØªÛŒÚ©Ø± Ø¨Ø§ Ø±ÛŒÙ¾Ù„ÛŒ) \nğŸ”´!photo (by reply) (ØªØ¨Ø¯ÛŒÙ„ Ø§Ø³ØªÛŒÚ©Ø± Ø¨Ù‡ Ø¹Ú©Ø³ Ø¨Ø§ Ø±ÛŒÙ¾Ù„ÛŒ)\nğŸ”´!tr fa-en word(Ù…ØªØ±Ø¬Ù…) \nğŸ”´!sticker name/word/emoji (ØªØ¨Ø¯ÛŒÙ„ Ø¨Ù‡ Ø§Ø³ØªÛŒÚ©Ø±|Ù†Ø§Ù…|Ø´Ú©Ù„Ú© Ùˆ...)\nğŸ”´!photo name/word/emoji(ØªØ¨Ø¯ÛŒÙ„ Ø¨Ù‡ Ø¹Ú©Ø³|Ù†Ø§Ù…|Ø´Ú©Ù„Ú© Ùˆ...) \nğŸ”´!calc number +-*Ã· (Ù…Ø§Ø´ÛŒÙ† Ø­Ø³Ø§Ø¨)\nğŸ”´!delplugin nameplugin(Ø­Ø°Ù� Ù¾Ù„Ø§Ú¯ÛŒÙ†-Ø§Ù�Ø²ÙˆÙ†Ù‡) \n ğŸ”´!weather city(Ø¯Ø±ÛŒØ§Ù�Øª ÙˆØ¶Ø¹ÛŒØª Ø§Ø¨ Ùˆ Ù‡ÙˆØ§ÛŒ Ø´Ù‡Ø± Ù…ÙˆØ±Ø¯ Ù†Ø¸Ø±)\nğŸ”´!time(Ø¯Ø±ÛŒØ§Ù�Øª Ø²Ù…Ø§Ù†) \nğŸ”´!voice text (ØªØ¨Ø¯ÛŒÙ„ Ù…ØªÙ† Ø¨Ù‡ ØµØ¯Ø§)\nğŸ”´!clean msg number(Ø­Ø°Ù� Ù¾ÛŒØ§Ù… Ù‡Ø§ÛŒ Ø§Ø®ÛŒØ±) \n> !autoleave on/off(Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ ÛŒØ§ ØºÛŒØ±Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ-Ø®Ø±ÙˆØ¬ Ø®ÙˆØ¯Ú©Ø§Ø± Ù¾Ø³ Ø§Ø² Ø§Ø¶Ø§Ù�Ù‡ Ú©Ø±Ø¯Ù† Ø´Ù…Ø§ Ø¨Ù‡ Ú¯Ø±ÙˆÙ‡) \n > !antiflood on/off(Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ ÛŒØ§ ØºÛŒØ±Ù�Ø¹Ø§Ù„ Ø³Ø§Ø²ÛŒ-Ø­Ø³Ø§Ø³ÛŒØª Ù¾ÛŒØ§Ù… Ù…Ú©Ø±Ø±)\n > !markread on|off(Ø®ÙˆØ§Ù†Ø¯Ù‡ Ø´Ø¯Ù† Ø®ÙˆØ¯Ú©Ø§Ø± Ù¾ÛŒØ§Ù… Ù‡Ø§ Ùˆ ØºÛŒØ±Ù�Ø¹Ø§Ù„ Ú©Ø±Ø¯Ù† Ø¢Ù†)\n > !pvsetflood(ØªÙ†Ø¸ÛŒÙ… Ù¾ÛŒØ§Ù… Ù…Ú©Ø±Ø±-Ø­Ø³Ø§Ø³ÛŒØª Ø¨Ø± Ø­Ø³Ø¨ ØªØ¹Ø¯Ø§Ø¯ Ù¾ÛŒØ§Ù… Ø§Ø±Ø³Ø§Ù„ÛŒ)\n> !pvfloodtime(ØªÙ†Ø¸ÛŒÙ… Ù¾ÛŒØ§Ù… Ù…Ú©Ø±Ø±-Ø­Ø³Ø§Ø³ÛŒØª Ø¨Ø± Ø­Ø³Ø¨ Ø²Ù…Ø§Ù†) \n > !pvblock(Ù…Ø³Ø¯ÙˆØ¯ Ø³Ø§Ø²ÛŒ Ú©Ø§Ø±Ø¨Ø±)\n> !pvunblock(Ø±Ù�Ø¹ Ù…Ø³Ø¯ÙˆØ¯ Ø³Ø§Ø²ÛŒ Ú©Ø§Ø±Ø¨Ø±) "
 send_large_msg('user#id'..msg.from.id, text.."\n", ok_cb, false)
			return "Help was sent in your private message!"
		end
end 
-----------------------
     if matches[1] == 'autoleave' and is_sudo(msg) then
local hash = 'autoleave'
--Enable Auto Leave
     if matches[2] == 'enable' then
    redis:del(hash)
   return 'Auto leave has been enabled'
--Disable Auto Leave
     elseif matches[2] == 'disable' then
    redis:set(hash, true)
   return 'Auto leave has been disabled'
--Auto Leave Status
      elseif matches[2] == 'status' then
      if not redis:get(hash) then
   return 'Auto leave is enable'
       else
   return 'Auto leave is disable'
         end
      end
   end
end

return {
	description = "Plugin to manage channels. Enable or disable channel.", 
	usage = {
		"/channel enable: enable current channel",
		"/channel disable: disable current channel" },
	patterns = {
		"^[!/#](autoleave) (.*)$",
		"^[!/#](help)$",
		"^[!/][Ss]elf (on)",
		"^[!/][Ss]elf (off)" }, 
	run = run,
	--privileged = true,
	moderated = true,
	pre_process = pre_process
}
