commandrb 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/commandrb.rb +90 -28
- metadata +4 -4
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 8e157c31805af8a1a19398b332dd1e711acc6086
         | 
| 4 | 
            +
              data.tar.gz: d9811c8a83caac8570795fb1e4525d2585dd72cf
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: d4f15b5e1e82bb14df045c85e207737c29a4b43fa44fbfcbd15d4d8b877dafdb7ef52dac2a0b45efdb422e55f8cd6d323de35617917743e41ccceee0ea1eac01
         | 
| 7 | 
            +
              data.tar.gz: 9acb080178dd98a8c15eacdd1673fefc9006be37db103e574cf481a5dea5bffe0e0ff3f63b25ccfde97ff70129f497ed0aa5edce3e96c991aba6ae16d1bd2e50
         | 
    
        data/lib/commandrb.rb
    CHANGED
    
    | @@ -1,4 +1,8 @@ | |
| 1 | 
            +
            require_relative 'helper'
         | 
| 2 | 
            +
             | 
| 1 3 | 
             
            class CommandrbBot
         | 
| 4 | 
            +
              
         | 
| 5 | 
            +
              ENV['COMMANDRB_MODE'] == 'debug' ? @debug_mode = true : @debug_mode = false
         | 
| 2 6 |  | 
| 3 7 | 
             
              # Be able to adjust the config on the fly.
         | 
| 4 8 | 
             
              attr_accessor :config
         | 
| @@ -41,7 +45,7 @@ class CommandrbBot | |
| 41 45 | 
             
                @config[:delete_activators] = false if @config[:delete_activators].nil?
         | 
| 42 46 |  | 
| 43 47 | 
             
                if @config[:token].nil? or init_hash[:token] == ''
         | 
| 44 | 
            -
                   | 
| 48 | 
            +
                  raise 'No token supplied in init hash!'
         | 
| 45 49 | 
             
                  return false
         | 
| 46 50 | 
             
                end
         | 
| 47 51 |  | 
| @@ -50,12 +54,13 @@ class CommandrbBot | |
| 50 54 |  | 
| 51 55 | 
             
                if init_type == :bot
         | 
| 52 56 | 
             
                  if init_hash[:client_id].nil?
         | 
| 53 | 
            -
                     | 
| 57 | 
            +
                    raise 'No client ID or invalid client ID supplied in init hash!'
         | 
| 54 58 | 
             
                    return false
         | 
| 55 59 | 
             
                  end
         | 
| 56 60 | 
             
                end
         | 
| 57 61 |  | 
| 58 62 | 
             
                @config[:owners] = init_hash[:owners]
         | 
| 63 | 
            +
                @config[:owners] = [] if @config[:owners].nil?
         | 
| 59 64 |  | 
| 60 65 | 
             
                @prefixes = init_hash[:prefixes]
         | 
| 61 66 |  | 
| @@ -76,6 +81,7 @@ class CommandrbBot | |
| 76 81 |  | 
| 77 82 | 
             
                # Command processing
         | 
| 78 83 | 
             
                @bot.message do |event|
         | 
| 84 | 
            +
                  @finished = false
         | 
| 79 85 | 
             
                  @command = nil
         | 
| 80 86 | 
             
                  @event = nil
         | 
| 81 87 | 
             
                  @chosen = nil
         | 
| @@ -83,15 +89,20 @@ class CommandrbBot | |
| 83 89 | 
             
                  @rawargs = nil
         | 
| 84 90 | 
             
                  @continue = false
         | 
