Class: OnyxCord::Commands::CommandChain

Inherits:
Object
  • Object
show all
Defined in:
lib/onyxcord/commands/parser.rb

Overview

Command chain, may have multiple commands, nested and commands

Instance Method Summary collapse

Constructor Details

#initialize(chain, bot, subchain = false) ⇒ CommandChain

Returns a new instance of CommandChain.

Parameters:

  • chain (String)

    The string the chain should be parsed from.

  • bot (Commands::Bot)

    The bot that executes this command chain.

  • subchain (true, false) (defaults to: false)

    Whether this chain is a sub chain of another chain.



226
227
228
229
230
231
# File 'lib/onyxcord/commands/parser.rb', line 226

def initialize(chain, bot, subchain = false)
  @attributes = bot.attributes
  @chain = chain
  @bot = bot
  @subchain = subchain
end

Instance Method Details

#execute(event) ⇒ String

Divides the command chain into chain arguments and command chain, then executes them both.

Parameters:

  • event (CommandEvent)

    The event to execute the command with.

Returns:

  • (String)

    the result of the command chain execution.



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/onyxcord/commands/parser.rb', line 361

def execute(event)
  old_chain = @chain
  @bot.debug 'Executing bare chain'
  result = execute_bare(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
        chain_result = CommandChain.new(executed_chain, @bot).execute(event)
        new_result += chain_result if chain_result
      end

      result = new_result
      # TODO: more chain arguments
    end
  end

  result
end

#execute_bare(event) ⇒ String

Parses the command chain itself, including sub-chains, and executes it. Executes only the command chain, without its chain arguments.

Parameters:

  • event (CommandEvent)

    The event to execute the chain with.

Returns:

  • (String)

    the result of the execution.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/onyxcord/commands/parser.rb', line 237

def execute_bare(event)
  b_start = -1
  b_level = 0
  result = ''
  quoted = false
  escaped = false
  hacky_delim, hacky_space, hacky_prev, hacky_newline = [0xe001, 0xe002, 0xe003, 0xe004].pack('U*').chars

  @chain.each_char.with_index do |char, index|
    # Escape character
    if char == '\\' && !escaped && b_level <= 0
      escaped = true
      next
    elsif escaped
      result += char
      escaped = false
      next
    end

    if quoted
      # Quote end
      if char == @attributes[:quote_end]
        quoted = false
        next
      end

      if b_level <= 0
        case char
        when @attributes[:chain_delimiter]
          result += hacky_delim
          next
        when @attributes[:previous]
          result += hacky_prev
          next
        when ' '
          result += hacky_space
          next
        when "\n"
          result += hacky_newline
          next
        end
      end
    else
      case char
      when @attributes[:quote_start] # Quote begin
        quoted = true
        next
      when @attributes[:sub_chain_start]
        b_start = index if b_level.zero?
        b_level += 1
      end
    end

    result += char if b_level <= 0

    next unless char == @attributes[:sub_chain_end] && !quoted

    b_level -= 1
    next unless b_level.zero?

    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.zero?

  @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 = chain_to_split.slice(1..-1) if !@attributes[:chain_delimiter].empty? && chain_to_split.start_with?(@attributes[:chain_delimiter])

  first = true
  split_chain = if @attributes[:chain_delimiter].empty?
                  [chain_to_split]
                else
                  chain_to_split.split(@attributes[:chain_delimiter])
                end
  split_chain.each do |command|
    command = @attributes[:chain_delimiter] + command if first && @chain.start_with?(@attributes[:chain_delimiter])
    first = false

    command = command.strip

    # Replace the hacky delimiter that was used inside quotes with actual delimiters
    command = 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)..] : ''

    # Append a previous sign if none is present
    arguments += @attributes[:previous] unless arguments.include? @attributes[:previous]
    arguments = arguments.gsub @attributes[:previous], prev

    # Replace hacky previous signs with actual ones
    arguments = arguments.gsub hacky_prev, @attributes[:previous]

    arguments = arguments.split ' '

    # Replace the hacky spaces/newlines with actual ones
    arguments.map! do |elem|
      elem.gsub(hacky_space, ' ').gsub(hacky_newline, "\n")
    end

    # Finally execute the command
    prev = @bot.execute_command(command_name.to_sym, event, arguments, split_chain.length > 1 || @subchain)

    # Stop chain if execute_command failed (maybe the command doesn't exist, or permissions failed, etc.)
    break unless prev
  end

  prev
end