Class: Discordrb::Commands::CommandChain
- Inherits:
-
Object
- Object
- Discordrb::Commands::CommandChain
- Defined in:
- lib/discordrb/commands/parser.rb
Overview
Command chain, may have multiple commands, nested and commands
Instance Method Summary collapse
- #execute(event) ⇒ Object
- #execute_bare(event) ⇒ Object
-
#initialize(chain, bot, subchain = false) ⇒ CommandChain
constructor
A new instance of CommandChain.
Constructor Details
#initialize(chain, bot, subchain = false) ⇒ CommandChain
Returns a new instance of CommandChain.
57 58 59 60 61 62 |
# File 'lib/discordrb/commands/parser.rb', line 57 def initialize(chain, bot, subchain = false) @attributes = bot.attributes @chain = chain @bot = bot @subchain = subchain end |
Instance Method Details
#execute(event) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/discordrb/commands/parser.rb', line 163 def execute(event) old_chain = @chain @bot.debug 'Executing bare chain' result = (event) @chain_args ||= [] @bot.debug "Found chain args #{@chain_args}, preliminary result #{result}" @chain_args.each do |arg| case arg.first when 'repeat' new_result = '' executed_chain = divide_chain(old_chain).last arg[1].to_i.times do new_result << CommandChain.new(executed_chain, @bot).execute(event) end result = new_result # TODO: more chain arguments end end result end |
#execute_bare(event) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/discordrb/commands/parser.rb', line 64 def (event) b_start = -1 b_level = 0 result = '' quoted = false hacky_delim, hacky_space, hacky_prev = [0xe001, 0xe002, 0xe003].pack('U*').chars @chain.each_char.each_with_index do |char, index| # Quote begin if char == @attributes[:quote_start] && !quoted quoted = true next end # Quote end if char == @attributes[:quote_end] && quoted quoted = false next end if char == @attributes[:chain_delimiter] && quoted result += hacky_delim next end if char == @attributes[:previous] && quoted result += hacky_prev next end if char == ' ' && quoted result += hacky_space next end if char == @attributes[:sub_chain_start] && !quoted b_start = index if b_level == 0 b_level += 1 end result << char if b_level <= 0 next unless char == @attributes[:sub_chain_end] && !quoted b_level -= 1 next unless b_level == 0 nested = @chain[b_start + 1..index - 1] subchain = CommandChain.new(nested, @bot, true) result << subchain.execute(event) end event.respond("Your subchains are mismatched! Make sure you don't have any extra #{@attributes[:sub_chain_start]}'s or #{@attributes[:sub_chain_end]}'s") unless b_level == 0 @chain = result @chain_args, @chain = divide_chain(@chain) prev = '' chain_to_split = @chain # Don't break if a command is called the same thing as the chain delimiter chain_to_split.slice!(1..-1) if chain_to_split.start_with?(@attributes[:chain_delimiter]) first = true split_chain = chain_to_split.split(@attributes[:chain_delimiter]) split_chain.each do |command| command = @attributes[:chain_delimiter] + command if first && @chain.start_with?(@attributes[:chain_delimiter]) first = false command.strip! # Replace the hacky delimiter that was used inside quotes with actual delimiters command.gsub! hacky_delim, @attributes[:chain_delimiter] first_space = command.index ' ' command_name = first_space ? command[0..first_space - 1] : command arguments = first_space ? command[first_space + 1..-1] : '' # Append a previous sign if none is present arguments << @attributes[:previous] unless arguments.include? @attributes[:previous] arguments.gsub! @attributes[:previous], prev # Replace hacky previous signs with actual ones arguments.gsub! hacky_prev, @attributes[:previous] arguments = arguments.split ' ' # Replace the hacky spaces with actual spaces arguments.map! do |elem| elem.gsub hacky_space, ' ' end # Finally execute the command prev = @bot.execute_command(command_name.to_sym, event, arguments, split_chain.length > 1 || @subchain) end prev end |