| 85 91 | 
             
                  @prefixes.each { |prefix|
         | 
| 92 | 
            +
                    break if @finished
         | 
| 86 93 | 
             
                    if event.message.content.start_with?(prefix)
         | 
| 87 94 |  | 
| 88 95 | 
             
                      @commands.each { | key, command |
         | 
| 96 | 
            +
                        break if @finished
         | 
| 97 | 
            +
                        puts ":: Considering #{key.to_s}"  if @debug_mode
         | 
| 89 98 | 
             
                        triggers =  command[:triggers].nil? ? [key.to_s] : command[:triggers]
         | 
| 90 99 |  | 
| 91 100 | 
             
                        triggers.each { |trigger|
         | 
| 92 101 | 
             
                          @activator = prefix + trigger.to_s
         | 
| 102 | 
            +
                          puts @activator if @debug_mode
         | 
| 93 103 | 
             
                          @activator = @activator.downcase
         | 
| 94 104 | 
             
                          if event.message.content.downcase.start_with?(@activator)
         | 
| 105 | 
            +
                            puts "Prefix matched! #{@activator}" if @debug_mode
         | 
| 95 106 |  | 
| 96 107 | 
             
                            # Continue only if you've already chosen a choice.
         | 
| 97 108 | 
             
                            unless @chosen.nil?
         | 
| @@ -99,20 +110,26 @@ class CommandrbBot | |
| 99 110 | 
             
                              # Example: sh is chosen, shell is the new one.
         | 
| 100 111 | 
             
                              # In this example, shell would override sh, preventing ugly bugs.
         | 
| 101 112 | 
             
                              if @activator.start_with?(@chosen)
         | 
| 113 | 
            +
                                puts "#{@activator} just overrode #{@chosen}" if @debug_mode
         | 
| 102 114 | 
             
                                @chosen = @activator
         | 
| 103 115 | 
             
                              # Otherwhise, just give up.
         | 
| 104 116 | 
             
                              else
         | 
| 117 | 
            +
                                puts "Match failed..." if @debug_mode
         | 
| 105 118 | 
             
                                next
         | 
| 106 119 | 
             
                              end
         | 
| 107 120 | 
             
                            # If you haven't chosen yet, get choosing!
         | 
| 108 121 | 
             
                            else
         | 
| 122 | 
            +
                                puts "First match obtained!" if @debug_mode
         | 
| 109 123 | 
             
                                @continue = true
         | 
| 110 124 | 
             
                                @chosen = @activator
         | 
| 111 125 | 
             
                            end
         | 
| 112 126 | 
             
                          end
         | 
| 113 127 | 
             
                        }
         | 
| 128 | 
            +
                        
         | 
| 129 | 
            +
                        puts "Result: #{@chosen}" if @debug_mode
         | 
| 114 130 |  | 
| 115 | 
            -
                        next  | 
| 131 | 
            +
                        next if !@continue
         | 
| 132 | 
            +
                        puts "Final esult: #{@chosen}" if @debug_mode
         | 
| 116 133 |  | 
| 117 134 | 
             
                        break if @config[:selfbot] && event.user.id != @bot.profile.id
         | 
| 118 135 |  | 
| @@ -125,9 +142,20 @@ class CommandrbBot | |
| 125 142 | 
             
                        command[:delete_activator] = @config[:delete_activators] if command[:delete_activator].nil?
         | 
| 126 143 |  | 
| 127 144 | 
             
                        # If the command is set to owners only and the user is not the owner, show error and abort.
         | 
| 145 | 
            +
                        puts "[DEBUG] Command being processed: '#{command}'" if @debug_mode
         | 
| 146 | 
            +
                        puts "[DEBUG] Owners only? #{command[:owners_only]}" if @debug_mode
         | 
| 128 147 | 
             
                        if command[:owners_only]
         | 
| 129 | 
            -
                           | 
| 130 | 
            -
                            event. | 
| 148 | 
            +
                          if !@config[:owners].include?(event.user.id)
         | 
| 149 | 
            +
                            event.channel.send_message('', false,
         | 
| 150 | 
            +
                              Helper.error_embed(
         | 
| 151 | 
            +
                               error: "You don't have permission for that!\nOnly owners are allowed to access this command.",
         | 
| 152 | 
            +
                               footer: "Command: `#{event.message.content}`",
         | 
| 153 | 
            +
                               colour: 0xFA0E30,
         | 
| 154 | 
            +
                               code_error: false
         | 
| 155 | 
            +
                              )
         | 
| 156 | 
            +
                            )
         | 
| 157 | 
            +
                            puts 'Were returning!'
         | 
| 158 | 
            +
                            @finished = true
         | 
| 131 159 | 
             
                            next
         | 
| 132 160 | 
             
                          end
         | 
| 133 161 | 
             
                        end
         | 
| @@ -151,6 +179,7 @@ class CommandrbBot | |
| 151 179 | 
             
                            event.respond('❌ This command will only work in servers!')
         | 
| 152 180 | 
             
                          end
         | 
| 153 181 | 
             
                          # Abort!
         | 
| 182 | 
            +
                          @finished = true
         | 
| 154 183 | 
             
                          next
         | 
| 155 184 | 
             
                        end
         | 
| 156 185 |  | 
| @@ -158,47 +187,80 @@ class CommandrbBot | |
| 158 187 | 
             
                        # ...then abort :3
         | 
| 159 188 | 
             
                        if (event.user.bot_account? && command[:parse_bots] == false) || (event.user.bot_account? && @config[:parse_bots] == false)
         | 
| 160 189 | 
             
                          # Abort!
         | 
| 190 | 
            +
                          @finished = true
         | 
| 161 191 | 
             
                          next
         | 
| 162 192 | 
             
                        end
         | 
| 163 193 |  | 
| 164 194 | 
             
                        # If the config is setup to show typing messages, then do so.
         | 
| 165 195 | 
             
                        event.channel.start_typing if command[:typing]
         | 
| 166 196 |  | 
| 167 | 
            -
                         | 
| 168 | 
            -
                        args  | 
| 169 | 
            -
                         | 
| 170 | 
            -
             | 
| 171 | 
            -
             | 
| 197 | 
            +
                        args = event.message.content.split(' ')
         | 
| 198 | 
            +
                        # Parse args if args exist !
         | 
| 199 | 
            +
                        if args.length > 2
         | 
| 200 | 
            +
                          spaces = 1
         | 
| 201 | 
            +
                          # Prefixes with spaces are special and need to be parsed differently : )
         | 
| 202 | 
            +
                          if prefix.include? " "
         | 
| 203 | 
            +
                            spaces += prefix.count(' ')
         | 
| 204 | 
            +
                            args = event.message.content.slice!(args[0].size + args[1].size + spaces, event.message.content.size)
         | 
| 205 | 
            +
                          else
         | 
| 206 | 
            +
                            args = event.message.content.slice!(args[0].size + spaces, event.message.content.size)
         | 
| 207 | 
            +
                          end
         | 
| 208 | 
            +
                          # Split the argmuents into an array for easy usage but keep the raw args !!
         | 
| 209 | 
            +
                          rawargs = args
         | 
| 210 | 
            +
                          args = args.split(/ /)
         | 
| 211 | 
            +
                        end
         | 
| 212 | 
            +
             | 
| 172 213 |  | 
| 173 214 | 
             
                        # Check the number of args for the command.
         | 
| 174 | 
            -
                         | 
| 175 | 
            -
                           | 
| 176 | 
            -
             | 
| 177 | 
            -
             | 
| 215 | 
            +
                        unless command[:max_args].nil?
         | 
| 216 | 
            +
                          if command[:max_args] > 0 && args.length > command[:max_args]
         | 
| 217 | 
            +
                            # May be replaced with an embed.
         | 
| 218 | 
            +
                            event.channel.send_message('', false,
         | 
| 219 | 
            +
                                Helper.error_embed(
         | 
| 220 | 
            +
                                 error: "Too many arguments! \nMax arguments: `#{command[:max_args]}`",
         | 
| 221 | 
            +
                                 footer: "Command: `#{event.message.content}`",
         | 
| 222 | 
            +
                                 colour: 0xFA0E30,
         | 
| 223 | 
            +
                                 code_error: false
         | 
| 224 | 
            +
                                )
         | 
| 225 | 
            +
                              )
         | 
| 226 | 
            +
                            break
         | 
| 227 | 
            +
                          end
         | 
| 178 228 | 
             
                        end
         | 
| 229 | 
            +
                        
         | 
| 230 | 
            +
                        # If the command is configured to catch all errors, thy shall be done.
         | 
| 231 | 
            +
                        if !command[:catch_errors] || @config['catch_errors']
         | 
| 232 | 
            +
                          # Run the command code!
         | 
| 233 | 
            +
                          command[:code].call(event, args, rawargs)
         | 
| 234 | 
            +
                        else
         | 
| 235 | 
            +
                          # Run the command code, but catch all errors and output accordingly.
         | 
| 236 | 
            +
                          begin
         | 
| 237 | 
            +
                            command[:code].call(event, args, rawargs)
         | 
| 238 | 
            +
                          rescue Exception => e
         | 
| 239 | 
            +
                            event.respond("❌ An error has occured!! ```ruby\n#{e}```Please contact the bot owner with the above message for assistance.")
         | 
| 240 | 
            +
                          end
         | 
| 241 | 
            +
                        end
         | 
| 242 | 
            +
             | 
| 243 | 
            +
                        unless command[:required_permissions].nil?
         | 
| 244 | 
            +
                          command[:required_permissions].each { |x|
         | 
| 245 | 
            +
                            if event.user.on(event.server).permission?(x,event.channel)
         | 
| 246 | 
            +
                              event.respond('❌ You don\'t have permission for that!')
         | 
| 247 | 
            +
                              @finished = true
         | 
| 248 | 
            +
                              next
         | 
| 249 | 
            +
                            end
         | 
| 250 | 
            +
                          }
         | 
| 251 | 
            +
                          end
         | 
| 179 252 |  | 
| 180 253 | 
             
                        # All done here.
         | 
| 254 | 
            +
                        puts "Finished!! Executed command: #{@chosen}" if @debug_mode
         | 
| 181 255 | 
             
                        @command = command
         | 
| 182 256 | 
             
                        @event = event
         | 
| 183 257 | 
             
                        @args = args
         | 
| 184 258 | 
             
                        @rawargs = rawargs
         | 
| 259 | 
            +
                        @finished = true
         | 
| 185 260 | 
             
                        break
         | 
| 186 261 | 
             
                      }
         | 
| 187 | 
            -
                      # If the command is configured to catch all errors, thy shall be done.
         | 
| 188 | 
            -
                      if !@command[:catch_errors] || @config['catch_errors']
         | 
| 189 | 
            -
                        # Run the command code!
         | 
| 190 | 
            -
                        @command[:code].call(@event, @args, @rawargs)
         | 
| 191 | 
            -
                      else
         | 
| 192 | 
            -
                        # Run the command code, but catch all errors and output accordingly.
         | 
| 193 | 
            -
                        begin
         | 
| 194 | 
            -
                          @command[:code].call(@event, @args, @rawargs)
         | 
| 195 | 
            -
                        rescue Exception => e
         | 
| 196 | 
            -
                          event.respond("❌ An error has occured!! ```ruby\n#{e}```Please contact the bot owner with the above message for assistance.")
         | 
| 197 | 
            -
                        end
         | 
| 198 | 
            -
                      end
         | 
| 199 | 
            -
                      break
         | 
| 200 262 | 
             
                    end
         | 
| 201 263 | 
             
                  }
         | 
| 202 264 | 
             
                end
         | 
| 203 265 | 
             
              end
         | 
| 204 | 
            -
            end
         | 
| 266 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: commandrb
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.4.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 | 
            -
            - Seriel | 
| 7 | 
            +
            - Erisa Komuro (Seriel)
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2017- | 
| 11 | 
            +
            date: 2017-10-31 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: discordrb
         | 
| @@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 57 57 | 
             
                  version: '0'
         | 
| 58 58 | 
             
            requirements: []
         | 
| 59 59 | 
             
            rubyforge_project: 
         | 
| 60 | 
            -
            rubygems_version: 2.6. | 
| 60 | 
            +
            rubygems_version: 2.6.12
         | 
| 61 61 | 
             
            signing_key: 
         | 
| 62 62 | 
             
            specification_version: 4
         | 
| 63 63 | 
             
            summary: Commandrb
         |