# frozen_string_literal: true require "kanal/core/plugins/plugin" require_relative "keyboard" module Kanal module Plugins # This module stores needed classes and helpers for batteries plugin module Batteries # Plugin with some batteries like .body property etc class BatteriesPlugin < Core::Plugins::Plugin def name :batteries end def setup(core) source_batteries core body_batteries core flow_batteries core attachments_batteries core keyboard_batteries core username_batteries core button_batteries core end def flow_batteries(core) core.add_condition_pack :flow do add_condition :any do met? do |_, _, _| true end end end end def source_batteries(core) # This parameter can be filled by different plugins/interfaces # to point from which source message came core.register_input_parameter :source core.add_condition_pack :source do add_condition :from do with_argument met? do |input, _, argument| input.source == argument end end end end def body_batteries(core) core.register_input_parameter :body core.register_output_parameter :body core.add_condition_pack :body do add_condition :starts_with do with_argument met? do |input, _, argument| if input.body.is_a? String input.body.start_with? argument else false end end end add_condition :ends_with do with_argument met? do |input, _, argument| if input.body.is_a? String input.body.end_with? argument else false end end end add_condition :contains do with_argument met? do |input, _, argument| if input.body.is_a? String input.body.include? argument else false end end end add_condition :contains_one_of do with_argument met? do |input, _, argument| met = false argument.each do |word| met = input.body.include? word break if met end met end end add_condition :equals do with_argument met? do |input, _, argument| input.body == argument end end end end def attachments_batteries(core) core.register_input_parameter :image core.register_input_parameter :audio core.register_input_parameter :video core.register_input_parameter :document core.register_output_parameter :image core.register_output_parameter :audio core.register_output_parameter :video core.register_output_parameter :document _this = self core.add_condition_pack :image do add_condition :exists do met? do |input, _core, _argument| !input.image.nil? end end add_condition :is do with_argument met? do |input, _, argument| if input.image.image? && input.image.send((argument.to_s + "?").to_sym) true else false end end end end core.add_condition_pack :audio do add_condition :exists do met? do |input, _core, _argument| !input.audio.nil? end end add_condition :is do with_argument met? do |input, _, argument| if input.audio.audio? && input.audio.send((argument.to_s + "?").to_sym) true else false end end end end core.add_condition_pack :video do add_condition :exists do met? do |input, _core, _argument| !input.video.nil? end end add_condition :is do with_argument met? do |input, _, argument| if input.video.video? && input.video.send((argument.to_s + "?").to_sym) true else false end end end end core.add_condition_pack :document do add_condition :exists do met? do |input, _core, _argument| !input.document.nil? end end add_condition :is do with_argument met? do |input, _, argument| if input.document.document? && input.document.send((argument.to_s + "?").to_sym) true else false end end end end end def keyboard_batteries(core) core.register_output_parameter :keyboard core.hooks.attach :output_just_created do |input, output| output.keyboard = Keyboard.new end end def username_batteries(core) core.register_input_parameter :username end def button_batteries(core) core.register_input_parameter :button_pressed core.add_condition_pack :button do add_condition :pressed do with_argument met? do |input, _, argument| input.button_pressed == argument end end end end end end end end